Adding Constraint in a Existing Table – SQL Server Tutorial

Adding constraint in a existing table :-

  • Alter is used to add constraint in an existing table.

Example :- Create table emp88 (eno int, Ename varchar(20), Sal smallmoney, Dno int)

Adding primary key   :-

Syntax  : {for any constraint}

Alter table <tabname>

Add{constraint<name>}type(collist)

Note:- Before adding primary key constraint make

The column is not null column then add primary key.

                 Sp_help emp88

Example : alter table emp88, Alter column eno int not null -> make the column is not null

Example  :

Alter table emp88

Add primary key (eno)

Adding check constraint :-

Alter table emp88

Add constraint ck_sal_emp88

                      optional

Check(sal>3000) 

Adding foreign key   :-

Alter table emp88

         Add constraint fk_dno_emp88

Foreign key(dno)references dept22(dno) 

Leave a Reply

Your email address will not be published. Required fields are marked *