我正在使用Microsoft SQL Server 2005,而且一般来说它对SQL来说相对较新.
数据库"Information"中的两个表"Resources"和"Group_Resources"之间存在关系.Resources在Group_Resources中有一个名为"resource_id"的外键"id".两者之间存在外键约束"fk_gr_res_resources".
我已建立为数据库所有者,并具有完整的读/写/创建/删除权限.
我想删除外键约束,所以我执行了以下查询:
ALTER TABLE [Information].[group_resources] DROP CONSTRAINT fk_gr_res_resources
Run Code Online (Sandbox Code Playgroud)
并收到以下错误:
'fk_gr_res_resources'不是约束.无法删除约束.查看以前的错误.
我很困惑,因为它是一个约束,并且没有拼写错误.我打算不正确地删除这个吗?我是否从不正确的表中删除约束?任何建议都将不胜感激,请不要激怒我:毕竟我是SQL新手.
Joh*_*hnB 28
您收到此错误:
Msg 3728, Level 16, State 1, Line 1
'fk_gr_res_resources' is not a constraint.
Msg 3727, Level 16, State 0, Line 1
Could not drop constraint. See previous errors.
Run Code Online (Sandbox Code Playgroud)
因为FK约束不存在!
你确定这 Information 是正确的架构名称而不是 dbo吗?
1.这个SQL将证明FK不存在:
SELECT * FROM sysobjects WHERE name = 'fk_gr_res_resources'
Run Code Online (Sandbox Code Playgroud)
哎呀,我原来的答案错了,这是SQL Server的正确语法:
ALTER TABLE <table_name>
DROP CONSTRAINT <foreignkey_name>
Run Code Online (Sandbox Code Playgroud)
3.数据库示例:
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'fk_gr_res_resources')
BEGIN
ALTER TABLE Group_Resources
DROP CONSTRAINT fk_gr_res_resources
END;
Run Code Online (Sandbox Code Playgroud)
4.试试这个:
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'fk_gr_res_resources')
BEGIN
ALTER TABLE Group_Resources
ADD CONSTRAINT fk_gr_res_resources
FOREIGN KEY (resource_id)
REFERENCES Resources(id) /* make sure Resources.id is a PRIMARY KEY */
END;
Run Code Online (Sandbox Code Playgroud)
5.然后尝试这个,看看你是否还是得到了这个错误:
ALTER TABLE Group_Resources
DROP CONSTRAINT fk_gr_res_resources
Run Code Online (Sandbox Code Playgroud)
另一种语法是MySQL,抱歉:
ALTER TABLE <table_name>
DROP FOREIGN KEY <foreignkey_name>
Run Code Online (Sandbox Code Playgroud)
谢谢你纠正我的OMG小马!
| 归档时间: |
|
| 查看次数: |
49413 次 |
| 最近记录: |