2017-08-30 84 views
0

我有一个表,当我从SQL Server Management Studio中创建一个定义,我得到这个删除SQL Server中的外键和列

CREATE TABLE [dbo].[Settings] 
(
    [SettingsID] [int] NOT NULL,  
    [Contact1] [uniqueidentifier] NULL, 

    CONSTRAINT [PK_Settings] 
     PRIMARY KEY CLUSTERED ([SettingsID] ASC) 
) ON [PRIMARY] 
GO 

ALTER TABLE [dbo].[Settings] WITH CHECK 
    ADD FOREIGN KEY([Contact1]) 
    REFERENCES [dbo].[User] ([UserSID]) 
GO 

我试图删除Contact1列,删除外键

当我尝试删除栏目中直接我得到这个

ALTER TABLE Settings 
    DROP COLUMN Contact1 

错误:

The object 'FK__GlobalSet_72E607DB' is dependent on column 'Contact1'.
Msg 4922, Level 16, State 9, Line 24
ALTER TABLE DROP COLUMN Contact1 failed because one or more objects access this column.

当我尝试删除外键第一

ALTER TABLE Settings 
    DROP CONSTRAINT Contact1 

错误:

Msg 3728, Level 16, State 1, Line 24
'Contact1' is not a constraint.
Msg 3727, Level 16, State 0, Line 24
Could not drop constraint. See previous errors.

是否有人可以告诉我怎样可以删除现有表中的列是一个外键。

感谢

+3

'FK__GlobalSet_72E607DB'是约束的名称,而不是'Contact1' – LONG

+0

是的,我注意到了,但不知道这是怎么generated..Will这总是是同一个名字。 – user1221989

+0

https://stackoverflow.com/questions/45945900/dropping-and-recreating-constraints-in-sql-server发现这个来解决这个问题,但仍然不知道如何生成该名称。 – user1221989

回答

0

删除约束“FK__GlobalSet_72E607DB”