








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









what
data integrity
means
how
integrity constraints
are expressed in SQL
Introduction Integrity constraints ^
Integrity constraints
( ICs
are an instrument to ensure that changes of the database by authorized users donot lead to a loss of data consistency. −^
serve for a restriction of the database states to those ones that really exist in thereal world. −^
are semantically derivable from the posed data model and can therefore alreadybe specified during the creation of the schema. ^
advantages −^
Consistency conditions are specified only once. −^
Consistency conditions are checked automatically by the DBMS. −^
APs do not need to care about a check of the conditions. ^
Static integrity constraints
relate to restrictions of the possible database states,
dynamic integrity constraints
to restrictions of the possible database state transi-
tions.
Static Integrity Constraints Type integrity
/ domain constraints
Each attribute value must be atomic. ^
determination of upper and lower bounds of attribute values −^
SQL92 allows the explicit definition of domains (not implemented in Oracle) ^
enumeration of values −^
Such enumeration types can be modeled by an own relation with one attribute. ^
null values −^
The construct
not null
attached to an attribute declaration can be used to achieve
that the value of the corresponding attribute exists for all tuples. Tuple constraints ^
restriction of the values which a tuple can take with respect to its different attributes ^
example: In a relation
occupancy
, where a tuple stores the reservation of a seat from
a train station
to a station
should be unequal to
In SQL a tuple constraint can be defined by the
check
clause
during the specification
of the schema.
Relation constraints / key constraints ^
refers to the set of all tuples of a relation ^
key integrity
by means of primary key and candidate key
no two tuples with equal keys −^
in SQL declaration of this constraint with
unique
and
primary key
during the
specification of the schema −^
No primary key or candidate key can have the value
null
aggregation constraints −^
example: An aggregate value shall be above or below a predefined bound. ^
Recursive constraints −^
example: In a relation
TrainConnection
each station should be connected with
each other station. Referential constraints ^
refer to several relations
Maintenance of referential integrity ^
In a relation, which models a relationship, we should ensure that values are assignedto the foreign keys. ^
formulation of explicit integrity constraints in the query language, small extensions ofthe query language ^
formulation in relational algebra: −^
relation
with primary key
relation
with foreign key
that references
π( F
⊆^ π
Allowed changes −^
insertion of a tupel
s^ into
if^ s
∈^ π
change of an attribute value of a tuple
s^ if
s’^
π( K
R ) (let
s’^
be the new value of
tuple
s ) −^
change of
r [ K
] of a tuple
r^ if
σ F
= r [ K
deletion of a tuple
r^ of
if^ σ
F = r
Referential integrity in the ER model ^
N -ary relationships contain the keys of all participating entities, i.e., all keys are for-eign keys. ^
A weak entity set must include the primary key of the entity set on which the weakentity set depends. That is, the weak entity set contains the primary key of the strongentity set as foreign key. Integrity constraints in SQL ^
domain constraints −^
not null +^ null value as attribute value not permitted +^ example: FamilyName
character
not null
default
smallint
default 0
default
null
: optional, default value
example check
is null and 1
is null and
or
is not null and
is not null and
The constraint check
sem
between
^1 and
is also fulfilled for a null value as semester. Relation constraints/ key constraints ^
primary key
clause
automatically specified as
not null
example:FirstName
character
not null
LastName
character
not null
Salary
numeric
primary key
(LastName, FirstName)
candidate key:
unique
example:FirstName
character
not null
LastName
character
not null
unique
(LastName, FirstName)
Referential Integrity ^
foreign key:
foreign key
unique foreign key
models a 1:1-relationship
Foreign key must be marked as primary key in the referenced relation. ^
example: create table
customers(cid
integer,
primary key
(cid))
create table
orders(..., cid
integer,
foreign key
(cid)
references
customers(cid))
standard behavior when violating a referential integrity constraint: rejection of theaction that triggered the violation ^
If a foreign key is specified with a
cascade
clause, changes of the respective primary
key are propagated.
Management of Integrity Constraints Adding ICs ^
alter table
orders
add constraint
limit
check
(price * number < 10000)
alter table
customers
add constraint
unique_name
unique
cname
alter table
customers
drop constraint
unique_name
Enabling and disabling ICs ^
alter table
customers
enable
constraint
unique_name
alter table
customers
disable
constraint
unique_name
Complex Integrity Constraints Assertions ^
An
assertion
is a predicate that expresses a condition which is to be always satisfied
by a database. Domain constraints and referential ICs are special kinds of asser-tions. There are also conditions which cannot be expressed with these two kinds likeconditions with respect to several relations. ^
syntax:
create assertion
check
example: There must be at least four professors in order to maintain teaching. create assertion
AlwaysFourProfessors
check
select count
from
professors))
The DBMS tests an assertion for validity. If the assertion is valid, future modificationsof the database are only allowed if the assertion is not violated. ^
Complex assertions can lead to an overhead. ^
Assertions should be used with care.