Presentation is loading. Please wait.

Presentation is loading. Please wait.

THE RELATIONAL MODEL I IST 210: Organization of Data IST210 1.

Similar presentations


Presentation on theme: "THE RELATIONAL MODEL I IST 210: Organization of Data IST210 1."— Presentation transcript:

1 THE RELATIONAL MODEL I IST 210: Organization of Data IST210 1

2 Chapter Objectives Learn the concept of the relational model Understand how relations differ from non-relational tables Learn basic relational terminology Learn the meaning and importance of keys, foreign keys, and related terminology Understand how foreign keys represent relationships Learn the purpose and use of surrogate keys Learn the meaning of functional dependencies Learn to apply a process for normalizing relations IST210 2

3 Characteristics of a Relation A relation is a two-dimensional table (row and column) that has specific characteristics Columns contain data about attributes of the entity Each column has a unique name All entries in a column are the same kind The order of the columns is unimportant Rows contain data about entity instances Cells of the table hold a single value No two rows may be identical The order of the rows is unimportant IST210 3 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug, 1,1989 9123453JoshCohenAug. 1,1989

4 Presenting Relation Structure IST210 4 Column 1Column 2 …Column n RELATION_NAME(Column1, Column 2, …, Column n) RELATION_NAME STUDENT(StudentID, FirstName, LastName, DOB) STUDENT Original Table Relation Representation From now on, we will frequently use this representation for relations StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug, 1,1989 9123453JoshCohenAug. 1,1989

5 A Sample Relation EmployeeNumberFirstNameLastName 100MaryAbernathy 101JerryCadley 104AlexCopley 107MeganJackson

6 Relation or Non-Relation? EmployeeNumberPhoneLastName 100335-6421, 454-9744 Abernathy 101215-7789Cadley 104610-9850Copley 107299-9090Jackson Non-relation: Cells of the table hold multiple values

7 Relation or Non-Relation? EmployeeNumberPhoneLastName 100335-6421Abernathy 101215-7789Cadley 104610-9850Copley 100335-6421Abernathy 107299-9090Jackson Non-Relation: No two rows may be identical

8 Terminology IST210 Synonyms… TableRowColumn FileRecordField RelationTupleAttribute

9 Key A (unique) key is one (or more) columns of a relation that is (are) used to uniquely identify a row A composite key is a key that contains two or more attributes IST210 9

10 Example: Key IST210 10 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug, 1,1989 9123453JoshCohenAug. 1,1989 What attribute(s) form a key? StudentID FirstName (FirstName, LastName) (FirstName, DOB) (StudentID, FirstName) (StudentID, FirstName, LastName, DOB)

11 Example: Key IST210 11 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug, 1,1989 9123453JoshCohenAug. 1,1989 StudentID: yes FirstName: no (FirstName, LastName): yes (in this table), but no (if there are thousands of records, there could be students with same first name and last name) (FirstName, DOB): yes (in this table), but no (if more records) (StudentID, FirstName): (StudentID, FirstName, LastName, DOB): yes, but …

12 Candidate Key A candidate key is called “candidate” because it is a candidate to become the primary key A special key If the subset of a key is also a key, we don’t usually consider it as a candidate IST210 12 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug, 1,1989 9123453JoshCohenAug. 1,1989 What attribute(s) form a key? StudentID: yes FirstName: no (FirstName, LastName): yes (in this table), but no (if there are thousands of records, there could be students with same first name and last name) (FirstName, DOB): yes (in this table), but no (if more records) (StudentID, FirstName): yes, but not a candidate key (StudentID, FirstName, LastName, DOB): yes, but not a candidate key

13 Primary Key A primary key is a candidate key chosen to be the main key for the relation A relation can only have one primary key Each candidate key could be chosen as a primary key, but we usually have preferences IST210 13 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug, 1,1989 9123453JoshCohenAug. 1,1989 Primary key: StudentID

14 Primary Key: Discussion IST210 14 Even if HomeAddress could be a candidate key, we still prefer choosing StudentID as the primary key. Because (1)HomeAddress might have duplicate (2)HomeAddress is a string, hard to index and query. StudentID is numeric value STUDENT(StudentID, FirstName, LastName, DOB, SSN) Candidate key: SSN? Good to be a primary key? STUDENT(StudentID, FirstName, LastName, DOB, HomeAddress) Candidate key: HomeAddress? Good to be a primary key? Even if SSN is a candidate key, we still prefer choosing StudentID as the primary key. Because SSN is sensitive information

15 Presenting Primary Key Non-Composite Key RELATION_NAME(Column1, Column 2, …, Column n) Student(StudentID, FirstName, LastName, DOB) The underline of StudentID indicates StudentID is the primary key of this relation Composite Key RELATION_NAME(Column1, Column 2, …, Column n) Student(StudentID, FirstName, LastName, DOB) The underline of FirstName and LastName indicates (FirstName, LastName) is the composite primary key of this relation IST210 15

