键约束和参绍约束的实例脚本/*外键约束和参绍约束pubs数据库的CarIfo表创建外键约束参绍CarFactory表的主键ID 外键约束和参绍约束的实例脚本 /* 外键约束和参绍约束 pubs数据库的CarIfo表创建外键约束参绍CarFactory表的主键ID */ USEPubs GO /*不显示行数*/ SETNOCOUNTON GO /*创建示例表*/ IFEXISTS(SELECT*FROMdbosysobjects WHEREid=object_id(NCarInfo) ANDOBJECTPROPERTY(idNIsUserTable)=) DROPTABLECarInfo GO CREATETABLECarInfo( Car_IDCHAR() NameCHAR() TypeCHAR() Driver_TypeCHAR() ) IFEXISTS(SELECT*FROMdbosysobjects WHEREid=object_id(NCarFactory) ANDOBJECTPROPERTY(idNIsUserTable)=) DROPTABLECarFactory GO CREATETABLECarFactory( IDCHAR()NOTNULL NameCHAR() PostCodeCHAR() TelphoneCHAR() AddressCHAR() ) GO ALTERTABLECarFactory ADDCONSTRAINTPK_Car_ID PRIMARYKEYNONCLUSTERED(ID) GO ALTERTABLECarInfo WITHNOCHECK ADDCONSTRAINTCar_id_link FOREIGNKEY(Car_ID)REFERENCESCarFactory(ID) GO USEmaster EXECsp_dboptionPubsdbouseonlyTRUE GO USEPubs SETNOCOUNTON GO |