Chapter 3
Can think of a relation as a set of rows or tuples (i.e., all rows are distinct).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3
Cardinality = 3, degree = 5, all rows distinct Do all columns in a relation instance have to be distinct?
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4
we get:
S.name E.cid Smith Topology112
8
Destroys the relation Students. The schema information and the tuples are deleted.
ALTER TABLE Students ADD COLUMN firstYear: integer
The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10
Can delete all tuples satisfying some condition (e.g., name = Smith):
DELETE FROM Students S WHERE S.name = Smith
If the DBMS checks ICs, stored data is more faithful to real-world meaning.
Avoids data entry errors, too!
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12
13
E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14
15
16
Enrolled
sid 53666 53666 53650 53666 cid grade Carnatic101 C Reggae203 B Topology112 A History105 B
Students
sid 53666 53688 53650 name login Jones jones@cs Smith smith@eecs Smith smith@math age 18 18 19 gpa 3.4 3.2 3.8
17
Key and foreign key ICs are the most common; more general ICs supported too.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20
ssn
name
lot
Employees
21
22
23
Each dept has at most one manager, according to the key constraint on Manages.
dname budget
Employees
Manages
Departments
did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)
25
Works_In
26
Employees
Policy
Dependents
28
Hourly_Emps
Contract_Emps
Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed) Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)
30
pname
age
Dependents
Bad design
pname
age
Dependents
Beneficiary
Better design
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Policies cost
32
policyid
Participation CREATE TABLE Dependents ( constraints lead to pname CHAR(20), NOT NULL age INTEGER, constraints. policyid INTEGER, What if Policies is PRIMARY KEY (pname, policyid). a weak entity set? FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 33
Views
A view is just a relation, but we store a definition, rather than a set of tuples.
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
35
Powerful and natural query languages exist. Rules to translate ER to relational model
36