Wednesday, 6 August 2014

Relational Model | Integrity constraints over relation


INTEGRITY CONSTRAINTS OVER RELATION

INTRODUCTION
Database integrity refers to the validity and consistency of stored data. Integrity is usually expressed in terms of constraints, which are consistency rules that the database is not permitted to violate. Constraints may apply to each attribute or they may apply to relationships between tables.
Integrity constraints ensure that changes (update deletion, insertion) made to the database by authorized users do not result in a loss of data consistency. Thus, integrity constraints guard against accidental damage to the database.

EXAMPLE- A brood group must be ‘A’ or ‘B’ or ‘AB’ or ‘O’ only (can not any other values else).

TYPES OF INTEGRITY CONSTRAINTS

Various types of integrity constraints are-
  1. Domain Integrity
  2. Entity Integrity Constraint
  3. Referential Integrity Constraint
  4. Key Constraints
1. Domain Integrity- Domain integrity means the definition of a valid set of values for an attribute. You define data type, length or size, is null value allowed , is the value unique or not for an attribute ,the default value, the range (values in between) and/or specific values for the attribute. 

2. Entity Integrity Constraint- This rule states that in any database relation value of attribute of a primary key can't be null.

EXAMPLE- Consider a relation "STUDENT" Where "Stu_id" is a primary key and it must not contain any null value whereas other attributes may contain null value e.g  "Branch" in the following relation contains one null value.


Stu_id
Name
Branch
11255234
Aman
CSE
11255369
Kapil
ECE
11255324
Ajay

11255237
Raman
CSE
11255678
Aastha
ECE

3.Referential Integrity Constraint-It states that if a foreign key exists in a relation then either the foreign key value must match a primary key value of some tuple in its home relation or the foreign key value must be null.

The rules are:

1. You can't delete a record from a primary table if matching records exist in a related table.
2. You can't change a primary key value in the primary table if that record has related records.
3. You can't enter a value in the foreign key field of the related table that doesn't exist in the primary key of the primary table.
4. However, you can enter a Null value in the foreign key, specifying that the records are unrelated.

EXAMPLE-Consider 2 relations "stu" and "stu_1" Where "Stu_id " is the primary key in the "stu" relation and foreign key in the "stu_1" relation.

Relation "stu"

Stu_id
Name
Branch
11255234
Aman
CSE
11255369
Kapil
ECE
11255324
Ajay
ME
11255237
Raman
CSE
11255678
Aastha
ECE

Relation "stu_1"

Stu_id
Cource
Duration
11255234
B.TECH
4 years
11255369
B.TECH
4 years
11255324
B.TECH
4 years
11255237
B.TECH
4 years
11255678
B.TECH
4 years

Examples

Rule 1. You can't delete any of the rows in the ”stu­” relation  that are visible  since all the ”stu” are in use in the “stu_1” relation.
Rule 2. You can't change any of the ”Stu_id” in the “stu” relation since all the “Stu_id” are in use in the ”stu_1” relation.
Rule 3. The values that you can enter in the” Stu_id” field in the “stu_1” relation must be in the” Stu_id” field in the “stu” relation.
Rule 4 You can enter a null value in the "stu_1" relation if the records are unrelated.

4.Key Constraints- A Key Constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for a tuple.
There are 4 types of  key constraints-
  1. Candidate key.
  2. Super key
  3. Primary key
  4. Foreign key

7 comments: