Integrity Constraints
Integrity Constraints
0 Integrity Constraints
• Integrity constraints are those constraints in database system which guard against invalid
database operations or accidental damage to the database, by ensuring that authorized
changes to the database. It does not allow to loss of data consistency in database, it
ensures database consistency.
• In fact, integrity constraints provide a way of ensuring that changes made to the database
by authorized users do not result in a loss of data consistency.
• Example of integrity constraints in E-R model
o Key declaration: candidate key, primary key
o Form of relationship : mapping cardinalities: one to one, one to many etc
• In database management system we can enforce any arbitrary predicate as integrity
constraints but it adds overhead to the database system so its cost should be evaluated,
as far as possible integrity constraint should with minimal overhead.
o Set of all possible values for attribute known as its domain. Domain constraints enforce
attribute should hold only particular types of attributes. A domain of possible values
should be associated with every attribute. Domain constraints are the most elementary
form of integrity constraint. It is tested by database system whenever a new data item is
entered into database. System test values inserted in the database and test queries to
ensure that the comparisons make sense.
The clause constraint value-test is optional but useful to indicate which constraint an
update violated.
©T Paneru 1
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
Example 2:
•
create domain AccountType char(10)
constraint account-type-test
check (value in (‘Checking’, ‘Saving’))
Example 3:
• Referential integrity is a condition which Ensures that a value that appears in one
relation for a given set of attributes also appears for a certain set of attributes in
another relation.
Example
If “B1” is a branch name appearing in one of the tuples in the account relation, then
there exists a tuple in the branch relation where “B1” exist for branch name attribute.
.
Example:
Consider two relation department and employee as follows
department(deptno#,dname)
employee(empno#,ename,deptno)
• Deletion of particular department from department table also need to delete records of
employees they belongs to that particular department or delete need not be allow if
there is any employee that is associated to that particular department that we are
going to delete.
• Any update made in deptno in department table deptno in employee must be updated
automatically.
• This implies primary key acts as a referential integrity constraint in a relation.
Formal Definition
• Let r1(R1) and r2(R2) be relations with primary keys K1 and K2 respectively.
The subset α of R2 is a foreign key referencing K1 in relation r1, if for every t2 in r2
there must be a tuple t1 in r1 such that t1[K1] = t2[α].
• Referential integrity constraint also called subset dependency since its can be
written as
∏α (r2) ⊆ ∏K1 (r1)
• Consider relationship set R between entity sets E1 and E2. The relational schema
for R includes the primary keys K1 of E1 and K2 of E2. Then K1 and K2 form foreign
keys on the relational schemas for E1 and E2 respectively that leads referential
integrity constraint.
• Weak entity sets are also a source of referential integrity constraints. A weak entity
set must include the primary key attributes of the entity set on which it depends
©T Paneru 2
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
• The following tests must be made in order to preserve the following referential
integrity constraint:
∏α (r2) ⊆ ∏K (r1)
• Insert. If a tuple t2 is inserted into r2, the system must ensure that there is a tuple t1
in r1 such that t1[K] = t2[α]. That is
t2 [α] ∈ ∏K (r1)
• Delete. If a tuple, t1 is deleted from r1, the system must compute the set of tuples in
r2 that reference t1:
σα = t1[K] (r2)
• If this set is not empty
• either the delete command is rejected as an error, or
• the tuples that reference t1 must themselves be deleted
(cascading deletions are possible).
©T Paneru 3
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
Syntax
5.3 Assertion
• Example 1: sum of loan amounts for each branch is less than the sum of all account
balances at the branch.
©T Paneru 4
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
Example 2: every customer must have minimum balance 1000 in an account who are loan
holder
o When an assertion is created, the system tests it for validity. If the assertion is valid then
only allow further modification. if test found assertion is violated then it can not go ahead.
o Assertion testing may introduce a significant amount of overhead, especially if the assertions
are complex; hence assertions should be used with great care.
5.4 Trigger
Triggers re useful mechanism to perform certain task automatically when certain condition/s met.
Sometime trigger is also called rule or action rule.
©T Paneru 5
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
emp(empno,ename,sal)
emp_log(empno,ename,sal,operation_perform,userid,opr_date
v_empno:=old.empno;
v_ename:=old.ename;
v_sal:=old.sal;
insert into emp_log values(v_empno,v_ename,v_sal,oper,user,sysdate);
end;
Example 2:
o Suppose that instead of allowing negative account balances, the bank deals with overdrafts by
o setting the account balance to zero
o creating a loan in the amount of the overdraft providing same loan number as a
account number of the overdrawn account
©T Paneru 6
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
• Functional dependencies are constraints on the set of legal relations. It defines attributes
of relation, how they are related to each other.
• It determines unique value for a certain set of attributes to the value for another set of
attributes that is functional dependency is a generalization of the notation of key.
• Functional dependencies are interrelationship among attributes of a relation.
Definition:
For a given relation R with attribute X and Y, Y is said to be functionally dependent on X, if given
value for each X uniquely determines the value of the attribute in Y. X is called determinant of
the functional dependency (FD) and functional dependency denoted by X→ Y.
Supplier(supplier_id#,sname,status,city)
Here, sname, status and city are functionally dependent on supplier_id. Meaning is that each
supplier id uniquely determines the value of attributes supplier name,supplier status and city This
can be express by
Supplier.supplier_id→supplier.sname
Supplier.supplier_id→supplier.status
Supplier.supplier_id→supplier.city
Or simply,
supplier_id→ sname
supplier_id→ status
supplier_id→city
Valid case
sname status
X Good
Y Good
Invalid case
sname status
X Good
Y Good
X Bad
©T Paneru 7
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
Student-info(name#,course#,phone_no,major,prof,grade)
FD X→Y is satisfied on relation R if the cardinality of ∏Y( σ x=x(r)) is at most one. That is if, two
tuples ti and tj of R have the same X value then the corresponding value of Y must identical.
Consider a relation r
A B C D
a1 b1 c1 d1
a1 b2 c1 d2
t1
a2 b2 c2 d2
t2
a2 b2 c2 d3
a3 b3 c2 d4
©T Paneru 8
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
For a given relation schema R, FD X→Y, Y is said to be fully functionally dependent on X if there
is no Z (where Z is a proper subset of X) such that Z→Y.
o Here, the FDs CD→E, C→E where E is functionally dependent on CD and again E is
functionally dependent on subset of CD. That is C (i.e. C→E). Hence E is not fully
functionally dependent on CD.
Sales (product_id#,sales_date#,quantity,product_name)
With the following functional dependencies
For a given relation schema R with set of functional dependency F on attribute of R. Let K as a
candidiate key in R. if X is a proper subset of K and X and X→A then A is said to be partially
dependent on K.
student_course_info(name#,course#,grade,phone_no,major,course_department)
{name→phone_no,major
course→course_department,
name,course→grade
}
©T Paneru 9
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
Transitive dependency
For a given relational schema R with set of functional dependency F. Let X and Y be the subset of
r anf Let A be the attribute of R s.t. X ⊄ Y, A ⊄ XY. If the functional dependencies {X→Y, Y→A}
implies by F (i.e. X→Y→A) then A is said to be transitively dependent on X.
Example:
Let us consider relational schema ‘prof_info’
prof_info=(prof_name#,department_name, head_of_department)
Example:
Let R=(A,B,C,D,E) and FDs F={AB→C,B→D,C→E}
Here AB act a candidate key and E is transitively dependent on the key AB, ince AB→C→E).
For a given set of functional dependencies F, there are certain other functional dependencies that
are logically implies by F. (i.e. if A→B and B→C, then we can write A→C). the set of all functional
dependencies logically implies F is the closure of F. Closure of F is denoted by F+.
©T Paneru 10
Downloaded from: www.csitsansar.com
5.0 Integrity Constraints
A→C,CG→H,CG→I,CG→HI,B→H,A→H,
AG→I,CG→Hi
}
here , first six FDs obtain by reflexive axiom.
We can further simplify the the computation of F+ by using the following addition rule.
The closure of X under a set of functional dependencies F, written as X+, is the set of attributes
{A1,A2, . . Am} such that the FD X→Ai for Ai ∈ X+ follows from F by the inference axioms for
functional dependencies.
Example:
• initialize X+:=BCD.
• Since left hand side of the FD CD→E is a subset of X+ (i.e CD ⊆ X+), X+ is augmented by
the right hand side of the FD (i.e. E) thus now X+:=BCDE.
• Similarly, D ⊆ X+, the right hand side of the FD D→AEH is added to X+. Hence now
X+:=ABCDEH.
• Now X+ can not be augmented any further because no FDs left hand side is subset of X+.
1. Testing superkey
To test α is a superkey we compute α + and check whether α + contains all attributes of R. if
so α is a superkey, otherwise not.
©T Paneru 11
Downloaded from: www.csitsansar.com