如何在SQL Server中删除外键?
时间:2020-03-06 14:21:43 来源:igfitidea点击:
我通过以下方式创建了一个外键(在SQL Server中):
alter table company add CountryID varchar(3); alter table company add constraint Company_CountryID_FK foreign key(CountryID) references Country;
然后,我运行以下查询:
alter table company drop column CountryID;
我得到这个错误:
Msg 5074, Level 16, State 4, Line 2 The object 'Company_CountryID_FK' is dependent on column 'CountryID'. Msg 4922, Level 16, State 9, Line 2 ALTER TABLE DROP COLUMN CountryID failed because one or more objects access this column
我已经尝试过了,但是似乎不起作用:
alter table company drop foreign key Company_CountryID_FK; alter table company drop column CountryID;
我需要怎么做才能删除" CountryID"列?
谢谢。
解决方案
尝试
alter table company drop constraint Company_CountryID_FK alter table company drop column CountryID
我不知道MSSQL,但不是:
alter table company drop **constraint** Company_CountryID_FK;
alter table company drop constraint Company_CountryID_FK
这将起作用:
ALTER TABLE [dbo].[company] DROP CONSTRAINT [Company_CountryID_FK]
我们也可以右键单击表,选择"修改",然后转到属性,右键单击它,然后选择"删除主键"。