快速掌握外键约束和参绍约束的实例脚本
来源:岁月联盟
时间:2008-01-28
外键约束和参绍约束的实例脚本:
/*外键约束和参绍约束pubs数据库的CarIfo表创建外键约束,参绍CarFactory表的主键ID.*/USE PubsGO/*不显示行数*/SET NOCOUNT ONGO/*创建示例表*/IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N’CarInfo’) AND OBJECTPROPERTY(id, N’IsUserTable’) = 1)DROP TABLE CarInfoGOCREATE TABLE CarInfo(Car_ID CHAR (10) ,Name CHAR (10) ,Type CHAR (20),Driver_Type CHAR (15))IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N’CarFactory’) AND OBJECTPROPERTY(id, N’IsUserTable’) = 1)DROP TABLE CarFactoryGOCREATE TABLE CarFactory(ID CHAR (10) NOT NULL,Name CHAR (20) ,PostCode CHAR (6),Telphone CHAR (15),Address CHAR (30),)GOALTER TABLE CarFactoryADD CONSTRAINT PK_Car_ID PRIMARY KEY NONCLUSTERED (ID)GOALTER TABLE CarInfoWITH NOCHECKADD CONSTRAINT Car_id_link FOREIGN KEY (Car_ID) REFERENCES CarFactory(ID)GOUSE masterEXEC sp_dboption Pubs, ’dbo use only’, TRUEGOUSE PubsSET NOCOUNT ONGO |