Professional Documents
Culture Documents
02 Relational SP 08
02 Relational SP 08
Review
Why use a DBMS? OS provides RAM
and disk
Concurrency
Recovery
Abstraction, Data Independence
Query Languages
Efficiency (for most tasks)
Security
Data Integrity
Data Models
10101
11101
Conceptual Design
ER Model Basics
name
ssn
lot
Employees
enterprise?
What information about these entities and
relationships should we store in the database?
What integrity constraints or business rules hold?
A database `schema in the ER Model can be
represented pictorially (ER diagrams).
Can map an ER diagram into a relational schema.
name
since
name
ssn
dname
did
lot
Employees
ssn
budget
Departments
Works_In
since
dname
did
Employees
supervisor
budget
Departments
lot
Works_In
subordinate
Reports_To
name
ssn
Key Constraints
An employee can
work in many
departments; a
dept can have
many employees.
since
lot
Employees
dname
did
Manages
budget
Departments
Works_In
since
to be clear
1-to-1
Weak Entities
Participation Constraints
Does every employee work in a department?
If so, this is a participation constraint
the participation of Employees in Works_In is said to be
total (vs. partial)
What if every department has an employee working in it?
name
ssn
dname
lot
did
Employees
budget
Departments
Manages
name
ssn
lot
cost
pname
age
Works_In
Employees
Policy
Dependents
name
pname
lot
Employees
Key constraint on
Policies would
mean policy can
name
ssn
only cover 1
dependent!
Policies
policyid
cost
pname
lot
age
Dependents
Employees
Dependents
Covers
Bad design
age
Purchaser
Beneficiary
Better design
policyid
Policies
cost
Summary so far
qty
Parts
Contract
Departments
VS.
Suppliers
Parts
can-supply
needs
Suppliers
Departments
deals-with
binary
n-ary
Administrivia
Blog online
Syllabus & HW calendar coming on-line
Schedule and due dates may change (check
frequently)
Lecture notes are/will be posted
Other textbooks
Korth/Silberschatz/Sudarshan
ONeil and ONeil
Garcia-Molina/Ullman/Widom
name
login
Jones jones@cs
Smith smith@eecs
Smith smith@math
age
18
18
19
gpa
3.4
3.2
3.8
SQL Overview
CREATE TABLE <name> ( <field> <domain>, )
INSERT INTO <name> (<field names>)
VALUES (<field values>)
DELETE FROM <name>
WHERE <condition>
UPDATE <name>
SET <field name> = <value>
WHERE <condition>
SELECT <fields>
FROM <name>
WHERE <condition>
Keys
Keys are a way to associate tuples in
different relations
Keys are one form of integrity constraint
(IC)
Enrolled
sid
53666
53666
53650
53666
DELETE
FROM Students S
WHERE S.name = Smith
Powerful variants of these commands are available;
more later!
FOREIGN Key
Primary Keys
A set of fields is a superkey if:
No two distinct tuples can have same values in all key fields
E.g.
sid is a key for Students.
What about name?
The set {sid, gpa} is a superkey.
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
PRIMARY Key
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
11111 English102 A
Key and foreign key ICs are the most common; more
general ICs supported too.
In the real world, sometimes the constraint should
hold but doesnt --> data cleaning!
gpa
3.4
3.2
3.8
sid
name
login
age gpa
Semantics of a Query
S.sid
53666
53666
53666
53666
53688
53688
53688
53688
53650
53650
53650
53650
S.name
Jones
Jones
Jones
Jones
Smith
Smith
Smith
Smith
Smith
Smith
Smith
Smith
S.login
jones@cs
jones@cs
jones@cs
jones@cs
smith@ee
smith@ee
smith@ee
smith@ee
smith@math
smith@math
smith@math
smith@math
cid
grade
Carnatic101
C
Reggae203
B
Topology112 A
History105
B
S.name E.cid
Smith
Topology112
we get:
sid
53831
53831
53650
53666
GOSUB XML;
S.age
18
18
18
18
18
18
18
18
19
19
19
19
S.gpa
3.4
3.4
3.4
3.4
3.2
3.2
3.2
3.2
3.8
3.8
3.8
3.8
E.sid
53831
53832
53650
53666
53831
53831
53650
53666
53831
53831
53650
53666
E.cid
E.grade
Carnatic101
C
Reggae203
B
Topology112
A
History105
B
Carnatic101
C
Reggae203
B
Topology112
A
History105
B
Carnatic101
C
Reggae203
B
Topology112
A
History105
B
Internet Moment
dapp
denv
<<
dt
dt
If storing indefinitelyuse a flexible
representation
\YUCK!!
How do I relationalize my objects?
Have to write a converter for each
class?
Think about when to save things into
the DB?
Good news:
Can all be automated
With varying amounts of trouble
Object-Relational Mappings
Roughly:
Class ~ Entity Set
Instance ~ Entity
Data member ~ Attribute
Reference ~ Foreign Key
Details, details
Ruby on Rails
Ruby: an OO scripting language
and a pretty nice one, too
Rails and ER
Models
Employees
Departments
Very ER-like
app/models/state.rb
class State < ActiveRecord::Base
has_many :cities
end
app/models/city.rb
class City < ActiveRecord::Base
belongs_to :state
end
Further Reading
Chapter 18 (through 18.3) in Agile Web
Development with Rails
dname
did
lot
Employees
since
name
ssn
Works_In
budget
Departments