0% found this document useful (0 votes)
10 views10 pages

Constraints in SQL

Uploaded by

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

Constraints in SQL

Uploaded by

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

CONSTRAINTS IN SQL

BY: SIRI
RAMIDI
CONSTRAINTS
 Sql constraints are used to specify rules for the data in a table.

 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.
The following constraints are commonly used in SQL:
NOT NULL constraint :
 The NOT NULL constraint enforces a column to NOT accept NULL values.
 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.
Example:
CREATE TABLE persons (
ID int NOT NULL,
lastname varchar(255) NOT NULL,
firstname varchar(255) NOT NULL,
age int
);
UNIQUE constraint:
 The UNIQUE constraint ensures that all values in a column are different.
 Both the unique and primary key constraints 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.

Example: The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is
created:

CREATE TABLE persons (


ID int NOT NULL UNIQUE,
lastname varchar(255) NOT NULL,
firstname varchar(255),
age int
);
PRIMARY KEY constraint:
 PRIMARY KEY constraint uniquely identifies each record in a table.
 Primary keys must contain unique values, and cannot contain null values.
 A table can have only one primary key; and in the table, this primary key can consist of single or

multiple columns (fields).


Example:
CREATE TABLE persons (
ID int NOT NULL,
lastname varchar(255) NOT NULL,
firstname varchar(255),
age int,
PRIMARY KEY (ID)
);
FOREIGN KEY constraint:
 The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
 A foreign key is a field (or collection of fields) 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.

• Look at the following two tables:

Persons table Orders


PersonI LastNa
Table
FirstNa Age OrderID OrderNum PersonID
D me me ber
1 Hansen Ola 30 1 77895 3
2 Svendso Tove 23 2 44678 3
n
3 22456 2
3 Petterse Kari 20
4 24562 1
n
 The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
 The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.

Example: The following SQL creates a FOREIGN KEY on the "personid" column when the "orders"
table is created:

CREATE TABLE orders (


orderid int NOT NULL,
ordernumber int NOT NULL,
personid int,
PRIMARY KEY (orderid),
FOREIGN KEY (personid) REFERENCES persons(personid)
);
CHECK constraint:
 The CHECK constraint is used to limit the value range that can be placed in a column.
 If you define a check constraint on a column it will allow only certain values for this column.
 If you define a check constraint on a table it can limit the values in certain columns based on values in
other columns in the row.
Example: The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is
created. The CHECK constraint ensures that the age of a person must be 18, or older:

CREATE TABLE persons (


ID int NOT NULL,
lastname varchar(255) NOT NULL,
firstname varchar(255),
age int,
CHECK (age>=18)
);
DEFAULT constraint:
 The DEFAULT constraint is used to set a default value for a column.
 The default value will be added to all new records, if no other value is specified.

Example: The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is
created:

CREATE TABLE persons (


ID int NOT NULL,
lastname varchar(255) NOT NULL,
firstname varchar(255),
age int,
city varchar(255) DEFAULT 'sandnes'
);
THANK YOU

You might also like