Chap 3
Chap 3
Chap 3
Chapter 3
•To find just names and logins, replace the first line:
SELECT S.name, S.login
Translation to
relational model?
Works_In
since
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24
Participation Constraints in SQL
We can capture participation constraints involving one
entity set in a binary relationship, but little else (without
resorting to CHECK constraints).
CREATE TABLE Dept_Mgr(
did INTEGER,
dname CHAR(20),
budget REAL,
ssn CHAR(11) NOT NULL,
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE NO ACTION)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25
Review: Weak Entities
A weak entity can be identified uniquely only by
considering the primary key of another (owner) entity.
Owner entity set and weak entity set must participate in a one-to-
many relationship set (1 owner, many weak entities).
Weak entity set must have total participation in this identifying
relationship set.
name
cost pname age
ssn lot
Employees
As in C++, or other PLs, attributes
hourly_wages hours_worked
are inherited. ISA
If we declare A ISA B, every A contractid
Purchaser
Beneficiary
policyid cost
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 30
Binary vs. Ternary Relationships (Contd.)
CREATE TABLE Policies (
The key constraints
policyid INTEGER,
allow us to combine cost REAL,
Purchaser with
ssn CHAR(11) NOT NULL,
Policies and
PRIMARY KEY (policyid).
Beneficiary with
Dependents. FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE CASCADE)
Participation
constraints lead to CREATE TABLE Dependents (
NOT NULL pname CHAR(20),
constraints. age INTEGER,
What if Policies is a policyid INTEGER,
weak entity set? PRIMARY KEY (pname, policyid).
FOREIGN KEY (policyid) REFERENCES Policies,
ON DELETE CASCADE)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 31
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