Constraints

Constraints protect the database from errors. It puts restrictions on database state.

Types of Constraints in Relational Model

  • Domain Constraint
  • Key Constraint
  • Entity Integrity Constraint
  • Referential Integrity Constraint 
Types of Constraints that cannot be captured in relational model
  • Semantic Constraints (Business Rules)
                       eg : Buy one get one free


Domain Constraint

Value of each attribute in a tuple must be atomic (no repeating values) and consistent with its domain(data type). It could be null if allowed.



Key Constraint


key constraint specifies that the values in the column declared as a primary key must be unique.

Types of keys
  • Candidate Key
           A relation has several keys referred to as candidate keys.but one candidate key is unique. it is               called the primary key.
  • Primary Key
          An attribute that uniquely identified the each row in a table.
  • Composite Key
          Two or more attribute that uniquely identified the each row in a table.
  • Foreign Key
          Set of columns in one table that serves as a primary key in another table.
  • Recursive Foreign Key
          A foreign key in a table that references the primary key values in that same table.
  • Super Key
          Every Key is a super key. But not Vice versa.

          (LicenseNumber , Model , Brand ) - This is a super key. but this is not a key.
          
           LicenseNumber  -  This is a super key and also a key

Properties of a key
  • uniqueness
  • minimality
name   id    amount

  A        1     1000
  B        2     2000
  C        3     1000

name can be a key. id can be a key.
[name , id] can be a key. It is called composite key.
But, it is not minimal. Because you can remove one column from it and still have a key.

amount is not a key. [amount , id] is a key. But it is not minimal.


Entity Integrity Constraint

Entity integrity constraint specifies that the primary key cannot be null.



Referential Integrity Constraint 

Tuples in the referencing relation(relation with a foreign key) must refer to tuples that exist in the referenced relation

Foreign Key and Primary Key are attributes that have the same domain although not necessarily the same attribute name.

        Professor (id , Name , DepID)
        Department (id , DName , Location)

        Professor(DepID) REFERENCES Department(id)

Possible Violations for INSERT and UPDATE
  • Domain Constraint
  • Key Constraint
  • Entity Integrity Constraint
  • Referential Integrity Constraint 
Possible Violations for DELETE
  • Referential Integrity Constraint 







Comments

Post a Comment

Popular posts from this blog

Normalization

Database Types

Entity Relationship Diagram (Pat II)