Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2. The Relational Model IST2101. Chapter 1 Review Potential problems with Lists – Deletion – Update – Insertion Avoid these problems using a relational.

Similar presentations


Presentation on theme: "Chapter 2. The Relational Model IST2101. Chapter 1 Review Potential problems with Lists – Deletion – Update – Insertion Avoid these problems using a relational."— Presentation transcript:

1 Chapter 2. The Relational Model IST2101

2 Chapter 1 Review Potential problems with Lists – Deletion – Update – Insertion Avoid these problems using a relational database: – Break a list into several tables, one table for each theme – Tables can be joined together using the value of the data So far, we are pretty vague on what we mean by “table”, “theme” and “value of the data”…… StudentCourse

3 Chapter 2 Objective Learn the concept of the relational model Learn the meaning and importance of keys, foreign keys, and related terminology Learn the meaning of functional dependencies Learn to apply a process for normalizing relations

4 Entity An entity is something of importance to a user that needs to be represented in a database An entity represents one theme or topic – An object (e.g., book) with various characteristics (e.g., authors, publisher, ISSN) IST2104

5 Relation A relation is a two-dimensional table that has specific characteristics The table dimensions, like a matrix, consist of rows and columns IST2105

6 Characteristics of a Relation Columns contain data about attributes of the entity class – Each column has a unique name – All entries in a column are the same kind Rows contain data about an entity instance – Cells of the table hold a single value – No two rows may be identical The order of the columns is unimportant The order of the rows is unimportant IST2106

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

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

9 Exercise: Relation or Non-Relation? 2-9 EmployeeNumberFirstNameLastName 100MaryAbernathy 101JerryCadley 104AlexCopley 107MeganJackson

10 Presenting Relation Structure IST210 Column 1 Column 2 …Column n RELATION_NAME(Column1, Column 2, …, Column n) RELATION_NAME 10 STUDENT(StudentID, FirstName, LastName, DOB) STUDENT Original Table Relation Representation From now on, we will frequently use this presentation for relations

11 Terminology IST210 Synonyms… TableRowColumn FileRecordField RelationTupleAttribute

12 Key A key is one (or more) columns of a relation that is (are) used to uniquely identify a row – E.g.: OrderID is a key to uniquely identify an order – A composite key is a key that contains two or more attributes E.g.: (BuildingNumber, ApartmentNumber) is a composite key to uniquely identify an apartment IST21012

13 Example: Key IST21013 What attribute(s) form a key? StudentID: FirstName: (FirstName, LastName): (FirstName, LastName, DOB): (StudentID, FirstName): (StudentID, FirstName, LastName, DOB): yes no yes (in this table), but no (if there are thousands of records, there could be students with same first name and last name) yes (in this table), but no (if more records) yes, but … A key is one (or more) columns of a relation that is (are) used to uniquely identify a row.

14 Candidate Key IST21014 What attribute(s) form a key? StudentID: FirstName: (FirstName, LastName): (FirstName, LastName, DOB): (StudentID, FirstName): (StudentID, FirstName, LastName, DOB): yes no yes (in this table), but no (if there are thousands of records, there could be students with same first name and last name) yes (in this table), but no (if more records) yes, but 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 usually don’t consider it as a candidate key not a candidate key, a candidate key

15 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 IST21015 Primary key: StudentID

16 Primary Key: Discussion IST21016 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) SSN: Candidate key? Good to be a primary key? STUDENT(StudentID, FirstName, LastName, DOB, HomeAddress) HomeAddress: Candidate key? 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

17 Presenting Primary Key Single 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) REGISTRATION(StudentID, CourseID, RegistrationDate) – The underline of StudentID and CourseID indicates (StudentID, CourseID) is the composite primary key of this relation IST21017

18 How to Choose a Primary Key? IST21018 What if none of existing attributes is appropriate? Answer: artificially create a new attribute Candidate keys: CustomerName? HomeAddress? DOB? Primary key?

19 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 IST21019

20 Surrogate Key Examples Penn State database – StudentID Membership database – MembershipID Online shopping – OrderNumber IST21020

21 Review Key: StudentID, (StudentID, FirstName), … Candidate key: StudentID Primary key: (StudentID, FirstName, LastName, DOB) Surrogate key: StudentID IST21021

22 Exercise: True/False Candidate keys may or may not be unique. The primary key is used to identify unique rows in a relation. Surrogate key values have no meaning to the users. Surrogate key is not primary key. IST21022

23 Exercise: True/False Suppose we have the following table: BOOK(BookID, Title, Publisher, Year) Q1. Title is a key in BOOK? Q2. (BookID, Title) is a key? Q3. (BookID, Title) is a candidate key? Q4. BookID is a surrogate key added to serve as a primary key?

24 And one more question… What’s the primary key of this table? EmployeeIDEmployeeNameSkillWork Location 101JonesTyping114 Main Street 101JonesShorthand114 Main Street 101JonesWhittling114 Main Street 102BravoLight Cleaning73 Industrial Way 103EllisAlchemy73 Industrial Way 103EllisFlying73 Industrial Way 104HarrisonLight Cleaning73 Industrial Way


Download ppt "Chapter 2. The Relational Model IST2101. Chapter 1 Review Potential problems with Lists – Deletion – Update – Insertion Avoid these problems using a relational."

Similar presentations


Ads by Google