Monday 11 April 2011

DDL : Creating a constraint

Contraints are used to define primary keys, enforce uniqueness, and to describe
foreign key relationships. Note that unique or primary key constraints
create indexes upon creation.


/* primary key for the employee table */
alter table employee add constraint
emp_constr primary key(emp_id)
go

/* add unique requirement for invoice table */
alter table invoice add constraint
inv_constr unique nonclustered(cust_id,inv_date)
go

/* add foreign key for relationship between invoice and employee */
alter table invoice add constraint inv_fk_emp
foreign key (sales_rep_id)
references employee(emp_id)
go

No comments:

Post a Comment