Integrity Constraints
Integrity Constraints
Integrity Constraints are used to apply business rules for the database tables.
The constraints available in SQL are Foreign Key, Not Null, Unique, Check.
1) The constraints can be specified immediately after the column definition. This is called column-
level definition.
2) The constraints can be specified after all the columns are defined. This is called table-level
definition.
This constraint defines a column or combination of columns which uniquely identifies each row in the
table.
• column_name1, column_name2 are the names of the columns which define the primary
Key.
For Example: To create an employee table with Primary Key constraint, the query would be like.
name char(20),
dept char(10),
age number(2),
location char(10)
);
or
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
( id number(5),
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
);
This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a
relationship between two columns in the same table or between different tables. For a column to be
defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring.
referenced_table_name(column_name);
For Example:
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
( order_id number(5) ,
product_id number(5),
product_name char(20),
supplier_name char(20),
unit_price number(10)
);
2) If the employee table has a 'mgr_id' i.e, manager id as a foreign key which references primary
key 'id' within the same table, the query would be like,
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
This constraint ensures all rows in the table contain a definite value for the column which is specified
For Example: To create a employee table with Null value, the query would be like
( id number(5),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
This constraint ensures that a column or a group of columns in each row have a distinct value. A
column(s) can have a null value but the values cannot be duplicated.
For Example: To create an employee table with Unique key, the query would be like,
name char(20),
dept char(10),
age number(2),
salary number(10),
);
or
name char(20),
dept char(10),
age number(2),
salary number(10),
);
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10),
);
This constraint defines a business rule on a column. All the rows must satisfy this rule. The
For Example: In the employee table to select the gender of a person, the query would be like
name char(20),
dept char(10),
age number(2),
salary number(10),
location char(10)
);
name char(20),
dept char(10),
age number(2),
gender char(1),
salary number(10),
location char(10),
);
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():