ALTER TABLE语句与FOREIGN KEY SAME TABLE约束冲突 [英] ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint

查看:364
本文介绍了ALTER TABLE语句与FOREIGN KEY SAME TABLE约束冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用ALTER TABLE语句创建自引用约束。 (tsql SQL Server 2012)

I like to know if it's possible to create a self refering constraint using an ALTER TABLE statement. (tsql SQL Server 2012)

期望我有一个表,其中只有一个名为ID的主键和一个名为parent_id的列

expected I have a table with just a Primary Key called ID and column called parent_id

我想做这样的事情:

ALTER TABLE myTable
ADD CONSTRAINT FK_myTablemyTable
FOREIGN KEY (parent_Id)
REFERENCES myTable(Id)

但我只是得到错误:

ALTER TABLE语句与FOREIGN KEY SAME TABLE约束冲突

The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint

是否存在使该语句正常工作的另一种方法,还是在这种情况下必须重新创建整个表?

Is there an alternative way to get the statement working, or do you have to recreate the whole table in this case ?

希望有人能够提供帮助

欢呼声

推荐答案

问题已解决

我正在创建某种表迁移工具,还没有意识到已经插入了数据……

I am creating some kind of table migration tool and have not realized that the data was inserted already ...

问题是根元素的插入是parent_id = 0而不是null ...

Problem was that the root element was Inserted with parent_id = 0 and not null...

所以在SQL语句中没错。

So there is no mistake in the SQL statement.

现在可以正常使用

这篇关于ALTER TABLE语句与FOREIGN KEY SAME TABLE约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