Lecture4 The - Relational - Model PartII Jan16 2018
Lecture4 The - Relational - Model PartII Jan16 2018
Lecture4 The - Relational - Model PartII Jan16 2018
Mohammad Hammoud
Today…
Last Session:
The ER model
Today’s Session:
The relational model
Basic SQL
ER to relational databases
Announcements:
PS1 is due on Sunday, January 21 by midnight
In this week’s recitation we will practice on translating ER designs into
relational databases
Outline
Query Languages
Creating Relations in SQL
S1 can be used to create the “Students” relation
S1
The DBMS enforces domain constraints whenever tuples are added or modified
Adding and Deleting Tuples
We can insert a single tuple to the “Students” relation using:
INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
Powerful variants of these commands are available; more on this next week!
Querying a Relation
How can we find all 18-year old students?
sid name login age gpa
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8
S E
S.name E.cid
We get: Smith Topology112
Destroying and Altering Relations
How to destroy the relation “Students”?
Every tuple in the current instance is extended with a null value in the
new field!
Integrity Constraints (ICs)
An IC is a condition that must be true for any instance of the
database (e.g., domain constraints)
ICs are specified when schemas are defined
ICs are checked when relations are modified
Enrolled
Students
sid cid grade
sid name login age gpa
53666 15-101 C
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
Keys
Keys help associate tuples in different relations
Enrolled
Students
sid cid grade
sid name login age gpa
53666 15-101 C
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
Examples:
sid is a key for Students (what about name?)
The set {sid, name} is a superkey (or a set of fields that contains a key)
Primary and Candidate Keys in SQL
Many candidate keys (specified using UNIQUE) can be designated
and one is chosen as a primary key
“A student can take only one course, and no two students in a course receive the same
grade”
Foreign Keys and Referential Integrity
A foreign key is a set of fields referring to a tuple
in another relation
It must correspond to the primary key of the
other relation
It acts like a `logical pointer’
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
Foreign Keys in SQL
Example: Only existing students may enroll for
courses
CREATE TABLE Enrolled
(sid CHAR(20),cid CHAR(20),grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students )
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
Enforcing Referential Integrity
What should be done if an “Enrolled” tuple with a
non-existent student id is inserted? (Reject it!)
Key and foreign key ICs are the most common; more general
ICs are supported too
Views
A view is a table whose rows are not explicitly stored but
computed as 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
Relationship sets
1-to-1, 1-to-many, and many-to-many
Key/Total/Partial participation
M-to-N Relationship Sets to Tables
since
name dname
ssn lot did budget
Approach 1:
Create separate tables for Manages and Departments
1-to-M Relationship Sets to Tables
since
name dname
ssn lot did budget
Approach 2:
Create a table for only the Departments entity set (i.e., take advantage of the key constraint)
One-Table vs. Two-Table Approaches
Works_In
since
name
ssn lot
Employees
hourly_wages hours_worked
ISA
contractid
Hourly_Emps Contract_Emps
Translating ISA Hierarchies to Relations
name
ssn lot
Hourly_Emps Contract_Emps
Alternatively: Employees
Duplicate Values!
Translating Aggregations
Consider the following example:
name
ssn lot
Employees
Monitors until
started_on since
dname
pid pbudget did budget
started_on since
For the Monitors relationship, pid pbudget did
dname
budget
(Binary) relationships:
Get keys from all participating entities:
1:1 -> either key can be the primary key
1:N -> the key of the ‘N’ part will be the primary key
M:N -> both keys will be the primary key
Weak entities:
Strong key + partial key -> primary key
..... ON DELETE CASCADE
ER to Tables - Summary of Advanced
Total/Partial participation:
NOT NULL
Ternary relationships:
Get keys from all; decide which one(s) -> primary Key
ISA:
3 tables (most general)
2 tables (‘total coverage’)
Outline
Query Languages
Relational Query Languages
Query languages (QLs) allow manipulating and retrieving
data from databases
Relational Calculus
Queries are subsets of first-order logic
Queries describe desired answers without specifying how they will
be computed
A type of non-procedural (or declarative) formal query language
Next Class
Relational Algebra