CONSTRAINTS
DEFINITION:
 Constraints are rules to limit data that can go into a table ,to maintain integrity and
accuracy of the data into table .
 Constraints are divided into following two types :
 1-table level constraints
 2-column level constraints
 Constraints are used to make sure integrity of data is maintained in database.
FOLLOWING ARE THE MOST USED CONSTRAINTS :
 Not null
 Primary key
 Unique key
 Foreign key
NOT NULL:
 By applying not null constraint you are declaring that a not null value cannot be added.
 Create table student (s-id int Not Null ,Name varchar (60),Age int);
The above query shows that s-id student table will not take any Null
value.
UNIQUE CONSTRAINT:
 A unique constraint that a field or column will only have unique values , no value will
repeat.
 There will not be any duplicate value , this constraint can be applied on column level or
table level.
 Create table student (s-id int NOT NULL UNIQUE, NAME varchar(60),Age int );
 The above query insures that a s-id of student table will only take unique value ,there
also wont be a NULL value.
 Using UNIQUE constraint after table is created (column level):
 ALTER TABLE student ADD UNIQUE (s-id);
PRIMARY KEY :
 Primary key constraint uniquely identifies each round in a database.
 A primary key must contain unique value and it must not contain null value.
 Usually primary key is used to index the data inside the table.
 Using primary key constraint at table level
 Create table student (s-id int primary key, Name varchar (60)NOT NULL , Age int)
 The above command will create a PRIMARY KEY on the s-id.
 Using PRIMARY KEY constraint at Column Level
 Alter table student add PRIMARY KEY (s-id);
 The above command will create a PRIMARY KEY on the s-id.
FOREIGN KEY:
 FOREIGN KEY is used to relate two tables.
 FOREIGN KEY constraint is also used to restrict actions that would destroy links
between two tables.
 To understand FOREIGN KEY, lets see its use, with help of below tables:
CUSTOMER-DETAIL TABLE :
C-id Customer name Address
101 Adam England
102 Alex France
103 Stuart Germany
ORDER-DETAIL TABLE :
Order-id Order-Name C-id
10 order1 101
11 order2 103
12 order3 102
 In customer- detail table c-id is a PRIMARY KEY which is set as FOREIGN
KEY in order- detail table .
 The value which 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.
USING FOREIGN KEY AT TABLE LEVEL :
create table Order-Detail(
order-id int PRIMARY KEY,
order-name varchar(60) NOT NULL,
c-id FOREIGN KEY REFERENCES Customer-Detail(c-id)
);
In this query ,c-id in the table order-detail is made as FOREIGN KEY ,which is a reference
of c-id column in customer-detail table.
USING FOREIGN KEY CONSTRAINT AT
COLUMN LEVEL:
 ALTER table order-Detail ADD FOREIGN KEY (c-id) REFERENCES customer-Detail(c-
id);

Constraints

  • 1.
  • 2.
    DEFINITION:  Constraints arerules to limit data that can go into a table ,to maintain integrity and accuracy of the data into table .  Constraints are divided into following two types :  1-table level constraints  2-column level constraints  Constraints are used to make sure integrity of data is maintained in database.
  • 3.
    FOLLOWING ARE THEMOST USED CONSTRAINTS :  Not null  Primary key  Unique key  Foreign key
  • 4.
    NOT NULL:  Byapplying not null constraint you are declaring that a not null value cannot be added.  Create table student (s-id int Not Null ,Name varchar (60),Age int); The above query shows that s-id student table will not take any Null value.
  • 5.
    UNIQUE CONSTRAINT:  Aunique constraint that a field or column will only have unique values , no value will repeat.  There will not be any duplicate value , this constraint can be applied on column level or table level.  Create table student (s-id int NOT NULL UNIQUE, NAME varchar(60),Age int );  The above query insures that a s-id of student table will only take unique value ,there also wont be a NULL value.  Using UNIQUE constraint after table is created (column level):  ALTER TABLE student ADD UNIQUE (s-id);
  • 6.
    PRIMARY KEY : Primary key constraint uniquely identifies each round in a database.  A primary key must contain unique value and it must not contain null value.  Usually primary key is used to index the data inside the table.  Using primary key constraint at table level  Create table student (s-id int primary key, Name varchar (60)NOT NULL , Age int)  The above command will create a PRIMARY KEY on the s-id.
  • 7.
     Using PRIMARYKEY constraint at Column Level  Alter table student add PRIMARY KEY (s-id);  The above command will create a PRIMARY KEY on the s-id.
  • 8.
    FOREIGN KEY:  FOREIGNKEY is used to relate two tables.  FOREIGN KEY constraint is also used to restrict actions that would destroy links between two tables.  To understand FOREIGN KEY, lets see its use, with help of below tables:
  • 9.
    CUSTOMER-DETAIL TABLE : C-idCustomer name Address 101 Adam England 102 Alex France 103 Stuart Germany
  • 10.
    ORDER-DETAIL TABLE : Order-idOrder-Name C-id 10 order1 101 11 order2 103 12 order3 102
  • 11.
     In customer-detail table c-id is a PRIMARY KEY which is set as FOREIGN KEY in order- detail table .  The value which 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.
  • 12.
    USING FOREIGN KEYAT TABLE LEVEL : create table Order-Detail( order-id int PRIMARY KEY, order-name varchar(60) NOT NULL, c-id FOREIGN KEY REFERENCES Customer-Detail(c-id) ); In this query ,c-id in the table order-detail is made as FOREIGN KEY ,which is a reference of c-id column in customer-detail table.
  • 13.
    USING FOREIGN KEYCONSTRAINT AT COLUMN LEVEL:  ALTER table order-Detail ADD FOREIGN KEY (c-id) REFERENCES customer-Detail(c- id);