如何删除外键参考表 [英] How to delete the forigne key reference table
本文介绍了如何删除外键参考表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人
如何删除原键参考表行.
表1:
id,名称(heare ID是主键)
表2:
Emp_ID,dept,ID(这里的ID是外来键参考)
Dear All
How to delete forigen key reference table rows.
table 1:
id, name (heare Id is primary key)
table 2:
Emp_ID, dept, ID (here id is forigen key reference)
推荐答案
您可以轻松地从表2中删除,但是如果要从表1中删除
使用
从table1级联删除
you can easily delete from table 2 but if you want to delete from table 1
use
delete from table1 cascade
如果要从table2中删除.要么
1.您应该从表1中删除所有相应的条目,然后只有u可以从表2中删除,否则它将显示
''DELETE语句与REFERENCE约束冲突''
2.如果您从表1中删除外键约束,则还有1个选项,那么您还可以从表2中删除行..
从表1的WHERE ID = 1
删除 GO
从table2中删除WHERE Emp_ID = 1
还有1种方法->否则在表定义中使用ON DELETE CASCADE.
If you want to delete from table2. either
1. You should to delete all the corresponding entries from table1, then only u can delete from table2,Or else it''ll show
''The DELETE statement conflicted with the REFERENCE constraint''
2. 1 more option is there if u drop the constraint of foreign key from table1, then also u can delete rows from table 2..
DELETE FROM table1 WHERE Id=1
GO
DELETE FROM table2 WHERE Emp_ID=1
1 more method -->Or Else use ON DELETE CASCADE with table Definition.
您好....
只要试试这个
ALTER TABLE table2
添加约束FK_table2ID外键(ID)
参考table1(id)
删除级联
祝你好运...
Hi there....
just try this one
ALTER TABLE table2
ADD CONSTRAINT FK_table2ID FOREIGN KEY (ID)
REFERENCES table1(id)
ON DELETE CASCADE
best of luck....
这篇关于如何删除外键参考表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文