删除SQL Server中的外键和列

时间:2021-09-07 00:35:55

I have a table which when I create a definition from SQL Server Management Studio I get this

我有一个表,当我从SQL Server Management Studio创建一个定义时,我得到了这个

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

I am trying to remove the Contact1 column and drop the foreign key

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

When I try to remove the column directly I get this

当我尝试直接删除列时,我得到了这个

ALTER TABLE Settings
    DROP COLUMN Contact1

Error:

错误:

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.

对象'FK__GlobalSet_72E607DB'依赖于列'Contact1'。消息4922,级别16,状态9,行24 ALTER TABLE DROP COLUMN Contact1失败,因为一个或多个对象访问此列。

When I try to remove the foreign key first

当我尝试先删除外键时

ALTER TABLE Settings
   DROP CONSTRAINT Contact1

Error:

错误:

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.

消息3728,级别16,状态1,行24'Contact1'不是约束。消息3727,级别16,状态0,行24无法删除约束。查看以前的错误。

Can someone please tell me how can I delete a column on existing table which is a foreign key.

有人可以告诉我如何删除现有表上的一个外键列。

Thanks

谢谢

1 个解决方案

#1


0  

Drop the Constraint 'FK__GlobalSet_72E607DB'

删除约束'FK__GlobalSet_72E607DB'

#1


0  

Drop the Constraint 'FK__GlobalSet_72E607DB'

删除约束'FK__GlobalSet_72E607DB'