Professional Documents
Culture Documents
History (cont.)
1980s:
Research relational prototypes evolve into commercial
systems
SQL becomes industrial standard
Parallel and distributed database systems
Object-oriented database systems
1990s:
Large decision support and data-mining applications
Large multi-terabyte data warehouses
Emergence of Web commerce
Early 2000s:
XML and XQuery standards
Automated database administration
Later 2000s:
Giant data storage systems
Google BigTable, Yahoo PNuts, Amazon, ..
Introducnon to ke|anona| Mode|
Example of a Relation
attributes
(or columns)
tuples
(or rows)
Attribute Types
The set of allowed values for each
attribute is called the domain of the
attribute
Attribute values are (normally)
required to be atomic; that is,
indivisible
The special value null is a member
of every domain
The null value causes complications
in the denition of many operations
Relation Schema and Instance
A
1
, A
2
, , A
n
are attributes
R = (A
1
, A
2
, , A
n
) is a relation schema
Example:
instructor = (ID, name, dept_name, salary)
given sets of domain D
1
, D
2
, . D
n
,
a relation r is a subset of
D
1
x D
2
x x D
n
#
Thus, a relation is a set of n-tuples (a
1i
, a
2i
, , a
ni
)
where each a
i
! D
i
1. Primary Key:
CREATE TABLE branch(
bname CHAR(15) PRIMARY KEY,
bcity CHAR(20),
assets INT);
or
CREATE TABLE depositor(
cname CHAR(15),
acct_no CHAR(5),
PRIMARY KEY(cname, acct_no));
2. Candidate Keys:
CREATE TABLE customer (
ssn CHAR(9) PRIMARY KEY,
cname CHAR(15),
address CHAR(30),
city CHAR(10),
UNIQUE (cname, address, city);
key ConsLralnLs
LecL of SCL key declarauons
8lMA8? (A1, A2, .., An)
or
unlCuL (A1, A2, ..., An)
lnseruons: check lf any Luple has same values for A1, A2, .., An as any
lnserLed Luple. lf found, re[ect |nsernon
updaLes Lo any of A1, A2, ..., An: LreaL as lnseruon of enure Luple
Primary vs Unique (candidate)
1. one primary key per table, several unique keys allowed.
2. Only primary key can be referenced by foreign key (SQL
Server allows to refer to candidate key)
3. DBMS may treat primary key differently
(e.g.: implicitly create an index on PK)
4. NULL values permitted in UNIQUE keys but not in PRIMARY
KEY
5. Primary key can not be modied (SQL Server allows)
8eferenual lnLegrlLy: lorelgn key
Referential Integrity is a constraint to enforce relationship between primary
key (some DBMS consider condidate key) of one relation and foreign key of
the other. Referential Integrity is used to ensure that each value of a foreign
key attribute refers to an entity that appears in the foreign table. As with other
constraints, any attempt to modify the database contents that would cause a
foreign key constraint violation must be disallowed.
Relational database systems provide enforcement of referential integrity
constraints. The constraint is specified in the database schema, and the
database system enforces it.
8eferenual lnLegrlLy: lorelgn key
Lnsures LhaL a value LhaL appears ln one relauon for a glven seL of
aurlbuLes also appears for a cerLaln seL of aurlbuLe ln anoLher
relauon.
lf an accounL exlsLs ln Lhe daLabase wlLh branch name erryrldge,
Lhen Lhe branch erryrldge musL acLually exlsL ln Lhe daLabase.
rlmary keys of
respecuve relauons
lorelgn key
branch (branch-name, branch-clLy, asseL )
erryrldge 8rooklyn 300,000
accounL ( accounL-no, branch-name, balance )
A-123 erryrldge 3000
A seL of aurlbuLes x ln 8 ls a forelgn key lf lL ls noL a prlmary key of 8 buL
lL ls a prlmary key of some relauon S.
8eferenual lnLegrlLy
lormal uenluon
LeL r
1
(8
1
) and r
2
(8
2
) be relauons wlLh prlmary keys k
1
and k
2
respecuvely.
1he subseL # of 8
2
ls a forelgn key referenclng k
1
ln relauon r
1
, lf for every
L
2
ln r
2
Lhere musL be a Luple L
1
ln r
1
such LhaL L
1
[k
1
]=L
2
[#].
8eferenual lnLegrlLy consLralnL: $
#
(r
2
) " $
k1
(r
1
)
R
2
( K
2
, ., #, )
t
2
R
1
( K
1
, ., . )
t
1
)
)
8eferenual lnLegrlLy for lnseruon and
ueleuon
1he followlng LesLs musL be made ln order Lo preserve Lhe followlng
referenual lnLegrlLy consLralnL:
$
#
(r
2
) " $
k
(r
1
)
lnserL. lf a Luple L
2
ls lnserLed lnLo r
2
. 1he sysLem musL ensure LhaL
Lhere ls a Luple L1 ln r1 such LhaL L
1
[k] = L
2
[#]. 1haL ls
L
2
[#] !$
k
(r
1
)
ueleLe. lf a Luple L
1
ls deleLed from r
1
, Lhe sysLem musL compuLe Lhe
seL of Luples ln r
2
LhaL reference L
1
:
%
#=L1[k]
(r
2
)
lf Lhls seL ls noL empLy, elLher Lhe deleLe command ls re[ecLed as an
error, or Lhe Luples LhaL reference L
1
musL Lhemselves be deleLed
(cascadlng deleuons are posslble)
8eferenual lnLegrlLy for updaLe
lf a Luple L
2
ls updaLed ln relauon r
2
and Lhe updaLe modles values
for Lhe forelgn key #, Lhen a LesL slmllar Lo Lhe lnserL case ls made. LeL
L
2
denoLe Lhe new value of Luple L
2
. 1he sysLem musL ensure LhaL
L
2
[#]! $
k
(r
1
)
lf a Luple L
1
ls updaLed ln r
1
, and Lhe updaLe modles values for
prlmary key(k), Lhen a LesL slmllar Lo Lhe deleLe case ls made. 1he
sysLem musL compuLe
%
#=L1[k]
(r
2
)
uslng Lhe old value of L
1
(Lhe value before Lhe updaLe ls applled). lf
Lhls seL ls noL empLy, Lhe updaLe may be re[ecLed as an error, or Lhe
updaLe may be applled Lo Lhe Luples ln Lhe seL (cascade updaLe), or
Lhe Luples ln Lhe seL may be deleLed.
new foreign key
value must exist
no foreign keys contain
the old primary key
8eferenual lnLegrlLy ln SCL -example
creaLe Lable cusLomer
(cusLomer-name char(20) noL null,
cusLomer-sLreeL char(30),
cusLomer-clLy char(30),
prlmary key (cusLomer-name))
creaLe Lable branch
(branch-name char(13) noL null,
branch-clLy char(30),
asseLs lnLeger,
prlmary key (branch-name))
8eferenual lnLegrlLy ln SCL- example
creaLe Lable accounL
(branch-name char(13),
accounL-number char(10) noL null,
balance lnLeger,
prlmary key(accounL-number),
forelgn key (branch-name) references branch)
creaLe Lable deposlLor
(cusLomer-name char(20) noL null,
accounL-number char(10) noL null,
prlmary key (cusLomer-name, accounL-number),
forelgn key (accounL-number) references accounL,
forelgn key (cusLomer-name) references cusLomer)
Cascadlng Acuons ln SCL
Due to the on de|ete cascade c|auses, |f a de|ete of a
tup|e |n branch resu|ts |n referenna|-|ntegr|ty constra|nt
v|o|anon, the de|ete cascades to the account
re|anon, de|enng the tup|e that refers to the branch that
was de|eted.
Cascad|ng updates are s|m||ar.
creaLe Lable accounL
...
forelgn key (branch-name) references branch
on deleLe cascade
on updaLe cascade,
.)