Database Design I IST 210: Organization of Data IST2101
2 I want a database to maintain departments in my company. Store information about my employees, their projects and assignments. I want …. $$$ User requirement A database YOUR JOB! Data Modeling (Ch.4) Data Modeling (Ch.4) Database Design (Ch.5)
Two-Step Approach: Step 1 IST2103 Input: User requirement; Output: E-R Diagram Use Entity-Relationship Diagram (E-R Diagram) to capture all user requirements Data Modeling (Ch.4) Data Modeling (Ch.4) Database Design (Ch.5)
Two-Step Approach: Step 2 IST2104 Data Modeling (Ch.4) Data Modeling (Ch.4) Database Design (Ch.5) Input: E-R diagram; Output: A database Transferring a data model to a relational database Entities Relations Relationships Foreign keys and extra
Chapter Key Objective Data Model (E-R Diagram) Relational Model (Tables) IST Entity Table Entity name Table name Identifier Primary key Attributes Attributes 2.Relationship Foreign key or a new table Determined by the maximal cardinality 3.Specify the attribute properties Data type Key Required Determined by the minimal cardinality Remarks
E-R Diagram Tables IST Entity Table Entity name Table name Identifier Primary key Attributes Attributes 2.Relationship Foreign key or a new table 3.Specify the attribute properties
Example IST2107 LockerID LockerRoom LockerSize EmployeeID Name OfficeNumber OfficePhone LOCKER EMPLOYEE 1)Entity name Table name 2)Identifier Primary key 3)Attributes Attributes EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize) *primary key: use underline
E-R Diagram Tables IST Entity Table Entity name Table name Identifier Primary key Attributes Attributes 2.Relationship Foreign key or a new table Determined by the maximal cardinality 3.Specify the attribute properties
Maximal Cardinality 1:1 (one-to-one) Relationship 1:N (one-to-many) Relationship N:M (many-to-many) Relationship IST2109 Entity AEntity B Entity AEntity B Entity A Entity B
1:1 Relationship IST21010 Locker ID Locker Room Locker Size 1#210 5#25 EMPLOYEE LOCKER Relationship is not captured yet! Add foreign key ? Employee ID NameOffice Number Office Phone 4Tom EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize)
IST21011 Employee ID NameOffice Number Office Phone Locker ID 4Tom Locker ID Locker Room Locker Size 1#210 5#25 EMPLOYEELOCKER Employee 4 owns locker 1; locker 1 belongs to employee 4. Employee ID NameOffice Number Office Phone 4Tom Locker ID Locker Room Locker Size Employee ID 1#2104 5#25 EMPLOYEELOCKER Locker ID Locker Room Locker Size Employee ID 1#2104 5#25 EMPLOYEELOCKER Employee ID NameOffice Number Office Phone Locker ID 4Tom Option A Option B Option C
1:1 Relationship IST21012 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID) Option A Option B Both options are correct. Option A is preferred. If choosing Option A, since every employee has one locker, LockerID must have a value in EMPLOYEE table. If choosing Option B, since some lockers does not belong to anyone, EmployeeID will be NULL value for some lockers and it takes extra space. (See the previous slide, Option B needs one more cell.) *foreign key: italic
1:1 Relationship The maximum cardinality determines how a relationship is represented 1:1 relationship –The key from one relation is placed in the other as a foreign key It does not matter which table receives the foreign key We prefer the option taking less space IST21013
1:N Relationship IST21014 Employee ID Last Name First Name Office Number Office Phone 4GreenTom Locker ID Locker Room Locker Size 1#210 5#25 EMPLOYEE LOCKER Relationship is not captured yet!Add foreign key ? LockerID LockerRoom LockerSize EmployeeID Name OfficeNumber OfficePhone LOCKER EMPLOYEE Assume one-to-many relationship
IST21015 Employee ID NameOffice Number Office Phone Locker ID 4Tom , 5 Locker ID Locker Room Locker Size 1#210 5#25 EMPLOYEELOCKER Employee 4 owns locker 1 and 5. Employee ID NameOffice Number Office Phone 4Tom Locker ID Locker Room Locker Size Employee ID 1#2104 5#254 EMPLOYEELOCKER LockerIDLocker Room Locker Size Employee ID 1#2104 5#255 EMPLOYEELOCKER Employe eID NameOffice Number Office Phone Locker ID 4Tom , 5 Option A Option B Option C
IST21016 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID) 1:N Relationship
Like a 1:1 relationship, a 1:N relationship is saved by placing the key from one table into another as a foreign key However, in a 1:N the foreign key always goes into the many-side of the relationship –The 1 side is called the parent –The N side is called the child IST :N Relationship
N:M Relationship IST21018 Employee ID NameOffice Number Office Phone 4Tom John Locker ID Locker Room Locker Size 1#210 5#25 EMPLOYEE LOCKER Relationship is not captured yet!Add foreign key ? LockerID LockerRoom LockerSize EmployeeID Name OfficeNumber OfficePhone LOCKER EMPLOYEE Assume many-to-many relationship
IST21019 Employee ID NameOffice Number Office Phone Locker ID 4Tom , 5 10John Locker Number Locker Room Locker Size 1#210 5#25 EMPLOYEELOCKER Employee 4 owns locker 1 and 5. Employee 10 owns locker 1. Employee ID NameOffice Number Office Phone 4Tom John Locker ID Locker Room Locker Size Employee ID 1#2104, 10 5#254 EMPLOYEELOCKER Option A Option B
IST21020 Employee ID NameOffice Number Office Phone 4Tom John Locker ID Locker Room Locker Size 1#210 5#25 EMPLOYEELOCKER Employee 4 owns locker 1 and 5. Employee 10 owns locker 1. Employee ID Locker ID ASSIGNMENT NOT SIMPLY ADDING FOREIGN KEYS! CREATE ANOTHER TABLE!
IST21021 EMPLOYEE(EmployeeID, LastName, FirstName, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize) ASSIGNMENT(EmployeeID, LockerID) Note: EmployeeID and LockerID are both primary keys and foreign keys in ASSIGNMENT table N:M Relationship
To represent a N:M relationship in relational design, a new table must be created. –This table is called an intersection table An intersection table has a composite key consisting of the keys from each of the tables that it connects IST21022 N:M Relationship
Relationship Summary IST21023 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID) N:M add an intersection table EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize) ASSIGNMENT(EmployeeID, LockerID) EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID) 1:N add a foreign key to the many-side table 1:1 add a foreign key to either table or
In-class Exercise 1 IST21024 Transform this diagram into tables UserID Name SpotID Location MonthlyCost USER PARKING
In-class Exercise 2 IST21025 BuildingName ApartmentID NumberOfBed NumberOfBath Rent BuildingName Address APARTMENT BUILDING Transform this diagram into tables
In-class Exercise 3 IST21026 CourseID CourseName Instructor StudentID StudentName COURSE STUDENT Transform this diagram into tables
In-class Exercise 4 IST21027 MovieID MovieName Director Year UserID Name MOVIE USER Transform this diagram into tables CommentID Rating Comment COMMENT
E-R Diagram Tables IST Entity Table Entity name Table name Identifier Primary key Attributes Attributes 2.Relationship Foreign key or a new table Determined by the maximal cardinality 1:1 add a foreign key to either table 1:N add a foreign key to the many-side table N:M add an intersection table 3.Specify the attribute properties
E-R Diagram Tables IST Entity Table Entity name Table name Identifier Primary key Attributes Attributes 2.Relationship Foreign key or a new table Determined by the maximal cardinality 3.Specify the attribute properties Data type Key Required Determined by the minimal cardinality Remarks
IST21030 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column NameData TypeKeyRequiredRemarks LockerID LockerRoom LockerSize LOCKER table Column NameData TypeKeyRequiredRemarks EmployeeID Name OfficeNumber OfficePhone LockerID
IST21031 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column NameData Type LockerIDint LockerRoomchar(10) LockerSizefloat LOCKER table Column NameData Type EmployeeIDint Namechar(50) OfficeNumberchar(20) OfficePhonechar(12) LockerIDint Data Type Determine the data types based on the type of these attributes Data types must be the types defined in SQL us/library/ms aspx us/library/ms aspx Data type of a foreign key must be the same type as its referred primary key LockerID in EMPLOYEE table must have the same data type as LockerID in LOCKER because LockerID is a foreign key in EMPLOYEE
IST21032 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column NameData Type LockerIDPrimary key LockerRoom LockerSize LOCKER table Column NameKey EmployeeIDPrimary key Name OfficeNumber OfficePhone LockerIDForeign key Key Specify primary key and foreign key(s) in the table
IST21033 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column NameRequired LockerIDYes LockerRoomYes LockerSizeYes LOCKER table Column NameRequired EmployeeIDYes NameYes OfficeNumberNo OfficePhoneNo LockerIDYes Required Whether an attribute is required or not determines whether we allow NULL value for this attribute Primary key must be required Normal attributes are determined based on requirements or common sense “not required” is preferred Foreign key is determined by minimum cardinality An employee must have at least one locker. So LockerID is required in EMPLOYEE
IST21034 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column NameRemarks LockerIDSurrogate key: initial value = 1 Increment = 1 LockerRoom LockerSizeDefault value: 10 LOCKER table Column NameRemarks EmployeeIDSurrogate key: initial value = 1 Increment = 1 Name OfficeNumber OfficePhoneFormat: ###-###-#### LockerIDReference: LOCKER Remarks If a primary key is a surrogate key, specify the initial value and increment If an attribute is a foreign key, specify which table it refers to If an attribute has a required format, specify the format If an attribute has a default value, specify the value
IST21035 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column Name Data Type KeyRequire d Remarks LockerIDintPrimary key YesSurrogate key: initial value = 1 Increment = 1 LockerRoomchar(10)Yes LockerSizefloatYesDefault value: 10 LOCKER table Column Name Data Type KeyRequiredRemarks EmployeeIDintPrimary key YesSurrogate key: initial value = 1 Increment = 1 Namechar(50)Yes OfficeNumberchar(20)No OfficePhonechar(12)NoFormat: ###-###-#### LockerIDintForeign key YesReference: LOCKER Now we complete converting an E-R diagram to tables Next, we can use SQL to create a real database
IST21036 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID) PARKING table Column NameData TypeKeyRequiredRemarks UserID Name SpotID USER table Column NameData TypeKeyRequiredRemarks SpotID Location MonthlyCost In-class Exercise 1
IST21037 BUILDING table Column NameData TypeKeyRequiredRemarks BuildingName ApartmentID NumberOfBed NumberOfBath Rent APARTMENT table Column NameData TypeKeyRequiredRemarks BuildingName Address BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent) In-class Exercise 2
IST21038 STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID) STUDENT table Column NameData TypeKeyRequiredRemarks CourseID CourseName Instructor COURSE table Column NameData TypeKeyRequiredRemarks StudentID StudentName Column NameData TypeKeyRequiredRemarks StudentID CourseID REGISTRATION table In-class Exercise 3
E-R Diagram Tables IST Entity Table Entity name Table name Identifier Primary key Attributes Attributes 2.Relationship Foreign key or a new table 3.Specify the attribute properties