0% found this document useful (0 votes)
2 views16 pages

Constraints

Constraints in SQL are conditions that must be met for a relation to be valid, including NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and DOMAIN constraints. Each constraint serves a specific purpose, such as ensuring non-empty values, uniqueness, referential integrity, and default values for table columns. Domain constraints allow for user-defined data types combined with other constraints to enforce specific rules on data entries.

Uploaded by

rajajaniknd
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)
2 views16 pages

Constraints

Constraints in SQL are conditions that must be met for a relation to be valid, including NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and DOMAIN constraints. Each constraint serves a specific purpose, such as ensuring non-empty values, uniqueness, referential integrity, and default values for table columns. Domain constraints allow for user-defined data types combined with other constraints to enforce specific rules on data entries.

Uploaded by

rajajaniknd
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/ 16

Constraints

What are constraints ?


Constraints are nothing but
conditions.
Every relation has some
conditions that must hold for it to
be a valid relation.
Constraints available in
SQL
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
DOMAIN CONSTRAINTS
NOT NULL
NULL means empty i.e. the value
is not available OR not applicable.
Whenever a table's column is
declared as NOT NULL, then the
value for that column cannot be
empty for any of the table's
records.
There must exist a value in the
column to which the NOT NULL
constraint is applied.
NOT NULL(Contd…)
CREATE TABLE student(StudentID
INT NOT NULL, Student_FirstName
VARCHAR(20));
UNIQUE
Duplicate values are not allowed
in the columns to which the
UNIQUE constraint is applied.
The column with the unique
constraint will always contain a
unique value.
This constraint can be applied to
one or more than one column of a
table, which means more than
one unique constraint can exist
UNIQUE(Contd...)

CREATE TABLE student(StudentID


INT UNIQUE, Student_FirstName
VARCHAR(20));
PRIMARY KEY
PRIMARY KEY Constraint is a
combination of NOT NULL and
UNIQUE constraints.
NOT NULL constraint and a
UNIQUE constraint together forms
a PRIMARY constraint.
The column to which we have
applied the primary constraint will
not contain a duplicate value and
will not allow null values.
PRIMARY KEY(Contd.)
CREATE TABLE student(StudentID
INT PRIMARY KEY, Student_First
Name VARCHAR(20), Student_Las
tName VARCHAR(20));
If table doesn’t contain Primary
Key then we can add Primary Key
using
ALTER TABLE student ADD PRIMA
RY KEY (StudentID);
FOREIGN KEY
A foreign key is used for
referential integrity.
When we have two tables, and
one table takes reference from
another table, i.e., the same
column is present in both the
tables and that column acts as a
primary key in one table. That
particular column will act as a
foreign key in another table.
FOREIGN KEY(Contd…)
Primary key

Foreign key
CHECK
Whenever a check constraint is
applied to the table's column,
and the user wants to insert the
value in it, then the value will
first be checked for certain
conditions before inserting the
value into that column.
CHECK(Contd…)
CREATE TABLE student(StudentID
INT, Student_Name VARCHAR(20),
Age INT CHECK( Age <= 15));
If CHECK constraint is not given at
the time of creation we can add
using
ALTER TABLE student ADD CHEC
K ( Age <=15 );
DEFAULT
Whenever a default constraint is
applied to the table's column,
and the user has not specified
the value to be inserted in it,
then the default value which was
specified while applying the
default constraint will be inserted
into that particular column.
DEFAULT(Contd…)
CREATE TABLE student(StudentID
INT, Student_Name VARCHAR(20
), Student_Email_ID VARCHAR(40)
DEFAULT “[email protected]");
Domain Constraints
Domain constraints are user defined data type and we can define
them like this:
Domain Constraint = data type + Constraints (NOT NULL / UNIQUE /
PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)

Example: For example I want to create a table “student_info” with


“stu_id” field having value greater than 100, I can create a domain and
table like this:

create domain id_value int


check(value > 100);

create table student_info (


stu_id id_value PRIMARY KEY,
stu_name varchar(30),
stu_age int
);

You might also like