外键约束和参绍约束的实例脚本 /*外键约束和参绍约束pubs数据库的CarIfo表创建外键约束参绍CarFactory表的主键ID*/USE PubsGO/*不显示行数*/SET NOCOUNT ONGO/*创建示例表*/IF EXISTS (SELECT * FROM dbosysobjects WHERE id = object_id(NCarInfo) AND OBJECTPROPERTY(id NIsUserTable) = )DROP TABLE CarInfoGOCREATE TABLE CarInfo(Car_ID CHAR () Name CHAR () Type CHAR ()Driver_Type CHAR ())IF EXISTS (SELECT * FROM dbosysobjects WHERE id = object_id(NCarFactory) AND OBJECTPROPERTY(id NIsUserTable) = )DROP TABLE CarFactoryGOCREATE TABLE CarFactory(ID CHAR () NOT NULLName CHAR () PostCode CHAR ()Telphone CHAR ()Address CHAR ())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 |