Integrity Constraints in DBMS
Integrity Constraints in DBMS
In DBMS, Integrity constraints can be defined as a set of rules that are used to maintain the information’s
quality. This ensures that the data integration is not affected at all by data insertion, updation or other
processes. Hence integrity constraints are like insurance to guard the database if there is any accidental
damage.
1. Domain constraint
2. Entity integrity constraint
3. Referential integrity constraint
4. Key constraint
Domain constraint
In DBMS, Domain constraints can be defined as a set of values that are valid for an attribute. The domain’s
data type includes string, character, integer, time etc. The value must be in the corresponding domain of the
attribute.
Example
Name Semester Age
Adarsh 1st 19
Name Semester Age
Kushal 4th 20
Akash 3rd 20
Vishal 7th H
Now in the table above, we can observe in the column Age, the data type of the domain is an integer but the
attributes data type is a character.
Example
S_No Name Semester Age
1 Adarsh 1st 19
2 Kushal 4th 20
3 Akash 3rd 20
Vishal 7th 21
In the above example, we can see, that in the S_No column and last tuple, the attribute is null, so it can not
be assigned as the primary key.
TABLE 1
S_No Name Semester Code
TABLE 2
Code City
4178 Patna
2451 Delhi
9654 Mumbai
1258 Pune
Now in the example above, we can see that in table 1, Code 5758 is not valid, as this attribute is not defined
in table 2 and also the attribute is assigned as the primary key, and also Code in table 1 is assigned the
foreign key.
Key constraint
In DBMS, a key is used to uniquely identify an entity in an entity set. An entity set can have multiple keys
but out only one can be a primary key. This primary key should not be null and must be unique. Although it
can contain a null and a non-null unique value.
Example
S. No Name Semester Code
In the example above, S_No can not be defined as a primary key because it contains a duplicate value. All
the rows must be unique.
• Constraints or nothing but the rules that are to be followed while entering data into columns of the
database table
• Constraints ensure that data entered by the user into columns must be within the criteria specified by the
condition
• For example, if you want to maintain only unique IDs in the employee table or if you want to enter only
age under 18 in the student table etc
• We have 5 types of key constraints in DBMS
o NOT NULL: ensures that the specified column doesn’t contain a NULL value.
o UNIQUE : provides a unique/distinct values to specified columns.
o DEFAULT: provides a default value to a column if none is specified.
o CHECK :checks for the predefined conditions before inserting the data inside the table.
o PRIMARY KEY: it uniquely identifies a row in a table.
o FOREIGN KEY: ensures referential integrity of the relationship
Not Null
• Null represents a record where data may be missing data or data for that record may be optional
• Once not null is applied to a particular column, you cannot enter null values to that column and
restricted to maintain only some proper value other than null
• A not-null constraint cannot be applied at table level
Example
• In the above example, we have applied not null on three columns ID, name and age which
means whenever a record is entered using insert statement all three columns should contain a value
other than null
• We have two other columns address and salary, where not null is not applied which means that you
can leave the row as empty or use null value while inserting the record into the table
Unique
• Sometimes we need to maintain only unique data in the column of a database table, this is possible by
using a unique constraint
• Unique constraint ensures that all values in a column are unique
Example
DEFAULT
The following SQL sets a DEFAULT value for the “city” column when the “emp” table is created:
My SQL / SQL Server / Oracle / MS Access:
CREATE TABLE emp (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'hyderabad'
);
• As a result, whenever you insert a new row each time you need not enter a value for this default
column that is entering a column value for a default column is optional and if you don’t enter the
same value is considered that is used in the default clause
Check
• Suppose in real-time if you want to give access to an application only if the age entered by the user is
greater than 18 this is done at the back-end by using a check constraint
• Check constraint ensures that the data entered by the user for that column is within the range of
values or possible values specified.
Example for check constraint
• As we have used a check constraint as (Age>=18) which means values entered by the user for this
age column while inserting the data must be less than or equal to 18 otherwise an error is shown
• Simply, the only possible values that the age column will accept is [0 -17]
Primary Key
A primary key is a constraint in a table that uniquely identifies each row record in a database table by
enabling one or more the columns in the table as the primary key.
A particular column is made as a primary key column by using the primary key keyword followed with the
column name
Foreign Key
• The foreign key a constraint is a column or list of columns that points to the primary key column of
another table
• The main purpose of the foreign key is only those values are allowed in the present table that will
match the primary key column of another table.
Reference Table
Child Table