0% found this document useful (0 votes)
15 views33 pages

6 MySql Constraints

Uploaded by

shashwatk862
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)
15 views33 pages

6 MySql Constraints

Uploaded by

shashwatk862
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/ 33

Structured

Query
Language
CONSTRAINT
S
Constraints are the rules
that we can apply on the
type of data in a table.
That is, we can specify the
limit on the type of data
that can be stored in a
particular column in a
table using constraints.
MySQL CONSTRAINT
is used to define rules to
allow or restrict what
values can be stored in
columns. The purpose of
inducing constraints is to
enforce the integrity of a
database. MySQL
CONSTRAINTS are used to
Constraints used
in MySql
1. Unique 2. Not Null
3. Default 4. Primary Key
5. Foreign Key
UNIQUE: This
constraint when
specified with a column,
tells that all the values in
the column must be
unique. That is, the
values in any row of a
column must not be
UNIQUE

Create table ak
(roll int(2)
unique,
Name char(10));
UNIQUE

Desc ak;
UNIQUE
Insert into ak
values(1,“amit”); Ok
Insert into ak
values(2,“amit”); Ok
Insert into ak
values(1,“sumit”);
Error
NOT NULL: This
constraint tells that we
cannot store a null
value in a column. That
is, if a column is
specified as NOT NULL
then we will not be able
Not Null

Create table ak
(roll int(2),
Name char(10) not
null);
Not Null

Desc ak;
Not Null
DEFAULT: This
constraint
specifies a default
value for the
column when no
value is specified
DEFAULT:
Create table tk
(roll int(2),
Name char(10),
Class char(5) default
“xii”);
DEFAULT
DEFAULT
PRIMARY KEY: A
primary key is a field
which can uniquely
identify each row in a
table. And this
constraint is used to
specify a field in a table
PRIMARY KEY:
Create table ak
(roll int(2) primary key,
Name char(10));
PRIMARY KEY:
PRIMARY KEY:
FOREIGN KEY: A
Foreign key is a field
which can uniquely
identify each row in a
another table. And
this constraint is used
to specify a field as
FOREIGN KEY:
Create table ak
(roll int(2) primary key,
Name char(10));

Create table bk
(Regno int(2),
Phy int(2),
Foreign key(Regno) references
Table
Constraints
Create table ak
(Roll int(2),
Regno int(2),
Name char(10),
CHECK: This constraint helps to validate the
values of a column to meet a particular
condition. That is, it helps to ensure that the
value stored in a column meets a specific
condition.
Auto_Increment allows a
unique number to be
generated automatically
when a new record is
inserted into a table.
This is the primary key
field that we would like to
be created automatically
every time a new record is
By default the
starting value for
AUTO_INCREME
NT is 1, and it
will increment by
1 for each new
To let
AUTO_INCREME
NT sequence
start with another
value, use
another SQL
To be
continued
after
ALTER
reate Table Data
Roll int(2) Auto_increment Primary k
har name(10));
nsert into Data (name
Values (“Amit”);
nsert into Data (name
Values (“Sumit”);
nsert into Data (name
Values (“Ajay”);
Select * from Data;
Roll Name

1 Amit
2 Sumit
3 Ajay

You might also like