Download presentation
Presentation is loading. Please wait.
Published bySybil Tyler Modified over 8 years ago
1
Structuring Data A Study of Primary Keys and Foreign Keys, Normalisation and Duplicated vs Redundant Data
2
Rows and Columns A “Row” in a database represents a thing So a row on the Student table represents a student A “Column” (also called an “Attribute”) is a list of a specific piece of data A column on the students table might contain student names The intersection of row and column contains a single piece of data This is important – no lists allowed! We’ll see more details on this later Each row in a table must be distinct This means that a row is always identifiable by some combination of its attributes
3
A Relation is a two dimensional table or array A Row is a record A Column is a data item, field or attribute
4
The cells of a table are single valued, i.e. no repeating groups are allowed. Any entry in a column must be of the same kind. Example: Student Id. The Order of the columns is unimportant. The Order of the rows is unimportant. No two rows of the table can be identical. The column must have a unique name. For a Table to be a Relation:
5
A Primary Key is a group of one or more attributes (columns) that uniquely identifies a row In theory a key can be made up of all attributes on a table – in practice we will generally use an ID on each table A Foreign Key is a column which exists on table B, while also being the Primary Key on table A Keys:
6
Student NoNameDoBe-mail 1 John Hancock01-Jan-92johnh@dit.ie 2Jane Doe01-Feb-92janed@dit.ie 3Bob Smith01-Mar-92bobs@dit.ie Enrolment ID Subject Name Academic YearStudent No 1Econ10120161 2Econ10120162 3Econ10120163 Student Table Enrolment Table Primary KeyForeign Key
7
Anomalies
8
IDNameDept NoDept Name 1000Black610Personnel 1001White640Admin 1004Yellow710Manufacturing 1005Green710Manufacturing
9
If we delete row 1 (1000, Black) we remove data about the employee, but also lose information about the Personnel department Deletion Anomaly:
10
If we want to insert a new department, but we don’t have any employees in that department, we are constrained from doing so Insertion Anomaly:
11
If we make a change to department name (eg “Manufacturing” becomes “Engineering”) we must duplicate this change across every row with Dept No 710. This is prone to errors Modification Anomaly:
12
IDName Dept No 1000Black610 1001White640 1004Yellow710 1005Green710 Dept NoDept Name 610Personnel 640Admin 710Manufacturing Primary Key Foreign Key
13
Fixing a poorly conceived database design – almost a certain exam question!
14
OrderIDProductIDProduct DescriptionCustomer NameCustomer Address 11Black InkJohn Smith123 Fake Street 12Cyan InkJohn Smith123 Fake Street 13Magenta InkJohn Smith123 Fake Street 21Black InkJane Doe234 Main Street 23Magenta InkJane Doe234 Main Street 24Yellow InkJane Doe234 Main Street 25Grey InkJane Doe234 Main Street 35Grey InkJohn Smith123 Fake Street 42Cyan InkSteven Hancock10 Schoolhouse Lane 53Magenta InkEva May15 Main Street 51Black InkEva May15 Main Street
15
Product ID Product Description 1Black Ink 2Cyan Ink 3Magenta Ink 4Yellow Ink 5Grey Ink Order ID Product ID 11 12 13 21 23 24 25 35 42 53 51 Customer NameCustomer AddressCustomer ID John Smith123 Fake Street1 Jane Doe234 Main Street2 Steven Hancock10 Schoolhouse Lane3 Eva May15 Main Street4 OrderIDCustomer ID 11 22 31 43 54
16
First Normal Form Removing repeating attributes – that is, “a data field which may occur for multiple values for a single value of the key” (we’re getting rid of “lists of items”) Move these to a new table, with a copy of the key
17
Order IDProduct ID Product Description 11Black Ink 12Cyan Ink 13Magenta Ink 21Black Ink 23Magenta Ink 24Yellow Ink 25Grey Ink 35 42Cyan Ink 53Magenta Ink 51Black Ink Order ID Customer NameCustomer Address Customer ID 1John Smith123 Fake Street1 1John Smith123 Fake Street1 1John Smith123 Fake Street1 2Jane Doe234 Main Street2 2Jane Doe234 Main Street2 2Jane Doe234 Main Street2 2Jane Doe234 Main Street2 3John Smith123 Fake Street1 4 Steven Hancock10 Schoolhouse Lane3 5Eva May15 Main Street4 5Eva May15 Main Street4
18
Second Normal Form Take each non-key attribute and check if it dependent on one part of the key If it is, move it to a new table
19
Product ID Product Description 1Black Ink 2Cyan Ink 3Magenta Ink 4Yellow Ink 5Grey Ink Order ID Product ID 11 12 13 21 23 24 25 35 42 53 51 Customer NameCustomer AddressCustomer ID John Smith123 Fake Street1 Jane Doe234 Main Street2 Steven Hancock10 Schoolhouse Lane3 Eva May15 Main Street4 OrderIDCustomer ID 11 22 31 43 54
20
Third Normal Form If a non-key attribute is more dependent on another non-key attribute than on the key, move them to a new table Leave the non-key attribute on which it is dependent in the original table and mark it a foreign key (Not relevant to this example – we’ll see this in the next example)
21
Project CodeProject Title Project Manager Project Budget Employee No Employee Name Department No Department Name Hourly Rate PC010 Pensions SystemsM Phillips € 24,500.00S10001A SmithL004IT € 22.00 PC010 Pensions SystemsM Phillips € 24,500.00S10030L JonesL023Pensions € 18.50 PC010 Pensions SystemsM Phillips € 24,500.00S21010P LewisL004IT € 21.00 PC045 Salaries SystemsH Martin € 17,400.00S10010B JonesL004IT € 21.75 PC045 Salaries SystemsH Martin € 17,400.00S10001A SmithL004IT € 18.00 PC045 Salaries SystemsH Martin € 17,400.00S31002T GilbertL028Database € 25.50 PC045 Salaries SystemsH Martin € 17,400.00S13210W RichardsL008Salary € 17.00 PC064HR SystemsK Lewis € 12,250.00S31002T GilbertL028Database € 23.25 PC064HR SystemsK Lewis € 12,250.00S21010P LewisL004IT € 17.50 PC064HR SystemsK Lewis € 12,250.00S10034B JamesL009HR € 16.50
22
First Normal Form Removing repeating attributes – that is, “a data field which may occur for multiple values for a single value of the key” (we’re getting rid of “list of items”) Move these to a new table, with a copy of they key
23
Project CodeProject TitleProject Manager Project Budget PC010Pensions SystemsM Phillips € 24,500.00 PC045Salaries SystemsH Martin € 17,400.00 PC064HR SystemsK Lewis € 12,250.00 Project CodeEmployee NoEmployee Name Department No Department Name Hourly Rate PC010S10001A SmithL004IT € 22.00 PC010S10030L JonesL023Pensions € 18.50 PC010S21010P LewisL004IT € 21.00 PC045S10010B JonesL004IT € 21.75 PC045S10001A SmithL004IT € 18.00 PC045S31002T GilbertL028Database € 25.50 PC045S13210W RichardsL008Salary € 17.00 PC064S31002T GilbertL028Database € 23.25 PC064S21010P LewisL004IT € 17.50 PC064S10034B JamesL009HR € 16.50
24
Second Normal Form Take each non-key attribute and check if it dependent on one part of the key If it is, move it to a new table
25
Project CodeProject TitleProject Manager Project Budget PC010Pensions SystemsM Phillips € 24,500.00 PC045Salaries SystemsH Martin € 17,400.00 PC064HR SystemsK Lewis € 12,250.00 Employee No Employee Name Department No Department Name S10001A SmithL004IT S10030L JonesL023Pensions S21010P LewisL004IT S10010B JonesL004IT S10001A SmithL004IT S31002T GilbertL028Database S13210W RichardsL008Salary S31002T GilbertL028Database S21010P LewisL004IT S10034B JamesL009HR Project CodeEmployee No Hourly Rate PC010S10001 € 22.00 PC010S10030 € 18.50 PC010S21010 € 21.00 PC045S10010 € 21.75 PC045S10001 € 18.00 PC045S31002 € 25.50 PC045S13210 € 17.00 PC064S31002 € 23.25 PC064S21010 € 17.50 PC064S10034 € 16.50
26
Third Normal Form If a non-key attribute is more dependent on another non-key attribute than on the key, move them to a new table Leave the non-key attribute on which it is dependent in the original table and mark it a foreign key
27
Project CodeProject TitleProject Manager Project Budget PC010Pensions SystemsM Phillips € 24,500.00 PC045Salaries SystemsH Martin € 17,400.00 PC064HR SystemsK Lewis € 12,250.00 Project CodeEmployee No Hourly Rate PC010S10001 € 22.00 PC010S10030 € 18.50 PC010S21010 € 21.00 PC045S10010 € 21.75 PC045S10001 € 18.00 PC045S31002 € 25.50 PC045S13210 € 17.00 PC064S31002 € 23.25 PC064S21010 € 17.50 PC064S10034 € 16.50 Employee No Employee Name Departmen t No S10001A SmithL004 S10030L JonesL023 S21010P LewisL004 S10010B JonesL004 S10001A SmithL004 S31002T GilbertL028 S13210W RichardsL008 S31002T GilbertL028 S21010P LewisL004 S10034B JamesL009 Departme nt No Department Name L004IT L023Pensions L028Database L008Salary
28
Summary of Questions for Revision Class
29
Project CodeProject Title Project Manager Project Budget Employee No Employee Name Department No Department Name Hourly Rate PC010 Pensions SystemsM Phillips € 24,500.00S10001A SmithL004IT € 22.00 PC010 Pensions SystemsM Phillips € 24,500.00S10030L JonesL023Pensions € 18.50 PC010 Pensions SystemsM Phillips € 24,500.00S21010P LewisL004IT € 21.00 PC045 Salaries SystemsH Martin € 17,400.00S10010B JonesL004IT € 21.75 PC045 Salaries SystemsH Martin € 17,400.00S10001A SmithL004IT € 18.00 PC045 Salaries SystemsH Martin € 17,400.00S31002T GilbertL028Database € 25.50 PC045 Salaries SystemsH Martin € 17,400.00S13210W RichardsL008Salary € 17.00 PC064HR SystemsK Lewis € 12,250.00S31002T GilbertL028Database € 23.25 PC064HR SystemsK Lewis € 12,250.00S21010P LewisL004IT € 17.50 PC064HR SystemsK Lewis € 12,250.00S10034B JamesL009HR € 16.50
31
OrderIDProductIDProduct DescriptionCustomer NameCustomer AddressCustomer ID 11Black InkJohn Smith123 Fake Street1 12Cyan InkJohn Smith123 Fake Street1 13Magenta InkJohn Smith123 Fake Street1 21Black InkJane Doe234 Main Street2 23Magenta InkJane Doe234 Main Street2 24Yellow InkJane Doe234 Main Street2 25Grey InkJane Doe234 Main Street2 35Grey InkJohn Smith123 Fake Street1 42Cyan InkSteven Hancock10 Schoolhouse Lane3 53Magenta InkEva May15 Main Street4 51Black InkEva May15 Main Street4
33
NINContract NoHours Employee Name Company ID Company Location 616681BSC102572P. WhiteSC115Belfast 674315ASC102548R. PressSC115Belfast 323113BSC102624P. SmithSC23Bangor 616681BSC102624P. WhiteSC23Bangor A engineering consultancy firm supplies temporary specialized staff to bigger companies in the country to work on their project for certain amount of time. The table below lists the time spent by each of the company’s employees at other companies to carry out projects. The National Insurance Number (NIN) is unique for every member of staff. Explain in which normal form this table is Find the Primary Key for this relation and explain your choice. Normalise the table to 2NF Normalise the tables to 3NF
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.