Skip to main content

SQLSERVER_8: Add Constraints after creation of table

Sometimes we forget to include constraints while a table is created. So SQL Server provides mechanism to include constraints in an already existing table. The following commands shows how to do the same:

Alter table EmpTemp add constraint Salary_check check (salary between 1000 and 5000)

In the above command we are adding a constraint “Salary_check” on table “EmpTemp” and on column “Salary” i.e. its value between 1000 and 5000 only.

Below are some commands to add constraints to an existing table.


1.  Alter table temp1 add Constraint Salary_check unique(salary)
2.  Alter table temp1 add constraint salary_primary primary key (salary)

3.  Alter table temp1 add constraint fk_dept foreign key (Deptno) references dept(deptno)

Comments