The Relational Model: February 6, 2014
The Relational Model: February 6, 2014
"
February 6, 2014 "
Administrivia "
! Announcements"
-! PS 2 will be out this evening"
! Reading assignment"
-!
Chapter 3"
! Today"
-!
! Acknowledgement"
-! Some slide content courtesy of Ramakrishnan and Gehrke"
! Implementations of the relational model use the SQL data denition and manipulation languages" ! A table in an SQL DB schema corresponds to a predicate variable; the contents of a table to a relation; constraints and queries correspond to predicates"
5
Relation schema: species name of relation, plus name and domain of each column, e.g.,"
!
Students(sid: string, name: string, login: string, age: integer, gpa: real) "
! Can think of a relation as a set of rows or tuples (i.e., all rows are distinct) [not a bag of rows]"
! Cardinality = 3, degree = 5, all rows distinct" ! Do all columns in a relation instance have to be distinct?"
The key: precise semantics for relational queries" Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change"
SQL-86" SQL-89 (minor revision)" SQL-92 (major revision)" SQL-99 (major extensions)" SQL-03 (collection of extensions to SQL-99, current standard)" SQL-06 (SQL and XML combination)" SQL-08 (minor revision on ORDER BY and a few more)"
10
sid
name
login jones@cs
53666 Jones
S.login
11
Enrolled
sid 53831 53831 53650 53666 cid grade Carnatic101 C Reggae203 B Topology112 A History105 B
Students sid name login 53666 Jones jones@cs 53688 Smith smith@eecs 53650 Smith smith@math
age 18 18 19
We get:"
S.name Smith E.cid Topology112
12
! As another example, the Enrolled table holds information about courses that students take"
13
Students
integer
! The schema of Students is altered by adding a new eld" ! Every tuple in the current instance is extended with a null value in the new eld"
14
! Can delete all tuples satisfying some condition (e.g., name = Smith):"
DELETE FROM Students S WHERE S.name =
Smith
ICs are part of schema" ICs are specied when schema is dened" ICs are checked when relations are modied"
! If the DBMS checks ICs, stored data is more faithful to realworld meaning"
-!
! ! ! !
E.g., sid is a key for Students" What about name?" The set {sid, gpa} is a superkey" Every relation is guaranteed to have a key"
17
18
Enrolled(sid: string, cid: string, grade: string)" If all foreign key constraints are enforced, referential integrity is achieved, e.g., no dangling references" Can you name a data model without referential integrity? "
! Links in HTML!"
19
Enrolled
sid 53666 53666 53650 53666
Students
age 18 18 19
Also delete all Enrolled tuples that refer to it, " Disallow deletion of a Students tuple that is referred to by another, " Set sid in Enrolled tuples that refer to it to a default sid, or" (In SQL, also: set sid in Enrolled tuples that refer to it to a special value null, denoting unknown or inapplicable)"
-!
Default is NO ACTION (delete/update is rejected)" CASCADE (also delete all tuples that refer to deleted tuple)" SET NULL / SET DEFAULT (sets foreign key value of referencing tuple)"
CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT )
22
23
An IC is a statement about all possible instances!" From example, we know name is not a key, but the assertion that sid is a key is given to us"
24
-! Assertions"
! Involve multiple tables" ! Checked whenever any of the tables is modied"
25
26
ssn
lot CREATE TABLE Employees ( ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) - Domain of each attribute - Primary key
28
Employees
-!
CREATE TABLE Works_In ( ssn CHAR(11) NOT NULL, did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)
29
! Each dept has at most one manager, according to the key constraint on Manages
dname budget
Employees
Manages
Departments
-!
Note that did is the key! Not did and ssn together since each dept has at most one manager Separate tables for Employees and Departments
CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Dept_Mgr ( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)
! Since each department has a unique manager, we could instead combine Manages and Departments into one
31
The participation of Departments in Manages is said to be total. (cf. partial)" Participation of an entity set in a relationship set" Every did value in Departments must appear in a row of the Manages table (with a non-null ssn value!)" since lot did Manages
WorksIn
since
WorksIn: Thick: total" -! Each employee works in at least one dept" -! Each dept has at least one employee"
32
33
Owner entity set and weak entity set must participate in a 1-to-many relationship set (1 owner, 1+ weak entities)" Weak entity set must have total participation in this identifying relationship set" Primary key plus partial key together can uniquely identify a weak entity, e.g., ssn and pname together below" name
ssn
lot
cost
pname
age
Employees
Policy
Dependents
- Dependents is a weak entity set" -! Policy is its identifying relationship set" -! Employees is its identifying owner"
34
When the owner entity is deleted, all owned weak entities must also be deleted CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)
! As in Java, attributes are inherited" ! If we declare A ISA B, every A entity is also considered to be a B entity"
-! Overlap constraints: Can Jim be an HourlyEmps as well as a ContractEmps entity? (allowed/disallowed)" -! Covering constraints: Does every Employees entity also have to be an HourlyEmps or a ContractEmps entity? (yes/no) ! -! Reasons for using ISA:"
! To add descriptive attributes specic to a subclass" ! To identify entities that participate in a relationship"
ssn
lot
Employees
hours_worked
ISA
HourlyEmps
ContractEmps
hourly_wages
contractid
36
HourlyEmps: ssn, name, lot, hourlyWages, hoursWorked Each employee must be in one of these two subclasses No way to represent employees who are neither hourly employees nor contract employees
37
Employees
Dependents
Dependents
constraints
Views
! A view is a relation: we store a definition, and compute the tuples when needed
CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21
! Some views may be updated ! Views can be dropped using the DROP VIEW command
-!
DROP TABLE command has options (RESTRICT or CASCADE) to let the user specify this
40
Given YoungActiveStudents, but not Students or Enrolled, we can find students who are enrolled, but not the cids of the courses they are enrolled in
41
Two important ICs: primary and foreign keys In addition, we always have domain constraints
! Powerful and natural query languages exist ! Rules to translate ER to relational model ! Views as external schema
42
Next . . .
Chapter 4 Relational Algebra
43