0% found this document useful (0 votes)
3 views

SQL constraints

SQL constraints are rules applied to table columns to enforce data integrity. Key constraints include NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK, each serving specific purposes such as preventing null values, ensuring default values, and maintaining unique or relational data. Examples are provided for each constraint to illustrate their usage in table creation.

Uploaded by

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

SQL constraints

SQL constraints are rules applied to table columns to enforce data integrity. Key constraints include NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK, each serving specific purposes such as preventing null values, ensuring default values, and maintaining unique or relational data. Examples are provided for each constraint to illustrate their usage in table creation.

Uploaded by

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

SQL constraints:

Constraints are the rules applied on the data columns of a table.

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) );

You might also like