0% found this document useful (0 votes)
27 views4 pages

12 (18-1) SQL Constraints

Uploaded by

ballabhkharvesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

12 (18-1) SQL Constraints

Uploaded by

ballabhkharvesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Constraints

Constraints can be specified when the table is created with the CREATE TABLE
statement, or after the table is created with the ALTER TABLE statement.
Types of SQL Constraints:

1. Primary Key 2. Unique 3. NOT NULL


4. DEFAULT 5. CHECK 6. Foreign Key

1. Primary Key Constraint:

a. A Primary Key must contain unique value


b. It must not contain a null value.

Example using PRIMARY KEY constraint

(i). CREATE table Student (sid int PRIMARY KEY, Name varchar(60),Age int);

(ii). ALTER TABLE Persons ADD PRIMARY KEY (ID);


2. UNIQUE Constraint:
a. UNIQUE constraint ensures that a field or column will only have unique
values.
b. A UNIQUE constraint field can have NULL value.

Example using UNIQUE constraint :


(i). CREATE table Student(sid int UNIQUE, Name varchar(60), Age int);
(ii). ALTER TABLE Persons ADD UNIQUE (ID);

3. NOT NULL Constraint:


a. A NOT NULL constraint field can not have NULL value.
Once *NOT NULL* constraint is applied to a column, you cannot store null value
to that column. It enforces a column to contain a proper value.

Example using NOT NULL constraint


(i). CREATE table Student(sid int NOT NULL, Name varchar(60), Age int);
(ii). ALTER TABLE Persons MODIFY Age int NOT NULL;
4. DEFAULT Constraint:
When no value is given for the specified field having a default constraint,
automatically the default value will be assigned to the field.

Example using DEFAULT constraint :

(I). CREATE TABLE Persons (ID int, LastName varchar(255), FirstName varchar(25),
Age int, City varchar(25) DEFAULT ‘Delhi');

(II). ALTER TABLE Persons MODIFY City DEFAULT ‘Delhi';

5. CHECK Constraint:
CHECK constraint is used to restrict the value of a column between a range. It
performs check operation on the values, before storing them into the database.

Example using CHECK constraint :

(i). create table Student(sid int CHECK(sid > 0), Name varchar(60), Age int);

(ii). ALTER TABLE Persons ADD CHECK (Age>=18);


6. FOREIGN KEY Constraint:
A FOREIGN KEY is a field in one table, that refers to the PRIMARY KEY in another
table.

The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.

Example using CHECK constraint :

PARENT (BASE) TABLE: PERSON


CHILD (SECOND) TABLE: ORDER

(i). CREATE TABLE Orders (OrderID int PRIMARY KEY, OrderNumber int, PersonID int
FOREIGN KEY REFERENCES Persons(PersonID));

You might also like