0% found this document useful (0 votes)
3 views2 pages

SQL-4 CONSTRAINTS

The document outlines various database constraints including NOT NULL, UNIQUE, and PRIMARY KEY, detailing their definitions and syntax for implementation. It provides examples for creating and modifying tables with these constraints, as well as instructions for altering existing tables to add or remove constraints. The document serves as a guide for managing data integrity in relational databases.

Uploaded by

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

SQL-4 CONSTRAINTS

The document outlines various database constraints including NOT NULL, UNIQUE, and PRIMARY KEY, detailing their definitions and syntax for implementation. It provides examples for creating and modifying tables with these constraints, as well as instructions for altering existing tables to add or remove constraints. The document serves as a guide for managing data integrity in relational databases.

Uploaded by

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

Constraints:

-NOT NULL
-UNIQUE
-PRIMARY KEY
-CHECK
-DEFAULT

NOT NULL CONSTRAINT - That column ensures cannot haver a null values,

Syntax :
create table tablename(id int not null,name varchar(20),age int);

NOT NULL on Alter Table:


Syntax:
Alter table tablename modify columnname not null;
EX: alter table department modify age int not null;
DROP Command using remove the Not null constraint
Syntax:
alter table tablename modify columnname;
EX: alter table department modify id int;

UNIQUE - Ensures that all values in a column are different.

-Creat a UNIQUE value of ID column when the department table;


EX: create table department(id int,name varchar(20),age int,unique(id));
create table depart(id int,name varchar(20),age
int,unique(id),unique(name));

Unique Constraint on ALTER TABLE:


-to create a unique constraint on the "ID" column when the table is
already created.
Syntax:
Alter table tablename ADD UNIQUE(ID);
EX:alter table department add unique(age);

Primary Key - Combain of Not NULL and UNIQUE.


- the Primary key constraint uniquely identifies each record in a table.

Primary Key ON Creating a table.


Syntax:
create table tablename(id int not null,lastname varchar(20),firstname
varchar(10),age int,primary key(id));
EX:create table person(id int not null,lastname varchar(20),firstname
varchar(10),age int,primary key(id));
create table persons(id int not null,lastname varchar(20),firstname
varchar(10),age int,primary key(id,lastname));

Primary Key on Alter table:


Syntax:
alter table employee add primary key(id,name);

Drop a Primary key constraint:


Syntax:
Alter table Tablename Drop Primary Key;
EX: alter table employee drop primary key;

You might also like