Professional Documents
Culture Documents
9/12/2011
9/12/2011
9/12/2011
The model was first proposed by Dr. E.F. Codd of IBM in 1970
A Relational Model for Large Shared Data Banks, Communications
of the ACM, June 1970
The above paper caused a major revolution in the field of database
management and earned Ted Codd the coveted ACM Turing Award
in 1981
9/12/2011
9/12/2011
Some Terms
Informal Name
Table
Row or Record
Column or Field
No. of Rows
No. of Columns
Unique Identifier
Legal Values Set
9/12/2011
Formal Name
Relation
Tuple
Attribute
Cardinality
Degree or Arity
Primary key
Domain
9/12/2011
sid
53666
53688
53650
name
login
Jones jones@cs
Smith smith@eecs
Smith smith@math
age gpa
18 3.4
18 3.2
19 3.8
9/12/2011
9/12/2011
Significance of domains
Domain-constrained comparisons
Select ..
From P, SP
Where P.P# = SP.P#
Select ..
From P, SP
Where P.weight = SP.qty
Both are valid queries in SQL, but second one makes no sense!!
Domains implemented as Data-Types?
9/12/2011
10
Relational Systems
9/12/2011
11
12
9/12/2011
13
Properties of Relations
9/12/2011
14
Types of Relations
Base Relations
The original (given) relations
Derived Relations
Relations obtained from base relations
Views
Virtual derived relation
Only definition is stored in the catalog
Definition executed at run-time
Snapshots
Real derived relation
Query Result
Unnamed derived relation
9/12/2011
15
Integrity Constraints
Data Integrity
Key Constraints
Domain (check) constraints
Not null constraints
Referential Integrity
Foreign Keys
Assertions
A condition that the database must always satisfy
Domain constraints and referential integrity constraints are
special cases
Every loan has at least one customer who maintains an
account with a balance of minimum 10000.
9/12/2011
16
9/12/2011
17
9/12/2011
18
SELECT *
FROM Students S
WHERE S.age=18
sid
name
53666 Jones
login
age gpa
jones@cs
18 3.4
9/12/2011
19
sid
53666
53688
53650
name
login
age gpa
Jones jones@cs
18 3.4
Smith smith@eecs 18 3.2
Smith smith@math 19 3.8
9/12/2011
sid
53831
53831
53650
53666
cid
grade
Carnatic101
C
Reggae203
B
Topology112
A
History105
B
we get:
S.name E.cid
Smith
Topology112
20
21
Destroys the relation Students. The schema information and the tuples
are deleted.
9/12/2011
22
9/12/2011
23
IC: condition that must be true for any instance of the database;
e.g., domain constraints
ICs are specified when schema is defined
ICs are checked when relations are modified
A legal instance of a relation is one that satisfies all specified ICs
DBMS should not allow illegal instances
If the DBMS checks ICs, stored data is more faithful to real-world
meaning
Avoids data entry errors, too!
9/12/2011
24
9/12/2011
25
Possibly many candidate keys (specified using UNIQUE), one of which is chosen
as the primary key.
9/12/2011
27
Only students listed in the Students relation should be allowed to 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
53666
53666
53650
53666
cid
grade
Carnatic101
C
Reggae203
B
Topology112
A
History105
B
9/12/2011
Students
sid
53666
53688
53650
name
login
Jones jones@cs
Smith smith@eecs
Smith smith@math
age gpa
18 3.4
18 3.2
19 3.8
28
Consider Students and Enrolled; sid in Enrolled is a foreign key that references
Students
What should be done if an Enrolled tuple with a non-existent student id is
inserted? (Reject it!)
What should be done if a Students tuple is deleted?
Also delete all Enrolled tuples that refer to it
Disallow deletion of a Students tuple that is referred to
Set sid in Enrolled tuples that refer to it to a default sid
(In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null,
denoting `unknown or `inapplicable)
Similar if primary key of Students tuple is updated
9/12/2011
29
9/12/2011
30
9/12/2011
31
ssn
name
Employees
9/12/2011
CREATE TABLE
Employees
(ssn CHAR(11),
lot
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
32
name
ssn
lot
Employees
dname
did
Works_In
lot
Employees
since
name
ssn
budget
Departments
supervisor
subordinate
Reports_To
33
9/12/2011
34
dname
lot
Employees
did
Manages
budget
Departments
Translation to
relational model?
1-to-1
9/12/2011
1-to Many
Many-to-1
Many-to-Many
35
Department-Manages-Employee
CREATE TABLE Departments
(dname CHAR(20),
did INTEGER,
budget INTEGER,
PRIMARY KEY (did))
CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
9/12/2011
36
Employees,
FOREIGN KEY (did) REFERENCES
CREATE
TABLE Dept_Mgr(
Departments)
did INTEGER,
name CHAR(20),
budget REAL,
ssn CHAR(11),
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees)
37
name
ssn
did
lot
Employees
dname
Manages
budget
Departments
Works_In
9/12/2011
since
38
39
Contd
9/12/2011
40
lot
Employees
9/12/2011
cost
Policy
pname
age
Dependents
41
Weak entity set and identifying relationship set are translated into a
single table.
When the owner entity is deleted, all owned weak entities must also
be deleted.
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)
9/12/2011
42
9/12/2011
43
name
ssn
lot
Employees
hours_worked
ISA
Hourly_Emps
contractid
Contract_Emps
9/12/2011
44
General approach:
3 relations: Employees, Hourly_Emps and Contract_Emps.
Hourly_Emps:
Every employee is recorded in Employees For hourly emps,
extra info recorded in Hourly_Emps (hourly_wages,
hours_worked, ssn)
must delete Hourly_Emps tuple if referenced Employees
tuple is deleted)
45
name
Employees
pname
lot
Policies
policyid
ssn
name
Dependents
Covers
Bad design
cost
pname
lot
age
Dependents
Employees
Purchaser
Better design
9/12/2011
age
Beneficiary
Policies
policyid
cost
46
9/12/2011
47
The key
constraints allow
us to combine
Purchaser with
Policies and
Beneficiary with
Dependents.
Participation
constraints lead
to NOT NULL
constraints.
What if Policies
is a weak entity
set?
9/12/2011
ssn
Aggregation
name
lot
Employees
Monitors
since
started_on
pid
pbudget
Projects
until
dname
did
Sponsors
budget
Departments
49
Aggregation
If Sponsors
has no descriptive attributes
Has total participation in Monitors
9/12/2011
50
Views
51
Updating Views
SQL 99 allows updates on views using more than one table where
the PK of that table is included in the fields of view
Views defined using UNION, INTERSECT, EXCEPT cant be
inserted into even if they are updatable
9/12/2011
52
Insertion
9/12/2011
53
9/12/2011
54
9/12/2011
55