Lab Exercise This Week PHP Basics See last Friday’s slides for requirements Make sure you show the final results to TA to get credit 1IST210
Chapter 2. The Relational Model IST2102
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
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
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) IST2105
Relation A relation is a two-dimensional table that has specific characteristics The table dimensions, like a matrix, consist of rows and columns IST2106
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 IST2107
Exercise: Relation or Non-Relation? 2-8 EmployeeNumberFirstNameLastName 100MaryAbernathy 101JerryCadley 104AlexCopley 107MeganJackson
Exercise: Relation or Non-Relation? 2-9 EmployeeNumberPhoneLastName , Abernathy Cadley Copley Jackson Cells of the table hold multiple values
Exercise: Relation or Non-Relation? 2-10 EmployeeNumberPhoneLastName Abernathy Cadley Copley Abernathy Jackson No two rows may be identical
Presenting Relation Structure IST210 Column 1 Column 2 …Column n RELATION_NAME(Column1, Column 2, …, Column n) RELATION_NAME 11 STUDENT(StudentID, FirstName, LastName, DOB) STUDENT Original Table Relation Representation From now on, we will frequently use this presentation for relations
Terminology IST210 Synonyms… TableRowColumn FileRecordField RelationTupleAttribute
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 IST21013
Example: 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 key is one (or more) columns of a relation that is (are) used to uniquely identify a row.
Candidate Key IST21015 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
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 IST21016 Primary key: StudentID
Primary Key: Discussion IST21017 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
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 IST21018
How to Choose a Primary Key? IST21019 What if none of existing attributes is appropriate? Answer: artificially create a new attribute Candidate keys: CustomerName? HomeAddress? DOB? Primary key?
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 IST21020
Surrogate Key Examples Penn State database – StudentID Membership database – MembershipID Online shopping – OrderNumber IST21021
Review Key: StudentID, (StudentID, FirstName), … Candidate key: StudentID Primary key: (StudentID, FirstName, LastName, DOB) Surrogate key: StudentID IST21022
True/False Quiz 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. IST21023
True/False Quiz 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?