Download presentation
Presentation is loading. Please wait.
1
IST 210: Organization of Data
Database Design I IST 210: Organization of Data
2
YOUR JOB! User requirement A database
IST210 I want a database to maintain departments in my company. Store information about my employees, their projects and assignments. I want …. User requirement $$$ Data Modeling (Ch.4) YOUR JOB! Database Design (Ch.5) A database
3
Two-Step Approach: Step 1
IST210 Two-Step Approach: Step 1 Data Modeling (Ch.4) Database Design (Ch.5) Input: User requirement; Output: E-R Diagram Use Entity-Relationship Diagram (E-R Diagram) to capture all user requirements
4
Two-Step Approach: Step 2
IST210 Two-Step Approach: Step 2 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
5
Data Model (E-R Diagram) Relational Model (Tables)
IST210 Chapter Key Objective Data Model (E-R Diagram) Relational Model (Tables) Entity Table Entity name Table name Identifier Primary key Attributes Attributes Relationship Foreign key or a new table Determined by the maximal cardinality Specify the attribute properties Data type Key Required Determined by the minimal cardinality Remarks
6
E-R Diagram Tables Entity Table Entity name Table name
IST210 E-R Diagram Tables Entity Table Entity name Table name Identifier Primary key Attributes Attributes Relationship Foreign key or a new table Specify the attribute properties
7
Example EMPLOYEE LOCKER EmployeeID LockerID Name LockerRoom
IST210 Example EMPLOYEE LOCKER Entity name Table name Identifier Primary key Attributes Attributes EmployeeID LockerID Name OfficeNumber OfficePhone LockerRoom LockerSize EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize) *primary key: use underline
8
E-R Diagram Tables Entity Table Entity name Table name
IST210 E-R Diagram Tables Entity Table Entity name Table name Identifier Primary key Attributes Attributes Relationship Foreign key or a new table Determined by the maximal cardinality Specify the attribute properties
9
Maximal Cardinality 1:1 (one-to-one) Relationship
IST210 Maximal Cardinality 1:1 (one-to-one) Relationship 1:N (one-to-many) Relationship N:M (many-to-many) Relationship Entity A Entity B Entity A Entity B Entity A Entity B
10
1:1 Relationship EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone)
IST210 1:1 Relationship EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE LOCKER Employee ID Name Office Number Phone 4 Tom 281 9182 Locker ID Room Size 1 #2 10 5 Relationship is not captured yet! Add foreign key ?
11
Employee 4 owns locker 1; locker 1 belongs to employee 4. Option A
IST210 Employee 4 owns locker 1; locker 1 belongs to employee 4. Option A EMPLOYEE LOCKER Locker ID Room Size 1 #2 10 5 Employee ID Name Office Number Phone Locker ID 4 Tom 281 9182 1 Option B EMPLOYEE LOCKER Locker ID Room Size EmployeeID 1 #2 10 4 5 Employee ID Name Office Number Phone 4 Tom 281 9182 Option C EMPLOYEE LOCKER Locker ID Room Size EmployeeID 1 #2 10 4 5 Employee ID Name Office Number Phone Locker ID 4 Tom 281 9182 1
12
1:1 Relationship *foreign key: italic
IST210 1:1 Relationship *foreign key: italic EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) Option A EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID) 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.)
13
IST210 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
14
1:N Relationship EMPLOYEE LOCKER EmployeeID LockerID Name OfficeNumber
IST210 1:N Relationship EMPLOYEE LOCKER EmployeeID LockerID Name OfficeNumber OfficePhone LockerRoom LockerSize Assume one-to-many relationship EMPLOYEE LOCKER Employee ID Last Name First Office Number Phone 4 Green Tom 281 9182 Locker ID Room Size 1 #2 10 5 Relationship is not captured yet! Add foreign key ?
15
Employee 4 owns locker 1 and 5. Option A EMPLOYEE LOCKER
IST210 Employee 4 owns locker 1 and 5. Option A EMPLOYEE LOCKER Locker ID Room Size 1 #2 10 5 Employee ID Name Office Number Phone Locker ID 4 Tom 281 9182 1, 5 Option B EMPLOYEE LOCKER Locker ID Room Size EmployeeID 1 #2 10 4 5 Employee ID Name Office Number Phone 4 Tom 281 9182 Option C EMPLOYEE LOCKER EmployeeID Name Office Number Phone Locker ID 4 Tom 281 9182 1, 5 Locker ID Room Size EmployeeID 1 #2 10 4 5
16
1:N Relationship EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone)
IST210 1:N Relationship EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID)
17
IST210 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
18
N:M Relationship EMPLOYEE LOCKER EmployeeID LockerID Name OfficeNumber
IST210 N:M Relationship EMPLOYEE LOCKER EmployeeID LockerID Name OfficeNumber OfficePhone LockerRoom LockerSize Assume many-to-many relationship EMPLOYEE LOCKER Employee ID Name Office Number Phone 4 Tom 281 9182 10 John 777 1829 Locker ID Room Size 1 #2 10 5 Relationship is not captured yet! Add foreign key ?
19
Employee 4 owns locker 1 and 5. Employee 10 owns locker 1.
IST210 Employee 4 owns locker 1 and 5. Employee 10 owns locker 1. Option A EMPLOYEE LOCKER Employee ID Name Office Number Phone Locker ID 4 Tom 281 9182 1, 5 10 John 777 1829 1 Locker Number Room Size 1 #2 10 5 Option B EMPLOYEE LOCKER Locker ID Room Size EmployeeID 1 #2 10 4, 10 5 4 Employee ID Name Office Number Phone 4 Tom 281 9182 10 John 777 1829
20
Employee 4 owns locker 1 and 5. Employee 10 owns locker 1.
IST210 Employee 4 owns locker 1 and 5. Employee 10 owns locker 1. EMPLOYEE LOCKER Employee ID Name Office Number Phone 4 Tom 281 9182 10 John 777 1829 Locker ID Room Size 1 #2 10 5 ASSIGNMENT Employee ID Locker 4 1 5 10 NOT SIMPLY ADDING FOREIGN KEYS! CREATE ANOTHER TABLE!
21
IST210 N:M Relationship 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
22
IST210 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
23
Relationship Summary 1:1 add a foreign key to either table
IST210 Relationship Summary 1:1 add a foreign key to either table (preferred) EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) or EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone) LOCKER(LockerID, LockerRoom, LockerSize, EmployeeID) 1:N add a foreign key to the many-side table 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)
24
In-class Exercise 1 Transform this diagram into tables PARKING USER
IST210 In-class Exercise 1 Transform this diagram into tables PARKING USER SpotID UserID Location MonthlyCost Name
25
In-class Exercise 1 Transform this diagram into tables PARKING USER
IST210 In-class Exercise 1 Transform this diagram into tables PARKING USER SpotID UserID Location MonthlyCost Name PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID)
26
In-class Exercise 1: Notes
IST210 In-class Exercise 1: Notes PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID) PARKING(SpotID, Location, MonthlyCost, UserID) USER(UserID, Name, ) Note: both options are correct, but the first one is preferred because of the minimal cardinality of SpotID is mandatory.
27
In-class Exercise 2 Transform this diagram into tables APARTMENT
IST210 In-class Exercise 2 Transform this diagram into tables BuildingNameApartmentID NumberOfBed NumberOfBath Rent BuildingName Address APARTMENT BUILDING
28
In-class Exercise 2 Transform this diagram into tables
IST210 In-class Exercise 2 Transform this diagram into tables BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent)
29
In-class Exercise 2: Notes
IST210 In-class Exercise 2: Notes BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent) Notes: * A common mistake is making ApartmentID as the only primary key. (BuildingName, ApartmentID) should a composite key for APARTMENT * Another common mistake is to create a duplicate attribute BuildingName in APARTMENT. Or forget to make BuildingName as the foreign key
30
In-class Exercise 3 Transform this diagram into tables STUDENT COURSE
IST210 In-class Exercise 3 Transform this diagram into tables CourseID CourseName Instructor StudentID StudentName COURSE STUDENT
31
In-class Exercise 3 Transform this diagram into tables STUDENT COURSE
IST210 In-class Exercise 3 Transform this diagram into tables CourseID CourseName Instructor StudentID StudentName COURSE STUDENT STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID)
32
In-class Exercise 3: Notes
IST210 In-class Exercise 3: Notes STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID) Notes: * In REGISTRATION table, StudetID and CourseID are both primary key and foreign key. So you should give both underline and italic (wave underline in hand-written format) to them.
33
E-R Diagram Tables Entity Table Entity name Table name
IST210 E-R Diagram Tables Entity Table Entity name Table name Identifier Primary key Attributes Attributes 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 Specify the attribute properties
34
E-R Diagram Tables Entity Table Entity name Table name
IST210 E-R Diagram Tables Entity Table Entity name Table name Identifier Primary key Attributes Attributes Relationship Foreign key or a new table Determined by the maximal cardinality Specify the attribute properties Data type Key Required Determined by the minimal cardinality Remarks
35
Column Name Data Type Key Required Remarks EmployeeID Name
IST210 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column Name Data Type Key Required Remarks EmployeeID Name OfficeNumber OfficePhone LockerID LOCKER table Column Name Data Type Key Required Remarks LockerID LockerRoom LockerSize
36
Determine the data types based on the type of these attributes
IST210 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column Name Data Type EmployeeID int Name char(50) OfficeNumber char(20) OfficePhone char(12) LockerID Data Type Determine the data types based on the type of these attributes Data types must be the types defined in SQL 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 LOCKER table Column Name Data Type LockerID int LockerRoom char(10) LockerSize float
37
Specify primary key and foreign key(s) in the table
IST210 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column Name Key EmployeeID Primary key Name OfficeNumber OfficePhone LockerID Foreign key Key Specify primary key and foreign key(s) in the table LOCKER table Column Name Data Type LockerID Primary key LockerRoom LockerSize
38
Primary key must be required
IST210 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column Name Required EmployeeID Yes Name OfficeNumber No OfficePhone LockerID 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 LOCKER table Column Name Required LockerID Yes LockerRoom LockerSize
39
Surrogate key: initial value = 1 Increment = 1 Name OfficeNumber
IST210 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table Column Name Remarks EmployeeID Surrogate key: initial value = 1 Increment = 1 Name OfficeNumber OfficePhone Format: ###-###-#### LockerID Reference: 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 LOCKER table Column Name Remarks LockerID Surrogate key: initial value = 1 Increment = 1 LockerRoom LockerSize Default value: 10
40
Now we complete converting an E-R diagram to tables
IST210 EMPLOYEE(EmployeeID, Name, OfficeNumber, OfficePhone, LockerID) LOCKER(LockerID, LockerRoom, LockerSize) EMPLOYEE table LOCKER table Column Name Data Type Key Required Remarks EmployeeID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 Name char(50) OfficeNumber char(20) No OfficePhone char(12) Format: ###-###-#### LockerID Foreign key Reference: LOCKER Column Name Data Type Key Required Remarks LockerID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 LockerRoom char(10) LockerSize float Default value: 10 Now we complete converting an E-R diagram to tables Next, we can use SQL to create a real database
41
In-class Exercise 1 PARKING(SpotID, Location, MonthlyCost)
IST210 In-class Exercise 1 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID)
42
PARKING(SpotID, Location, MonthlyCost)
IST210 In-class Exercise 1 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID) PARKING table Column Name Data Type Key Required Remarks SpotID Location MonthlyCost USER table Column Name Data Type Key Required Remarks UserID Name SpotID
43
PARKING(SpotID, Location, MonthlyCost)
IST210 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID) PARKING table Column Name Data Type Key Required Remarks SpotID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 Location char(50) MonthlyCost Default: 50 USER table Column Name Data Type Key Required Remarks UserID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 Name char(100) char(50) No SpotID Foreign key Reference: PARKING
44
Notes: See the red parts
IST210 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID) PARKING table Column Name Data Type Key Required Remarks SpotID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 Location char(50) MonthlyCost Default: 50 USER table Column Name Data Type Key Required Remarks UserID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 Name char(100) char(50) No SpotID Foreign key Reference: PARKING Notes: See the red parts Char is not a data type, char(50) is a data type. If you use char, you need to specify the max length When you can use other data types, do not use char. For example, it is better to use int for MonthlyCost instead of using char
45
In-class Exercise 2 Transform this diagram into tables
IST210 In-class Exercise 2 Transform this diagram into tables BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent)
46
In-class Exercise 2 BUILDING(BuildingName, Address)
IST210 BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent) BUILDING table Column Name Data Type Key Required Remarks BuildingName Address APARTMENT table Column Name Data Type Key Required Remarks BuildingName ApartmentID NumberOfBed NumberOfBath Rent
47
BUILDING(BuildingName, Address)
IST210 BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent) BUILDING table Column Name Data Type Key Required Remarks BuildingName Char(20) Primary key Yes Address Char(100) Format: street, city, state, zip code APARTMENT table Column Name Data Type Key Required Remarks BuildingName Char(20) Primary key, foreign key Yes Reference: BUILDING ApartmentID int Primary key NumberOfBed float NumberOfBath Rent
48
IST210 BUILDING table Column Name Data Type Key Required Remarks BuildingName Char(20) Primary key Yes Address Char(100) Format: street, city, state, zip code APARTMENT table Column Name Data Type Key Required Remarks BuildingName Char(20) Primary key, foreign key Yes Reference: BUILDING ApartmentID int Primary key NumberOfBed float NumberOfBath Rent Notes: ApartmentID should NOT be a surrogate key. In real scenarios, we are using some meaningful ApartmentIDs, such as 100 or 201 instead of a meaningless system-generated id. Use float for #ofBed, #ofBath, and Rent. Because #ofBath could be 1.5 and rent could be $890.50, which are not integers.
49
In-class Exercise 3 Transform this diagram into tables
IST210 In-class Exercise 3 Transform this diagram into tables STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID)
50
In-class Exercise 3 STUDENT(StudentID, StudentName, Email)
IST210 STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID) STUDENT table Column Name Data Type Key Required Remarks StudentID StudentName COURSE table Column Name Data Type Key Required Remarks CourseID CourseName Instructor REGISTRATION table Column Name Data Type Key Required Remarks StudentID CourseID
51
STUDENT(StudentID, StudentName, Email)
IST210 STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID) STUDENT table Column Name Data Type Key Required Remarks StudentID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 StudentName Char(100) Char(50) No COURSE table Column Name Data Type Key Required Remarks CourseID Char(20) Primary key Yes Format: DepartmentName + CourseNumber CourseName Char(50) Instructor Char(100) No REGISTRATION table Column Name Data Type Key Required Remarks StudentID int Primary key, foreign key Yes Reference: STUDENT CourseID Char(20) Reference: COURSE
52
IST210 STUDENT table Column Name Data Type Key Required Remarks StudentID int Primary key Yes Surrogate key: initial value = 1 Increment = 1 StudentName Char(100) Char(50) No COURSE table Column Name Data Type Key Required Remarks CourseID Char(20) Primary key Yes Format: DepartmentName + CourseNumber CourseName Char(50) Instructor Char(100) No REGISTRATION table Column Name Data Type Key Required Remarks StudentID int Primary key, foreign key Yes Reference: STUDENT CourseID Char(20) Reference: COURSE Notes: A CourseID should not be integer, for example, “IST210” is a courseID. CourseName for IST210 is “Organization of the data”.
53
E-R Diagram Tables Entity Table Entity name Table name
IST210 E-R Diagram Tables Entity Table Entity name Table name Identifier Primary key Attributes Attributes Relationship Foreign key or a new table Specify the attribute properties
54
IST210 Attendance check?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.