Tuesday, December 15, 2009

SQL Server Constraints Alexander Chigrik

/*1)Tablename-C_ActiorType
fields--ActiorID--->Pk
--Name --->UK
2)Tablename-C_CreditsMatrix
fields--ActorID---->Fk of ActiorID in C_ActiorType
*/

/* settng the primary key constraint...Unique and not allow null*/
ALTER TABLE C_ActiorType
ADD CONSTRAINT pk_ActiorID PRIMARY KEY (ActiorID)
GO

/*setting the Unique key constraint.....unique&can allow only one null*/
ALTER TABLE C_ActiorType ADD CONSTRAINT IX_Name UNIQUE(Name)
GO

/*drop the constraint*/
ALTER TABLE C_ActiorType drop CONSTRAINT IX_ActorID
GO
/*creating the foriegn key for another primary key*/
ALTER TABLE C_CreditsMatrix
ADD CONSTRAINT fk_ActorID
FOREIGN KEY (ActorID)
REFERENCES C_ActiorType (ActiorID) ON DELETE CASCADE
GO

//Please Refer this links

http://blog.sqlauthority.com/2007/02/05/sql-server-primary-key-constraints-and-unique-key-constraints/

http://mssqlserver.wordpress.com/2006/11/22/difference-between-unique-constraint-and-primary-key/

http://www.mssqlcity.com/Articles/General/using_constraints.htm

No comments:

Post a Comment