16 How to Choose a Primary Key? IST210 16 CustomerNameHomeAddressEmail John Smith293 Main Staaa@g.com John Adam10 Green Rdjjj@m.com Jane White111 Universityabc@g.com Josh Cohen12 Beaveracd@psu.edu What if none of existing attributes is appropriate? Answer: artificially create a new attribute Candidate keys: HomeAddress? Email? Primary key?

17 A Surrogate Key A Surrogate Key is a unique numeric value that is added to a relation to serve as the primary key System generated Contains no semantic meaning Surrogate key is very commonly used. A surrogate key is often used to replace a composite primary key or a non- numeric primary key (FirstName, LastName, DOB)  StudentID HomeAddress  CustomerID IST210 17

18 Surrogate Key Examples Penn State database StudentID Membership database Membership ID Online shopping Order number IST210 18

19 Review Key: StudentID, (StudentID, FirstName), … Candidate key: StudentID Primary key: (StudentID, FirstName, LastName, DOB) Surrogate key: StudentID IST210 19 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug. 1,1989 9123453JoshCohenAug. 1,1989

20 Relationships Between Tables A table is related to other tables Shared columns in Chapter 1 IST210 20 StudentIDFirstNameLastNameDOB 9123450JohnSmithJan. 1, 1989 9123451JohnAdamJun. 1, 1988 9123452JaneAdamAug. 1,1989 9123453JoshCohenAug. 1,1989 ClubIDClubNamePresidentStudentID 12Football9123450 13Medical9123453 15Dance9123452 Primary Key StudentID is the primary key in STUDENT table Foreign Key PresentStudentID is the foreign key in CLUB table CLUB(ClubID, ClubName, PresidentStudentID) STUDENT(StudentID, FirstName, LastName, DOB)

21 A Foreign Key To preserve relationships of relations, it is needed to create a foreign key A foreign key is a primary key from one table placed into another table Why? The key is called a foreign key in the relation that receives the key Presenting a foreign key Attributes name in italic RELATION_NAME(Column1, Column 2, …, Column n) IST210 21

22 Foreign Key Example IST210 22 Foreign Key Primary Key PROJECT(ProjID, ProjName, MgrID) MANAGER(MgrID, MgrName, Office) ProjIDProjNameMgrID PROJECT MgrIDMgrNameOffice MANAGER

23 Foreign Key Example IST210 23 Foreign Key Primary Key DEPARTMENT(DeptID, DeptName, Location) EMPLOYEE(EmpID, DeptID, EmpName) DeptIDDeptNam e Location DEPARTMENT EmpIDDeptIDEmpName EMPLOYEE

24 Foreign Key Example IST210 24 STUDENT tableCOURSE table REGISTRATION table STUDENT(StudentID, Name, Department, Email) COURSE(CourseID, Instructor, CourseName, Location) REGISTRATION(StudentID, CourseID) An attribute can be both part of primary key and foreign key!

25 Referential Integrity Every value of a foreign key must match a value of an existing primary key IST210 25 Student IDCourseID 1210 5 2 3 1220 3 10230 10250 250 does not exist in COURSE table! Violate referential integrity!

26 Summary of Keys Key is one (or more) columns of a relation that is (are) used to identify a row Unique key Single key and composite key A unique key (can be)  a candidate key (may be chosen to be)  a primary key A surrogate key: an intentionally created attribute to serve as a primary key A foreign key: link to the primary key in another table IST210 26

27 Review Quiz Q1. Candidate keys could be a non-unique key? Q2. Surrogate key values have no semantic meaning to the users? Q3. A surrogate key can NOT be chosen as a primary key? Q4. A foreign key in one table must be a primary key in another table? IST210 27

28 Review Quiz Suppose we have two tables: BOOK(BookID, Title, PublisherID) PUBLISHER(PublisherID, Name, Location) Q1. Title is a key in BOOK? Q2. (BookID, Title) is a key? Q3. (BookID, Title) is a candidate key? Q4. PublisherID is a foreign key in PUBLISHER? IST210 28

29 Review Quiz Suppose we have two tables: BOOK(BookID, Title, PublisherID) PUBLISHER(PublisherID, Name, Location) Q5. Is following design about primary key and foreign key correct? BOOK(BookID, Title, PublishedYear, PublisherID) PUBLISHER(PublisherID, Name, Location) IST210 29

30 Review Quiz Suppose we have two tables: BOOK(BookID, Title, PublisherID) PUBLISHER(PublisherID, Name, Location) Q6. Is following design about primary key and foreign correct? BOOK(BookID, Title, PublishedYear, PublisherID) PUBLISHER(PublisherID, Name, Location) IST210 30

31 Review Quiz Suppose we have two tables: BOOK(BookID, Title, PublisherID) PUBLISHER(PublisherID, Name, Location) Q7. Is following design about primary key and foreign correct? BOOK(BookID, Title, PublishedYear, PublisherID) PUBLISHER(PublisherID, Name, Location) IST210 31

32 Reminder Homework 1 due tonight 11:59PM Homework P1 due Wed night 11:59PM IST210 32


Download ppt "THE RELATIONAL MODEL I IST 210: Organization of Data IST210 1."

Similar presentations


Ads by Google