You are on page 1of 12

SQL

CONSTRAINTS

With Alter table and Update commands…..


SQL • SQL constraints are used to specify rules for data in a
table.

constraints • Constraints are used to limit the type of data that can
go into a table. This ensures the accuracy and reliability
of the data in the table. If there is any violation
between the constraint and the data action, the action
is aborted.
• Constraints can be column level or table level. Column
level constraints apply to a column, and table level
constraints apply to the whole table.
• 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.
2
09/05/2022
SQL • NOT NULL - Ensures that a column cannot have a
NULL value

Constraints • UNIQUE - Ensures that all values in a column are


different
types • PRIMARY KEY - A combination of a NOT NULL and
UNIQUE. Uniquely identifies each row in a table
• FOREIGN KEY - Uniquely identifies a row/record in
another table
• CHECK - Ensures that all values in a column satisfies a
specific condition
• DEFAULT - Sets a default value for a column when no
value is specified
• INDEX - Used to create and retrieve data from the
3
09/05/2022
database very quickly
Syntax CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

4
09/05/2022
SQL NOT • By default, a column can hold NULL values.

NULL • The NOT NULL constraint enforces a column to NOT


accept NULL values.
Constraint
• This enforces a field to always contain a value, which
means that you cannot insert a new record, or update a
record without adding a value to this field.

5
09/05/2022
SQL NOT • The following SQL ensures that the "ID", "LastName",
and "FirstName" columns will NOT accept NULL values

NULL on when the "Persons" table is created:

CREATE CREATE TABLE Persons (

TABLE ID int NOT NULL,


LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);

6
09/05/2022
SQL NOT • To create a NOT NULL constraint on the "Age" column
when the "Persons" table is already created, use the

NULL on following SQL:

ALTER ALTER TABLE Persons

TABLE MODIFY Age int NOT NULL;

7
09/05/2022
SQL • The UNIQUE constraint ensures that all values in a
column are different.

UNIQUE • Both the UNIQUE and PRIMARY KEY constraints


Constraint provide a guarantee for uniqueness for a column or set
of columns.

• A PRIMARY KEY constraint automatically has a


UNIQUE constraint.

• However, you can have many UNIQUE constraints per


table, but only one PRIMARY KEY constraint per table.
8
09/05/2022
SQL • The following SQL creates a UNIQUE constraint on the
"ID" column when the "Persons" table is created:

UNIQUE
Constraint on CREATE TABLE Persons (

CREATE ID int NOT NULL UNIQUE,


LastName varchar(255) NOT NULL,
TABLE FirstName varchar(255),
Age int
);

9
09/05/2022
UNIQUE • To name a UNIQUE constraint, and to define a UNIQUE
constraint on multiple columns, use the following SQL

constraint on syntax:
CREATE TABLE Persons (
multiple ID int NOT NULL,

columns LastName varchar(255) NOT NULL,


FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);

10
09/05/2022
SQL • To create a UNIQUE constraint on the "ID" column
when the table is already created, use the following

UNIQUE SQL:
ALTER TABLE Persons
Constraint on ADD UNIQUE (ID);

ALTER • To name a UNIQUE constraint, and to define a UNIQUE


constraint on multiple columns, use the following SQL

TABLE syntax:
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

11
09/05/2022
DROP a • To drop a UNIQUE constraint, use the following SQL:

UNIQUE
Constraint ALTER TABLE Persons
DROP CONSTRAINT UC_Person;

12
09/05/2022

You might also like