Docsity

Data Integrity - Database Management Systems - Lecture Slides | COP 5725, Study notes of Computer Science

Partial preview of the text

Download Data Integrity - Database Management Systems - Lecture Slides | COP 5725 and more Study notes Computer Science in PDF only on Docsity!

What you should have learned after this lecture ...

‰^

what

data integrity

means

‰^

how

integrity constraints

are expressed in SQL

9. Data Integrity

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

X^

to a station

Y ,^ X

should be unequal to

Y.

‰^

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

R^

with primary key

K

−^

relation

S^

with foreign key

F^

that references

K

π( F

S )^

⊆^ π

( RK

‰^

Allowed changes −^

insertion of a tupel

s^ into

S^

if^ s

[ F ]^

∈^ π

( RK

−^

change of an attribute value of a tuple

s^ if

s’^

[ F ]

∈^

π( K

R ) (let

s’^

be the new value of

tuple

s ) −^

change of

r [ K

] of a tuple

r^ if

σ F

= r [ K

( S ]

−^

deletion of a tuple

r^ of

R^

if^ σ

F = r

([ K ]

S ) =

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

+^ initialization of an attribute value with the value which must be a con-stant or the result of the evaluation of an expression. The value mustbe compatible with the domain of the attribute. +^ example: NoOfChildren

smallint

default 0

−^

default

null

: optional, default value

−^

example check

(( S

is null and 1

S^2

is null and

or

( S^1

is not null and

S^2

is not null and

−^

The constraint check

sem

between

^1 and

^8

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.