SQL constraints
SQL constraints
1.NOT NULL
NOT NULL contraint will not allow to hold null values in the table column
syntax:
create table table_name(column_name NOT NULL);
Example:
create table mobiles(sid int not null,name varchar(20));
2.DEFAULT
DEFAULT constraint provides a default value to a column if no value is
inserted into the column
syntax:
create table table_name(column_name DEFAULT value);
Example:
create table mobiles(sid int ,name varchar(20) default 'chinaset');
3.UNIQUE
UNIQUE constraint does not allow for duplicate values in a column
syntax:
create table table_name(column_name UNIQUE);
Example:
create table mobiles(sid int unique ,name varchar(20) );
4.PRIMARY KEY
PRIMARY KEY constraint is used to identify unique row in a table.
PRIMARY KEY and FOREIGN KEY are used to provide relations between
tables
syntax:
create table table_name(column_name PRIMARY KEY);
Example
create table mobiles(sid int primary key ,name varchar(20) );
5.FORIEGN KEY
A FOREIGN KEY column value of one table refers to the PRIMARY KEY
column value in another table
The table with the foreign key is called the child table, and the table with the
primary key is called the parent table.
syntax:
create table table_name(column_name foreign key(column_name) references
primarykeytable_name(column_name);
Example:
create table accessories(ano int,aname varchar(20) ,foreign key (ano) references
mobiles(sid));
6.CHECK
The CHECK Constraint will check the value being entered into a row according
to condition provided
syntax:
create table table_name(column_name CHECK(condition));
Example:
create table mobiles(sid int check(sid >10),name varchar(20) );