03 Relational Model
03 Relational Model
• Cardinality = 3, arity = 5 ,
• all rows distinct
• Q: do values in a column need to
be distinct?
Faloutsos - Pavlo, 15-415/615 7
SQL - A language for Relational
DBs
• SQL* (a.k.a. “Sequel”), standard
language
• Data Definition Language (DDL)
– create, modify, delete relations
– specify constraints
– administer users, security, etc.
create table student
– E.g.:
(ssn fixed, name char(20));
* Structured Query Language
• Another example:
DELETE
FROM Students S
WHERE S.name = ‘Smith’
Enrolled
sid cid grade Students
53666 15-101 C sid name login age gpa
53666 18-203 B 53666 Jones jones@cs 18 3.4
53650 15-112 A 53688 Smith smith@cs 18 3.2
53666 15-105 B 53650 Smith smith@math 19 3.8
Enrolled
sid cid grade Students
53666 15-101 C sid name login age gpa
53666 18-203 B 53666 Jones jones@cs 18 3.4
53650 15-112 A 53688 Smith smith@cs 18 3.2
53666 15-105 B 53650 Smith smith@math 19 3.8
Enrolled
sid cid grade Students
53666 15-101 C sid name login age gpa
53666 18-203 B 53666 Jones jones@cs 18 3.4
53650 15-112 A 53688 Smith smith@cs 18 3.2
53666 15-105 B 53650 Smith smith@math 19 3.8
• Subtle issues:
• What should be done if an Enrolled tuple
with a non-existent student id is inserted?
Enrolled
sid cid grade Students
53666 15-101 C sid name login age gpa
53666 18-203 B 53666 Jones jones@cs 18 3.4
53650 15-112 A 53688 Smith smith@cs 18 3.2
53666 15-105 B 53650 Smith smith@math 19 3.8
• Subtle issues:
• What should be done if an Enrolled tuple
with a non-existent student id is inserted?
(Reject it!)
Enrolled
sid cid grade Students
53666 15-101 C sid name login age gpa
53666 18-203 B 53666 Jones jones@cs 18 3.4
53650 15-112 A 53688 Smith smith@cs 18 3.2
53666 15-105 B 53650 Smith smith@math 19 3.8
• Introduction
• Integrity constraints (IC)
• Enforcing IC
• Querying Relational Data
• ER to tables
• Intro to Views
• Destroying/altering tables
• strong entities
• weak entities
• (binary) relationships
– 1-to-1, 1-to-many, etc
– total/partial participation
• ternary relationships
• ISA-hierarchies
• aggregation
Employees
since
name dname
ssn lot did budget
1-to-1 Many-to-1
• strong entities:
– key -> primary key
• (binary) relationships:
– get keys from all participating entities -
pr. key:
– 1-to-1 -> either key (other: ‘cand. key’)
– 1-to-N -> the key of the ‘N’ part
– M-to-N -> both keys
Single-table-solution
Faloutsos - Pavlo, 15-415/615 56
Translating ER with Key
Constraints since
name dname
ssn lot did budget
• strong entities
• weak entities
• (binary) relationships
– 1-to-1, 1-to-many, etc
– total/partial participation
• ternary relationships
• ISA-hierarchies
• aggregation
Works_In
since
Faloutsos - Pavlo, 15-415/615 62
Participation Constraints in SQL
• strong entities
• weak entities
• (binary) relationships
– 1-to-1, 1-to-many, etc
– total/partial participation
• ternary relationships
• ISA-hierarchies
• aggregation
name
cost dname age
ssn lot
• strong entities
• weak entities
• (binary) relationships
– 1-to-1, 1-to-many, etc
– total/partial participation
• ternary relationships
• ISA-hierarchies
• aggregation
hourly_wages hours_worked
ISA
contractid
Hourly_Emps Contract_Emps
name
ssn lot
Employees
hourly_wages hours_worked
ISA
contractid
Hourly_Emps Contract_Emps
• strong entities
• weak entities
• (binary) relationships
– 1-to-1, 1-to-many, etc
– total/partial participation
• ternary relationships
• ISA-hierarchies
• aggregation
• Introduction
• Integrity constraints (IC)
• Enforcing IC
• Querying Relational Data
• ER to tables
• Intro to Views
• Destroying/altering tables
• Virtual tables
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
• DROP VIEW
• Introduction
• Integrity constraints (IC)
• Enforcing IC
• Querying Relational Data
• ER to tables
• Intro to Views
• Destroying/altering tables
• DROP TABLE
• ALTER TABLE, e.g.
ALTER TABLE students
ADD COLUMN maiden-name CHAR(10)
• strong entities:
– key -> primary key
• (binary) relationships:
– get keys from all participating entities - pr. key:
– 1:1 -> either key
– 1:N -> the key of the ‘N’ part
– M:N -> both keys
• weak entities:
– strong key + partial key -> primary key
– ..... ON DELETE CASCADE