Chapter 3 The Relational Data Model McGraw-Hill/Irwin Copyright © 2007 by The McGraw-Hill Companies, Inc. All rights reserved. Outline Relational model basics Integrity rules Rules about referenced rows Relational Algebra 3-2 Tables Relational database is a collection of tables Heading: table name and column names Body: rows, occurrences of data Student StdSSN 123-45-6789 124-56-7890 234-56-7890 StdLastName WELLS NORBERT KENDALL StdMajor IS FIN ACCT StdClass FR JR JR StdGPA 3.00 2.70 3.50 3-3 CREATE TABLE Statement CREATE TABLE Student ( StdSSN CHAR(11), StdFirstName VARCHAR(50), StdLastName VARCHAR(50), StdCity VARCHAR(50), StdState CHAR(2), StdZip CHAR(10), StdMajor CHAR(6), StdClass CHAR(6), StdGPA DECIMAL(3,2) ) 3-4 Common Data Types CHAR(L) VARCHAR(L) INTEGER FLOAT(P) Date/Time: DATE, TIME, TIMESTAMP DECIMAL(W, R) BOOLEAN 3-5 Relationships Offering Student StdSSN 123-45-6789 124-56-7890 234-56-7890 OfferNo CourseNo 1234 IS320 4321 IS320 StdLastName WELLS KENDALL NORBERT Enrollment StdSSN 123-45-6789 OfferNo 1234 234-56-7890 1234 123-45-6789 4321 124-56-7890 4321 3-6 Alternative Terminology Table-oriented Set-oriented Recordoriented Table Relation Record-type, file Row Tuple Record Column Attribute Field 3-7 Integrity Rules Entity integrity: primary keys Each table has column(s) with unique values Ensures entities are traceable Referential integrity: foreign keys Values of a column in one table match values in a source table Ensures valid references among tables 3-8 Formal Definitions I Superkey: column(s) with unique values Candidate key: minimal superkey Null value: special value meaning value unknown or inapplicable Primary key: a designated candidate key; cannot contain null values Foreign key: column(s) whose values must match the values in a candidate key of another table 3-9 Formal Definitions II Entity integrity No two rows with the same primary key value No null values in any part of a primary key Referential integrity Foreign keys must match candidate key of source table Foreign keys can be null in some cases In SQL, foreign keys associated with primary keys 3-10 Course Table Example CREATE TABLE Course ( CourseNo CHAR(6), CrsDesc VARCHAR(250), CrsUnits SMALLINT, CONSTRAINT PKCourse PRIMARY KEY(CourseNo), CONSTRAINT UniqueCrsDesc UNIQUE (CrsDesc) ) 3-11 Enrollment Table Example CREATE TABLE Enrollment ( OfferNo INTEGER, StdSSN CHAR(11), EnrGrade DECIMAL(3,2), CONSTRAINT PKEnrollment PRIMARY KEY (OfferNo, StdSSN), CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo) REFERENCES Offering, CONSTRAINT FKStdSSN FOREIGN KEY (StdSSN) REFERENCES Student ) 3-12 Offering Table Example CREATE TABLE Offering ( OfferNo INTEGER, CourseNo CHAR(6) CONSTRAINT OffCourseNoRequired NOT NULL, OffLocation VARCHAR(50), OffDays CHAR(6), OffTerm CHAR(6) CONSTRAINT OffTermRequired NOT NULL, OffYear INTEGER CONSTRAINT OffYearRequired NOT NULL, FacSSN CHAR(11), OffTime DATE, CONSTRAINT PKOffering PRIMARY KEY (OfferNo), CONSTRAINT FKCourseNo FOREIGN KEY (CourseNo) REFERENCES Course, CONSTRAINT FKFacSSN FOREIGN KEY (FacSSN) REFERENCES Faculty ) 3-13 Self-Referencing Relationships Foreign key that references the same table Represents relationships among members of the same set Not common but important in specialized situations 3-14 Faculty Data FacSSN 098-76-5432 543-21-0987 654-32-1098 765-43-2109 876-54-3210 987-65-4321 FacFirstName LEONARD VICTORIA LEONARD NICKI CRISTOPHER JULIA FacLastName VINCE EMMANUEL FIBON MACON COLAN MILLS FacRank FacSalary FacSupervisor ASST $35,000 654-32-1098 PROF $120,000 ASSC $70,000 543-21-0987 PROF $65,000 ASST $40,000 654-32-1098 ASSC $75,000 765-43-2109 3-15 Hierarchical Data Display 543-21-0987 Victoria Emmanual 654-32-1098 Leonard Fibon 098-78-5432 Leonard Vince 876-54-3210 Cristopher Colan 3-16 Faculty Table Definition CREATE TABLE Faculty ( FacSSN CHAR(11), FacFirstName VARCHAR(50) NOT NULL, FacLastName VARCHAR(50) NOT NULL, FacCity VARCHAR(50) NOT NULL, FacState CHAR(2) NOT NULL, FacZipCode CHAR(10)NOT NULL, FacHireDate DATE, FacDept CHAR(6), FacSupervisor CHAR(11), CONSTRAINT PKFaculty PRIMARY KEY (FacSSN), CONSTRAINT FKFacSupervisor FOREIGN KEY (FacSupervisor) REFERENCES Faculty ) 3-17 Relationship Window with 1-M Relationships 3-18 M-N Relationships Rows of each table are related to multiple rows of the other table Not directly represented in the relational model Use two 1-M relationships and an associative table 3-19 Referenced Rows Referenced row Foreign keys reference rows in the associated primary key table Enrollment rows refer to Student and Offering Actions on referenced rows Delete a referenced row Change the primary key of a referenced row Referential integrity should not be violated 3-20 Possible Actions Restrict: do not permit action on the referenced row Cascade: perform action on related rows Nullify: only valid if foreign keys accept null values Default: set foreign keys to a default value 3-21 SQL Syntax for Actions CREATE TABLE Enrollment ( OfferNo INTEGER NOT NULL, StdSSN CHAR(11) NOT NULL, EnrGrade DECIMAL(3,2), CONSTRAINT PKEnrollment PRIMARY KEY(OfferNo, StdSSN), CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo) REFERENCES Offering ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT FKStdSSN FOREIGN KEY (StdSSN) REFERENCES Student ON DELETE RESTRICT ON UPDATE CASCADE ) 3-22 Relational Algebra Overview Collection of table operators Transform one or two tables into a new table Understand operators in isolation Classification Table specific operators Traditional set operators Advanced operators 3-23 Subset Operators Restrict Project 3-24 Subset Operator Notes Restrict Logical expression as input Example: OffDays = 'MW' AND OffTerm = 'SPRING' AND OffYear = 2006 Project List of columns is input Duplicate rows eliminated if present Often used together 3-25 Extended Cross Product Building block for join operator Builds a table consisting of all combinations of rows from each of the two input tables Produces excessive data Subset of cross product is useful (join) 3-26 Extended Cross Product Example Faculty FacSSN 111-11-1111 222-22-2222 333-33-3333 Student StdSSN 111-11-1111 444-44-4444 555-55-5555 Faculty PRODUCT Student FacSSN 111-11-1111 111-11-1111 111-11-1111 222-22-2222 222-22-2222 222-22-2222 333-33-3333 333-33-3333 333-33-3333 StdSSN 111-11-1111 444-44-4444 555-55-5555 111-11-1111 444-44-4444 555-55-5555 111-11-1111 444-44-4444 555-55-5555 3-27 Join Operator Most databases have many tables Combine tables using the join operator Specify matching condition Can be any comparison but usually = PK = FK most common join condition Relationship diagram useful when combining tables 3-28 Natural Join Operator Most common join operator Requirements Equality matching condition Matching columns with the same unqualified names Remove one join column in the result Usually performed on PK-FK join columns 3-29 Natural Join Example Faculty FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara Offering OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111 Natural Join of Offering and Faculty FacSSN 111-11-1111 FacName OfferNo joe 1111 222-22-2222 sue 2222 111-11-1111 joe 3333 3-30 Visual Formulation of Join 3-31 Outer Join Overview Join excludes non matching rows Preserving non matching rows is important in some business situations Outer join variations Full outer join One-sided outer join 3-32 Outer Join Operators Full outer join Left Outer Join Unmatched rows of the left table Join Right Outer Join Matched rows using the join condition Unmatched rows of the right table 3-33 Full Outer Join Example Faculty FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara Offering Offerno 1111 2222 3333 4444 FacSSN 111-11-1111 222-22-2222 111-11-1111 Outer Join of Offering and Faculty FacSSN 111-11-1111 FacName joe OfferNo 1111 222-22-2222 sue 2222 111-11-1111 joe 3333 333-33-3333 sara 4444 3-34 Visual Formulation of Outer Join 3-35 Traditional Set Operators A UNION B A INTERSECT B A MINUS B 3-36 Union Compatibility Requirement for the traditional set operators Strong requirement Same number of columns Each corresponding column is compatible Positional correspondence Apply to similar tables by removing columns first 3-37 Summarize Operator Decision-making operator Compresses groups of rows into calculated values Simple statistical (aggregate) functions Not part of original relational algebra 3-38 Summarize Example Enrollment StdSSN 111-11-1111 111-11-1111 111-11-1111 222-22-2222 222-22-2222 333-33-3333 OfferNo 1111 2222 3333 1111 3333 1111 EnrGrade 3.8 3.0 3.4 3.5 3.1 3.0 SUMMARIZE Enrollment ADD AVG(EnrGrade) GROUP BY StdSSN StdSSN AVG(EnrGrade) 111-11-1111 3.4 222-22-2222 3.3 333-33-3333 3.0 3-39 Divide Operator Match on a subset of values Suppliers who supply all parts Faculty who teach every IS course Specialized operator Typically applied to associative tables representing M-N relationships 3-40 Division Example SuppPart SuppNo PartNo s3 p1 s3 p2 s3 p3 s0 p1 s1 p2 Part SuppPart DIVIDEBY Part PartNo p1 p2 SuppNo s3 s3 {p1, p2, p3} contains {p1, p2} 3-41 Relational Algebra Summary Operator Restrict (Select) Project Product Union Intersect Difference Join Outer Join Divide Summarize Meaning Extracts rows that satisfy a specified condition Extracts specified columns. Builds a table from two tables consisting of all possible combinations of rows, one from each of the two tables. Builds a table consisting of all rows appearing in either of two tables Builds a table consisting of all rows appearing in both of two specified tables Builds a table consisting of all rows appearing in the first table but not in the second table Extracts rows from a product of two tables such that two input rows contributing to any output row satisfy some specified condition. Extracts the matching rows (the join part) of two tables and the “unmatched” rows from both tables. Builds a table consisting of all values of one column of a binary (2 column) table that match (in the other column) all values in a unary (1 column) table. Organizes a table on specified grouping columns. Specified aggregate computations are made on each value of the grouping columns. 3-42 Summary Relational model is commercially dominant Learn primary keys, data types, and foreign keys Visualize relationships Understanding existing databases is crucial to query formulation 3-43 Questions & Discussion 3-44