Mysql Constraints
Mysql Constraints
CONSTRAINT
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.
CONSTRAINT DESCRIPTION
NOT NULL In MySQL NOT NULL constraint allows to specify that a column can not contain any NULL
value. MySQL NOT NULL can be used to CREATE and ALTER a table.
UNIQUE The UNIQUE constraint in MySQL does not allow to insert a duplicate value in a column.
The UNIQUE constraint maintains the uniqueness of a column in a table. More than one
UNIQUE column can be used in a table.
PRIMARY KEY A PRIMARY KEY constraint for a table enforces the table to accept unique data for a
specific column and this constraint creates a unique index for accessing the table faster.
FOREIGN KEY A FOREIGN KEY in MySQL creates a link between two tables by one specific column of
both tables. The specified column in one table must be a PRIMARY KEY and referred by the
column of another table known as FOREIGN KEY.
CHECK A CHECK constraint controls the values in the associated column. The CHECK constraint
determines whether the value is valid or not from a logical expression.
DEFAULT In a MySQL table, each column must contain a value ( including a NULL). While inserting
data into a table, if no value is supplied to a column, then the column gets the value set as
DEFAULT.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
Syntax:
CREATE TABLE [table name]
([column name] [data type]([size]) [column constraint]….
[table constraint] ([[column name]……])……);
Explanation:
The above MySQL code shows how to create a table with some
constraints. Constraints are applied to columns as well as
tables.
You can replace table name, column name, data type and size
with your own.
Contents:
Example:
Copy
Here in the above statement the constraint 'NOT NULL' have
been used to exclude the NULL VALUE.
The following picture shows that the columns will not accept
the NULL values.
Example:
book_name varchar(50) ,
aut_id varchar(8) ,
pub_id varchar(8) ,
dt_of_pub date ,
pub_lang varchar(15) ,
no_page decimal(5,0)
CHECK(no_page>0) ,
book_price decimal(8,2) ,
);
Copy
Here in the above MySQL statement will create a table
'newbook_mast' with a PRIMARY KEY on 'book _id' column,
unique constraint on 'isbn_no' column and adding
CHECK(no_page>0) will set the no_page in such, that it would
hold values more than zero only.
Example:
Copy
Here in the above MySQL statement will create a table
'newauthor' with a PRIMARY KEY on a combination of two
columns (aut_id,home_city) and the value for the column
country has been limited by using IN operator.
Example:
book_name varchar(50) ,
cate_id varchar(8) ,
aut_id varchar(8) ,
pub_id varchar(8) ,
pub_lang varchar(15) ,
book_price decimal(8,2) ,
Copy
MySQL CREATE TABLE with AND and OR operator and CHECK
CONSTRAINT
Example
(pub_id varchar(8) ,
pub_name varchar(50),
pub_city varchar(25) ,
country varchar(25) ,
country_office varchar(25) ,
no_of_branch int(3),
estd date
Copy
MySQL UNIQUE CONSTRAINT
Example:
aut_name varchar(50)
NOT NULL,
home_city varchar(25)
NOT NULL,
UNIQUE (aut_id));
Copy
The picture below shows the structure of the table.
country varchar(25)
NOT NULL,
Copy
The following picture shows the Structure of the Table
MySQL CREATE TABLE with DEFAULT CONSTRAINT
Example:
The MySQL statement also sets the default value white space
for pub_id, pub_name, pub_city columns and 'India' as a default
value for a country column.
country_office varchar(25) ,
no_of_branch int(3),
estd date
Copy
MySQL CREATE TABLE with AUTO INCREMENT
Example:
aut_id varchar(8),
aut_name varchar(50),
country varchar(25),
Copy
MySQL PRIMARY KEY CONSTRAINT
Example:
Copy
MySQL CREATE TABLE PRIMARY KEY CONSTRAINT on single
column
Example:
country varchar(25)
NOT NULL,
Example:
Copy
MySQL CREATE TABLE PRIMARY KEY on multiple columns
Example:
Copy
MySQL creating table with FOREIGN KEY CONSTRAINT
Syntax:
FOREIGN KEY [column list] REFERENCES [primary key table] ([column list]);
Arguments:
Name Description
REFERENCES Keyword.
primary key table Table name which contains the PRIMARY KEY.
column list A list of the columns on which PRIMARY KEY is set in the primary key table.
Example:
book_name varchar(50) ,
cate_id varchar(8) ,
aut_id varchar(8) ,
pub_id varchar(8) ,
dt_of_pub date ,
pub_lang varchar(15) ,
no_page decimal(5,0) ,
book_price decimal(8,2) ,
Copy
MySQL CREATE TABLE with FOREIGN KEY CONSTRAINT on
multiple columns
Example:
invoice_dt date ,
ord_no varchar(25) ,
ord_date date ,
receive_dt date ,
book_id varchar(8) ,
book_name varchar(50) ,
pub_lang varchar(8) ,
cate_id varchar(8) ,
receive_qty int(5) ,
purch_price decimal(12,2) ,
total_cost decimal(12,2) ,
INDEX (ord_no,book_id),
INDEX (cate_id),
Copy
MySQL CREATE TABLE with FOREIGN KEY CONSTRAINT on
multiple tables
Example:
book_name varchar(50) ,
cate_id varchar(8),
aut_id varchar(8) ,
pub_id varchar(8) ,
dt_of_pub date ,
pub_lang varchar(15) ,
no_page decimal(5,0) ,
book_price decimal(8,2) ,
INDEX (aut_id),
INDEX(pub_id),
Copy
MySQL CREATE TABLE with CASCADE and RESTRICT
Example:
invoice_dt date ,
ord_no varchar(25) ,
ord_date date ,
receive_dt date ,
book_id varchar(8) ,
book_name varchar(50) ,
pub_lang varchar(8) ,
cate_id varchar(8) ,
receive_qty int(5) ,
purch_price decimal(12,2) ,
total_cost decimal(12,2) ,
INDEX (ord_no,book_id),
neworder(ord_no,book_id)
INDEX (cate_id),
Copy
MySQL CREATE TABLE with SET NULL
Example:
invoice_dt date ,
ord_no varchar(25) ,
ord_date date ,
receive_dt date ,
book_id varchar(8) ,
book_name varchar(50) ,
pub_lang varchar(8) ,
cate_id varchar(8) ,
receive_qty int(5) ,
purch_price decimal(12,2) ,
total_cost decimal(12,2) ,
INDEX (ord_no,book_id),
(ord_no,book_id)
SET NULL,
INDEX (cate_id),
Copy
MySQL CREATE TABLE with NO ACTION
Example:
invoice_dt date ,
ord_no varchar(25) ,
ord_date date ,
receive_dt date ,
book_id varchar(8) ,
book_name varchar(50) ,
pub_lang varchar(8) ,
cate_id varchar(8) ,
receive_qty int(5) ,
purch_price decimal(12,2) ,
total_cost decimal(12,2) ,
INDEX (ord_no,book_id),
neworder(ord_no,book_id)
INDEX (cate_id),