Wednesday, 16 December 2009
Create and Drop Constraints (Primary Key and Foreign Key Constraints)
Objective: Create and Drop Constraints (Primary Key and Foreign Key Constraints)
sp_pkeys -- To see primary key
GO
sp_fkeys -- To see foriegn key
USE [DatabaseName]
GO
ALTER TABLE [dbo].[TableName] WITH NOCHECK ADD CONSTRAINT [ForiegnKeyConstraintName] FOREIGN KEY([AffiliateID])
REFERENCES [dbo].[ReferencedTableName] ([ColumnName])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[TableName] CHECK CONSTRAINT [ForiegnKeyConstraintName]
GO
USE [DatabaseName]
GO
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[ForiegnKeyConstraintName]') AND parent_object_id = OBJECT_ID(N'[dbo].[TableName]'))
ALTER TABLE [dbo].[TableName] DROP CONSTRAINT [ForiegnKeyConstraintName]
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment