Database Constraints
Constraints are used to specify rules for the
data in a table.
 It enforces limits to the data or type of data
that can be inserted/updated/deleted from a
table.
Purpose
• The main purpose of constraints is to maintain
the data integrity during database operations
(update/delete/insert) into a table.
• The term data integrity simply means that the
data stored in the table is valid.
Levels of Constraints
• There are two levels where a constraint can be
defined:
1. Column Level- A column level constraint
references a single column and is defined along
with the definition of the column.
2. Table Level- A table level constraint references
one or more columns and is defined separately
from the definitions of the columns. Normally, it
is written after all columns are defined.
Types of constraints
• NOT NULL
• UNIQUE
• CHECK
• DEFAULT
• Key Constraints – PRIMARY KEY, FOREIGN KEY
NOT NULL Constraint
NOT NULL constraint makes sure that a column does not hold NULL
value. Normally, when we don’t provide value for a particular column
while inserting a record into a table, it takes NULL value by default.
But, by specifying NOT NULL constraint, we can be sure that a
particular column(s) cannot have NULL values.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (235),
PRIMARY KEY (ROLL_NO)
);
UNIQUE Constraint
UNIQUE Constraint enforces a column or set of
columns to have unique values. If a column has
a UNIQUE constraint, it means that particular
column cannot have duplicate values in a table.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
CHECK Constraint
CHECK constraint is used to restrict the value of a column
between a range. It performs check on the values, before storing
them into the database. Its like condition checking before saving
data into a column.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL CHECK(ROLL_NO >1000) ,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35)
PRIMARY KEY (ROLL_NO
);
DEFAULT Constraint
The DEFAULT constraint provides a default value
to a column when there is no value provided
while inserting a record into a table.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO)
);
Key Constraints
1. PRIMARY KEY:
• Uniquely identifies each record in a table. It must
have unique values and cannot contain nulls.
• In the below example the ROLL_NO field is marked as
primary key, that means the ROLL_NO field cannot
have duplicate and null values.
Ex:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
Key Constraints
2. FOREIGN KEY
• Foreign keys are the columns of a table that points to the primary
key of another table.
• It is used to relate two tables.
• It is also used to restrict actions that would destroy link between
table.
Ex:
CREATE table Order_Detail
(
order_id int PRIMARY KEY,
order_name varchar(60) NOT NULL,
c_id int FOREIGN KEY REFERENCES Customer_Detail(c_id)
);
In Customer_Detail table, c_id is the primary key which is set as foreign key
in Order_Detail table. The value that is entered in c_id which is set as foreign key
in Order_Detail table must be present in Customer_Detail table where it is set as
primary key. This prevents invalid data to be inserted into c_id column
of Order_Detailtable.
THANK YOU

Database constraints

  • 1.
    Database Constraints Constraints areused to specify rules for the data in a table.  It enforces limits to the data or type of data that can be inserted/updated/deleted from a table.
  • 2.
    Purpose • The mainpurpose of constraints is to maintain the data integrity during database operations (update/delete/insert) into a table. • The term data integrity simply means that the data stored in the table is valid.
  • 3.
    Levels of Constraints •There are two levels where a constraint can be defined: 1. Column Level- A column level constraint references a single column and is defined along with the definition of the column. 2. Table Level- A table level constraint references one or more columns and is defined separately from the definitions of the columns. Normally, it is written after all columns are defined.
  • 4.
    Types of constraints •NOT NULL • UNIQUE • CHECK • DEFAULT • Key Constraints – PRIMARY KEY, FOREIGN KEY
  • 5.
    NOT NULL Constraint NOTNULL constraint makes sure that a column does not hold NULL value. Normally, when we don’t provide value for a particular column while inserting a record into a table, it takes NULL value by default. But, by specifying NOT NULL constraint, we can be sure that a particular column(s) cannot have NULL values. Ex: CREATE TABLE STUDENT ( ROLL_NO INT NOT NULL, STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT NULL, STU_ADDRESS VARCHAR (235), PRIMARY KEY (ROLL_NO) );
  • 6.
    UNIQUE Constraint UNIQUE Constraintenforces a column or set of columns to have unique values. If a column has a UNIQUE constraint, it means that particular column cannot have duplicate values in a table. Ex: CREATE TABLE STUDENT ( ROLL_NO INT NOT NULL, STU_NAME VARCHAR (35) NOT NULL UNIQUE, STU_AGE INT NOT NULL, STU_ADDRESS VARCHAR (35) UNIQUE, PRIMARY KEY (ROLL_NO) );
  • 7.
    CHECK Constraint CHECK constraintis used to restrict the value of a column between a range. It performs check on the values, before storing them into the database. Its like condition checking before saving data into a column. Ex: CREATE TABLE STUDENT ( ROLL_NO INT NOT NULL CHECK(ROLL_NO >1000) , STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT NULL, EXAM_FEE INT DEFAULT 10000, STU_ADDRESS VARCHAR (35) PRIMARY KEY (ROLL_NO );
  • 8.
    DEFAULT Constraint The DEFAULTconstraint provides a default value to a column when there is no value provided while inserting a record into a table. Ex: CREATE TABLE STUDENT ( ROLL_NO INT NOT NULL, STU_NAME VARCHAR (35) NOT NULL, STU_AGE INT NOT NULL, EXAM_FEE INT DEFAULT 10000, STU_ADDRESS VARCHAR (35) , PRIMARY KEY (ROLL_NO) );
  • 9.
    Key Constraints 1. PRIMARYKEY: • Uniquely identifies each record in a table. It must have unique values and cannot contain nulls. • In the below example the ROLL_NO field is marked as primary key, that means the ROLL_NO field cannot have duplicate and null values. Ex: CREATE TABLE STUDENT ( ROLL_NO INT NOT NULL, STU_NAME VARCHAR (35) NOT NULL UNIQUE, STU_AGE INT NOT NULL, STU_ADDRESS VARCHAR (35) UNIQUE, PRIMARY KEY (ROLL_NO) );
  • 10.
    Key Constraints 2. FOREIGNKEY • Foreign keys are the columns of a table that points to the primary key of another table. • It is used to relate two tables. • It is also used to restrict actions that would destroy link between table. Ex: CREATE table Order_Detail ( order_id int PRIMARY KEY, order_name varchar(60) NOT NULL, c_id int FOREIGN KEY REFERENCES Customer_Detail(c_id) ); In Customer_Detail table, c_id is the primary key which is set as foreign key in Order_Detail table. The value that is entered in c_id which is set as foreign key in Order_Detail table must be present in Customer_Detail table where it is set as primary key. This prevents invalid data to be inserted into c_id column of Order_Detailtable.
  • 11.