140 likes | 298 Views
Database Design. CP4 Revision. Database Design. Careful database design is needed to avoid… Data Redundancy – storing the same data item in more than one place (waste of storage space) Data Inconsistency – two versions of the same data may be different. Entities.
E N D
Database Design CP4 Revision Computing
Database Design • Careful database design is needed to avoid… • Data Redundancy – storing the same data item in more than one place (waste of storage space) • Data Inconsistency – two versions of the same data may be different. Computing
Entities • An entity is a thing about which data is stored (Eg Customer, Employee, Stock) • A relationship is a link between two entities. Computing
Relationships • One-to-One : One Pupil has one Network Account. • One-to-Many : One Pupil borrows many LibraryBooks • Many-to-Many : One Teacher teaches many Pupils – One Pupil is taught by many Teachers. Computing
Entity-Relationship Diagrams One-to-One One-to-Many Many-to-Many Computing
Key Fields • Each Entity must have a unique key field – the primary key. PatientID is the primary key field. An Entity may have foreign keys – primary key fields of other entities. These provide the links (relationships) between different entities. WardID is a foreign key. Computing
Table Design • The standard notation for writing down the design of a table… • CAPITAL letters for the name of the table • Underline the key field, • Italicise the foreign keys (or overline). • Eg.:PATIENTS (PatientID, Surname, Forename, Disease, WardID) Computing
Database Normalisation • Avoids data duplication • Avoids data inconsistencies • …ensures the best possible design for a database so other applications can use it. Computing
Un-Normalised Data There are a number of repeated fields here… To change into First Normal Form – the repeated groups of fields must go… Split into two linked tables… Computing
First Normal Form • PUPILS ( PupilID, PupilName, DOB, ExamID) • EXAMS ( ExamID, Subject, Level, Date, RoomID, RoomName) • The key field of the PUPILS table is a combined key field. • The link is made through the foreign key ‘ExamID’. To get this into Second Normal Form, no field must be dependant on only part of the key field. Do this by creating a linking table HINT : You need to do this every time you have a Many-to-Many relationship) Computing
Second Normal Form • PUPILS ( PupilID, PupilName, DOB) • EXAMS ( ExamID, Subject, Level, Date, RoomID, RoomName) • PUPIL_SITS(PupilID,ExamID) The Many-to-Many relationship… …must be changed to… Computing
Second Normal Form To make this database into Third Normal Form, there must be no non-key dependencies… ..so separate the Rooms into another table… Computing
Third Normal Form PUPILS (PupilID, PupilName, DOB) EXAMS (ExamID, Subject, Level, Date, RoomID) PUPIL_SITS (PupilID, ExamID) ROOMS (RoomID, RoomName) Computing