Professional Documents
Culture Documents
The Relational Model: Text: Chapter 3
The Relational Model: Text: Chapter 3
The Relational Model: Text: Chapter 3
Text: Chapter 3
Unit 3 2
Historical Perspective
Introduced by Edgar Codd (IBM) in 1970
Most widely used model today.
¾ Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
“Legacy systems” are usually hierarchical or network
models (i.e., not relational)
¾ e.g., IMS, IDMS, …
Competitor: object-oriented model
¾ ObjectStore, Versant, Ontos
¾ A synthesis emerging: object-relational model
o Informix Universal Server, UniSQL, O2, Oracle, DB2
Recent competitor: XML data model
Unit 3 3
Main Characteristics of the Relational Model
Unit 3 4
Structure of Relational Databases
Unit 3 8
The SQL Query Language
Unit 3 9
The SQL Query Language
Student
sid name address phone major
1234 W. 12 th
99111120 G. Jones 889-4444 CPSC
Ave., Van.
2020 E. 18 th St.,
92001200 G. Smith 409-2222 MATH
Van
2020 E. 18 th St.,
94001020 A. Smith 222-2222 CPSC
Van
To find the id’s, names and phones of all CPSC students, we can write:
… … …. … … … … …. …
Unit 3 11
Creating Relations in SQL/DDL
Unit 3 15
Keys Constraints (for Relations)
Unit 3 17
Keys Constraints in SQL (cont’)
Unit 3 21
Referential Integrity in SQL/92
Unit 3 22
More IC’s in SQL
SQL supports two types of IC’s
¾ table constraints
¾ assertions
Table constraints
¾ are part of the table definition
¾ domain, key, referential constraints, etc
¾ Checked when a table is created or modified
Assertions
¾ Involve more than one table
¾ Checked when any of the tables is modified
SQL constraints can be named
¾ CONSTRAINT studentKey PRIMARY KEY (sid)
Constraints can be set to be
¾ DEFERRED (is applied at the end of the transaction
¾ IMMEDIATE (is applied at the end of an SQL statement)
More on constraints when we discuss SQL
Unit 3 23
Where do ICs Come From?
Unit 3 24
Logical DB Design: ER to Relational
Entity sets to tables.
Each strong entity set is mapped to a table.
¾ entity attributes become table attributes
¾ entity keys become table keys
Unit 3 25
Relationship Sets to Tables: Simple Case
since
name dname ssn and did
ssn salary did budget CANNOT
be null
since
name dname
ssn salary did budget
Unit 3 28
Translating ER Diagrams with Key Constraints
WRONG WAY:
CREATE TABLE Manages(
¾ Create a separate table
ssn CHAR(11),
for Manages:
did INTEGER,
¾ Note that did is the key
since DATE,
now!
PRIMARY KEY (did),
¾ Create separate tables
FOREIGN KEY (ssn) REFERENCES Employee,
for Employee and
FOREIGN KEY (did) REFERENCES Department)
Department.
RIGHT WAY
CREATE TABLE Dept_Mgr(
¾ Since each department did INTEGER,
has a unique manager, dname CHAR(20),
we can combine budgetREAL,
Manages and ssn CHAR(11),
Department into one since DATE,
table. PRIMARY KEY (did),
¾ Create another table for FOREIGN KEY (ssn) REFERENCES Employee)
Employee
Unit 3 29
Translating Participation Constraints
Unit 3 30
Participation Constraints in SQL
Using the right method for Manages (add Manages relation in the Department
table), we can capture participation constraints by
¾ ensuring that each did is associated with a ssn that is not null
¾ not allowing to delete a manager before he/she is replaced
Unit 3 32
Translating Weak Entity Sets
name
cost pname birthday
ssn salary
Unit 3 33
Translating Weak Entity Sets(cont’)
Weak entity set and its identifying relationship set are translated into a
single table.
¾ Primary key would consist of the owner’s primary key and week entity’s
partial key
¾ When the owner entity is deleted, all owned weak entities must also be
deleted.
Employee
hourly_rate hours
contractid
Hourly_Emp Contract_Emp
Unit 3 35
First Attempt : Totally unsatisfactory
name
ssn salary
Unit 3 36
Method 1 : One table
name
ssn
Wastes a lot of
salary space for null
values
Employee Worse when there
are many
hourly_rate
subclasses with
hours
many attributes
contractid Excellent method
if subclasses do
not have any new
Hourly_Emp Contract_Emp attributes and
relationships
One table which has all attributes and a type field which can have values
to identify the type of the employee:
Unit 3 37
Method 2: Tables for superclass and subclasses
name
ssn salary
• Works well for queries
Employee involving only
superclass attributes or
hourly_rate hours only the extra subclass
attributes
contractid
• Have to combine two
tables to get all
Hourly_Emp Contract_Emp
attributes for a
subclass
Superclass table contains al superclass attributes
Each subclass table contains primary key of superclass and the
subclass attributes
Employee(sin, name, salary)
Hourly_Emp(sin, hourly_rate, hours)
Contract_Emp(sin, contractid)
Unit 3 38
Method 3: Only subclass tables
name
ssn salary
If ISA-relation is partial, it
Employee cannot be applied (may
loose entities)
hourly_rate hours Works poorly if the
superclass participates in
contractid
any relationship ( should
not applied in that case)
Hourly_Emp Contract_Emp
If ISA-relation is not
No table for superclass disjoint, it duplicates info
One table per subclass containing
¾ all superclass and subclass attributes
Unit 3 40
Translating Aggregation
ssn name
bname type
Unit 3 43
View Updates
View updates must occur at the base tables.
¾ Ambiguous
¾ Difficult
DBMS’s restrict view updates only to some simple views
on single tables (called updatable views)
How to handle DROP TABLE if there’s a view on the
table?
DROP TABLE command has options to prevent a table
from being dropped if views are defined on it:
¾ DROP TABLE Student RESTRICT
o drops the table, unless there is a view on it
¾ DROP TABLE Student CASCADE
o drops the table, and recursively drops any view referencing it
Unit 3 44
Relational Model: Summary
Unit 3 45