Database Design I (In-Class Exercise Answer) IST 210: Organization of Data IST2101
In-class Exercise 1 IST2102 Transform this diagram into tables UserID Name SpotID Location MonthlyCost USER PARKING PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID)
In-class Exercise 1: Notes IST2103 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.
In-class Exercise 2 IST2104 Transform this diagram into tables BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent)
In-class Exercise 2: Notes IST2105 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
In-class Exercise 3 IST2106 CourseID CourseName Instructor StudentID StudentName COURSE STUDENT Transform this diagram into tables STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID)
In-class Exercise 3: Notes IST2107 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.
In-class Exercise 4 IST2108 USER(UserID, Name, ) COMMENT(CommentID, Rating, Comment, UserID, MovieID) MOVIE(MovieID, MovieName, Director, Year) MovieID MovieName Director Year UserID Name MOVIE USER Transform this diagram into tables CommentID Rating Comment COMMENT
In-class Exercise 1 IST2109 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID)
IST21010 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID) PARKING table Column NameData Type KeyRequire d Remarks UserIDintPrimary keyYesSurrogate key: initial value = 1 Increment = 1 Namechar(100)Yes char(50)No SpotIDintForeign keyYesReference: PARKING USER table Column NameData Type KeyRequire d Remarks SpotIDintPrimary keyYesSurrogate key: initial value = 1 Increment = 1 Locationchar(50)Yes MonthlyCostintYesDefault: 50
IST21011 PARKING table Column NameData Type KeyRequire d Remarks UserIDintPrimary keyYesSurrogate key: initial value = 1 Increment = 1 Namechar(100)Yes char(50)No SpotIDintForeign keyYesReference: PARKING USER table Column NameData Type KeyRequire d Remarks SpotIDintPrimary keyYesSurrogate key: initial value = 1 Increment = 1 Locationchar(50)Yes MonthlyCostintYesDefault: 50 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 PARKING(SpotID, Location, MonthlyCost) USER(UserID, Name, , SpotID)
In-class Exercise 2 IST21012 Transform this diagram into tables BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent)
IST21013 BUILDING table Column NameData Type KeyRequiredRemarks BuildingNameChar(20)Primary key, foreign keyYesReference: BUILDING ApartmentIDintPrimary keyYes NumberOfBedfloatYes NumberOfBathfloatYes RentfloatYes APARTMENT table Column NameData Type KeyRequiredRemarks BuildingNameChar(20)Primary keyYes AddressChar(100)YesFormat: street, city, state, zip code BUILDING(BuildingName, Address) APARTMENT(BuildingName, ApartmentID, NumberOfBedrooms, NumberofBaths, Rent)
IST21014 BUILDING table Column NameData Type KeyRequiredRemarks BuildingNameChar(20)Primary key, foreign keyYesReference: BUILDING ApartmentIDintPrimary keyYes NumberOfBedfloatYes NumberOfBathfloatYes RentfloatYes APARTMENT table Column NameData Type KeyRequiredRemarks BuildingNameChar(20)Primary keyYes AddressChar(100)YesFormat: street, city, state, zip code 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.
In-class Exercise 3 IST21015 Transform this diagram into tables STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID)
IST21016 STUDENT(StudentID, StudentName, ) COURSE(CourseID, CourseName, Instructor) REGISTRATION(StudentID, CourseID) STUDENT table Column NameData Type KeyRequiredRemarks CourseIDChar(20)Primary keyYesFormat: DepartmentName + CourseNumber CourseNameChar(50)Yes InstructorChar(100)No COURSE table Column NameData Type KeyRequiredRemarks StudentIDintPrimary keyYesSurrogate key: initial value = 1 Increment = 1 StudentNameChar(100)Yes Char(50)No Column NameData Type KeyRequiredRemarks StudentIDintPrimary key, foreign keyYesReference: STUDENT CourseIDChar(20)Primary key, foreign keyYesReference: COURSE REGISTRATION table
IST21017 STUDENT table Column NameData Type KeyRequiredRemarks CourseIDChar(20)Primary keyYesFormat: DepartmentName + CourseNumber CourseNameChar(50)Yes InstructorChar(100)No COURSE table Column NameData Type KeyRequiredRemarks StudentIDintPrimary keyYesSurrogate key: initial value = 1 Increment = 1 StudentNameChar(100)Yes Char(50)No Column NameData Type KeyRequiredRemarks StudentIDintPrimary key, foreign keyYesReference: STUDENT CourseIDChar(20)Primary key, foreign keyYesReference: COURSE REGISTRATION table Notes: A CourseID should not be integer, for example, “IST210” is a courseID. CourseName for IST210 is “Organization of the data”.