The Relational Model�
Dr.S.Sivakumar,Principal
C.P.A College, Bodinayakanur
Why Study the Relational Model?
Relational databases
Relational Database: Definitions
Example Instance of Students Relation
be distinct?
Relational Query Languages
The SQL Query Language
The SQL Query Language
SELECT *
FROM Students S
WHERE S.age=18
SELECT S.name, S.login
Querying Multiple Relations
SELECT S.name, E.cid
FROM Students S, Enrolled E
WHERE S.sid=E.sid AND E.grade=“A”
Given the following instance of Enrolled (is this possible if the DBMS ensures referential integrity?):
we get:
Creating Relations in SQL
CREATE TABLE Students
(sid: CHAR(20),
name: CHAR(20),
login: CHAR(10),
age: INTEGER,
gpa: REAL)
CREATE TABLE Enrolled
(sid: CHAR(20),
cid: CHAR(20),
grade: CHAR(2))
Destroying and Altering Relations
DROP TABLE Students
ALTER TABLE Students
ADD COLUMN firstYear: integer
Adding and Deleting Tuples
INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
DELETE
FROM Students S
WHERE S.name = ‘Smith’
Integrity Constraints (ICs)
Primary Key Constraints
1. No two distinct tuples can have same values in all key fields, and
2. This is not true for any subset of the key.
Primary and Candidate Keys in SQL
CREATE TABLE Enrolled
(sid CHAR(20)
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid) )
CREATE TABLE Enrolled
(sid CHAR(20)
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid),
UNIQUE (cid, grade) )
Foreign Keys, Referential Integrity
Foreign Keys in SQL
CREATE TABLE Enrolled
(sid CHAR(20), cid CHAR(20), grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students )
Enrolled
Students
Enforcing Referential Integrity
Referential Integrity in SQL/92
CREATE TABLE Enrolled
(sid CHAR(20) default ‘11111’,
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid)
REFERENCES Students
ON DELETE CASCADE
ON UPDATE SET DEFAULT )
Where do ICs Come From?
Logical DB Design: ER to Relational
CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
Employees
ssn
name
lot
Relationship Sets to Tables
CREATE TABLE Works_In(
ssn CHAR(1),
did INTEGER,
since DATE,
PRIMARY KEY (ssn, did),
FOREIGN KEY (ssn)
REFERENCES Employees,
FOREIGN KEY (did)
REFERENCES Departments)
Review: Key Constraints
Translation to
relational model?
Many-to-Many
1-to-1
1-to Many
Many-to-1
dname
budget
did
since
lot
name
ssn
Manages
Employees
Departments
Translating ER Diagrams with Key Constraints
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)
Review: Participation Constraints
lot
name
dname
budget
did
since
name
dname
budget
did
since
Manages
since
Departments
Employees
ssn
Works_In
Participation Constraints in SQL
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)
Review: Weak Entities
lot
name
age
pname
Dependents
Employees
ssn
Policy
cost
Translating Weak Entity Sets
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)
Review: ISA Hierarchies
Contract_Emps
name
ssn
Employees
lot
hourly_wages
ISA
Hourly_Emps
contractid
hours_worked
Translating ISA Hierarchies to Relations
Review: Binary vs. Ternary Relationships
age
pname
Dependents
Covers
name
Employees
ssn
lot
Policies
policyid
cost
Beneficiary
age
pname
Dependents
policyid
cost
Policies
Purchaser
name
Employees
ssn
lot
Bad design
Better design
Binary vs. Ternary Relationships (Contd.)
CREATE TABLE Policies (
policyid INTEGER,
cost REAL,
ssn CHAR(11) NOT NULL,
PRIMARY KEY (policyid).
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE CASCADE)
CREATE TABLE Dependents (
pname CHAR(20),
age INTEGER,
policyid INTEGER,
PRIMARY KEY (pname, policyid).
FOREIGN KEY (policyid) REFERENCES Policies,
ON DELETE CASCADE)
Views
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
Views and Security
Relational Model: Summary