Database Design A2 Computing Comp3
Jan 01, 2016
Database Design
A2 Computing Comp3
Aims and Objectives
• To understand the process of breaking down complex data structures using normalisation
• To understand the need for normalisation in database design
• To be able to normalise a set of data to produce a relational database design.
What is a relational database?
• A database which has many interrelated tables• Each table has a heading (fields / attributes)• Each table contains data (records or tuples)• Tables define entities (objects of interest )• Entities are linked together to form
relationships (1:1, 1:n, n:n)• These objects and relationships are modelled
using an ER diagram
Linking database tables
• Each table in a database must have a name and a primary key
• A primary key is a field or attribute which will uniquely identify each row (tuple) in the table
• Sometimes more than one attribute is needed to uniquely identify each row
• A composite key is made up of 2 or more attributes to uniquely identify each row in the table
• A foreign key is an attribute in one table that is a primary key of another table.
• Links between tables are made using the foreign key
ExamplesRegistration number
Model Manufacturer code
Colour Date of Registration
Cost
AZ06 GNS Passat VW01 Blue 31/01/06 £12,000
AS56 CHA Astra FD01 Silver 01/08/05 £11,500Manufacturer Code Name Head office
VW01 Volkswagen …………….
FD01 Ford ……………..
Which attribute would be suitable for a primary key?
ExamplesRegistration number
Model Manufacturer code
Colour Date of Registration
Cost
AZ06 GNS Passat VW01 Blue 31/01/06 £12,000
AS56 CHA Astra FD01 Silver 01/08/05 £11,500Manufacturer Code Name Head office
VW01 Volkswagen …………….
FD01 Ford ……………..
Which attribute is being used as a foreign key?Can you think of a potential problem using foreign keys?
Database design
• How do we come up with a database design that will work?
• Some data structures are complex• It can be difficult to represent them accurately
without errors and inconsistencies• This man came up with a formal process called normalisation
Dr Edgar F Codd
Normalisation
• Normalisation is about finding good ways of arranging the data into entities and attributes in a database
• It’s a formal process that ensures your database will work efficiently and effectively
• Consists of 3 stages– first normal form (1NF)– second normal form (2NF)– third normal form (3NF)
• Each stage in normalisation is designed to remove potential problems in entity design
Why normalise?
• To ensure data is not duplicated • To ensure data is consistent throughout the
database• To ensure the structure of each table is
flexible enough to allow you to enter as many or as few items as required
• The structure should enable a user to make all kinds of complex queries relating data from different entities
Worked example
Student no.
Student name
DOB Gender Course no.
Course name
Lecturer no.
Lecturer name
12345 Smith,D 20/3/1996 M EC6654 Computing L1783 Bloggs,F
24661 Head,G 13/2/1996 F EC6654 Computing L1783 Bloggs,F
MU7881 Music L2311 Day,M
MA4112 Maths L1902 West,A
66883 Evans, L 9/11/1995 M MA4112 Maths L1902 West,A
BU2213 Business L1877 Reed, K
Sample data held on Students taking courses at college
Primary keyfor each tuple
First Normal form
• A table is in first normal form (1NF) if it contains no repeating attributes or groups of attributes.
• For each student what info is repeated? (what info occurs more than once with different values each time?)• Establishing 1NF means removing the
repeating attributes into another table and adding the primary key from the first table
Establishing 1NFStudent no.
Student name
DOB Gender
12345 Smith,D 20/3/1996 M
24661 Head,G 13/2/1996 F
66883 Evans, L 9/11/1995 M
Student no.
Course no.
Course name
Lecturer no.
Lecturer name
1245 EC6654 Computing L1783 Bloggs,F
24661 EC6654 Computing L1783 Bloggs,F
24661 MU7881
Music L2311 Day,M
24661 MA4112 Maths L1902 West,A
66883 MA4112 Maths L1902 West,A
66883 BU2213 Business L1877 Reed, K
Standard Notation
STUDENT(studentNo,StudentName,DOB,Gender)
Student no.
Student name
DOB Gender
12345 Smith,D 20/3/1996 M
24661 Head,G 13/2/1996 F
66883 Evans, L 9/11/1995 M
Standard Notation
STUDENTCOURSE(StudentNo,CourseNo,CourseName,LecturerNo,LecturerName)
Student no.
Course no.
Course name
Lecturer no.
Lecturer name
1245 EC6654 Computing L1783 Bloggs,F
24661 EC6654 Computing L1783 Bloggs,F
24661 MU7881
Music L2311 Day,M
24661 MA4112 Maths L1902 West,A
66883 MA4112 Maths L1902 West,A
66883 BU2213 Business L1877 Reed, K
Second normal form
• A table is in second normal form (2NF) if it is in 1NF and contains no partial key dependencies.
• Every attribute in the table which is not a key depends on all keys present
• If there is only part dependency then attributes are removed and placed into another table with the part key dependency
Establishing 2NF
• Looking at tables with more than one key presentSTUDENTCOURSE(StudentNo,CourseNo,CourseName,LecturerNo,LecturerName )
• Remove any partial key dependencies• CourseName,LecturerNo,LecturerName depend
on CourseNo and not on StudentNo• So we can remove them into a new table with a
copy of CourseNo
Establishing 2NF
• The Course table becomesCOURSE(CourseNo,CourseName,LecturerNo,LecturerName)
• The STUDENTCOURSE table becomesSTUDENT_COURSE(studentNo, CourseNo)
• We now have three tables in 2NF STUDENT(studentNo, studentName, DOB,Gender) STUDENT_COURSE(studentNo, CourseNo)COURSE(CourseNo,CourseName,LecturerNo,LecturerName)
Student no.
Student name
DOB Gender
12345 Smith,D 20/3/1996 M
24661 Head,G 13/2/1996 F
66883 Evans, L 9/11/1995 M
Student no. Course no.
12345 EC6654
24661 MU7881
24661 MA4112
24661 EC6654
66883 MA4112
66883 BU2213
Course no.
Course name
Lecturer no.
Lecturer name
EC6654 Computing L1783 Bloggs,F
MU7881 Music L2311 Day,M
MA4112 Maths L1902 West,A
BU2213 Business L1877 Reed, K
Third normal form
• A table is in third normal form (3NF) if it is in 2NF and contains no non-key dependencies
• Looking at the tables so far:- STUDENT(studentNo, studentName, DOB,Gender)
STUDENT_COURSE(studentNo, CourseNo)COURSE(CourseNo,CourseName,LecturerNo,LecturerName)
• Is there anything in these tables that does not depend on the primary key of the table?
Establishing 3NF
COURSE(CourseNo,CourseName,LecturerNo,LecturerName)
• Lecturer name is not defined by courseNo • Lecturer name is defined by LecturerNo so we
remove them into a new tableLECTURER(LecturerNo, LecturerName)
• The Course table keeps a link to the Lecturer by adding the key
COURSE(CourseNo,CourseName,LecturerNo)
Student no.
Student name
DOB Gender
12345 Smith,D 20/3/1996 M
24661 Head,G 13/2/1996 F
66883 Evans, L 9/11/1995 M
Student no. Course no.
12345 EC6654
24661 MU7881
24661 MA4112
24661 EC6654
66883 MA4112
66883 BU2213
Course no.
Course name
Lecturer no.
EC6654 Computing L1783
MU7881 Music L2311
MA4112 Maths L1902
BU2213 Business L1877
Lecturer no.
Lecturer name
L1783 Bloggs,F
L2311 Day,M
L1902 West,A
L1877 Reed, K
Summary
• Normalisation is a formal process to determine the structure of a database
• 1NF – remove repeating attributes• 2NF – remove part-key dependencies• 3NF – remove non-key dependancies(The key, the whole key and nothing but the key, so help me Codd!”)
Glossary
Primary Key: an attribute that uniquely identifies a tuple
Foreign Key: an attribute in one table that is a primary key in another table
Normalisation: a technique used to produce a set of entities with no redundant data
Composite key: a combination of attributes that uniquely identify a tuple
Referential integrity: if a value appears as a foreign key in one table it must also appear as a primary key in another