简体   繁体   English

SQL删除外键

[英]SQL delete foreign key

    +--------+--------+
    |A_id    |A_desc  |
    +--------+--------+

    table B:
    +--------+--------+
    |B_id    |B_desc  |
    +--------+--------+

    table c:
    +--------+--------+
    |A_id    |B_id    |
    +--------+--------+

I want to delete 1 row in c table and 1 row in b table without deleting table a by getting value of "B_id", anyone can solve this problem? 我想删除c表中的1行和b表中的1行而不通过获取“ B_id”的值来删除表a,有人可以解决此问题吗?

you can only do that with triggers / writhing some other code to do that for you. 您只能使用触发器/编写一些其他代码来为您执行此操作。

Note that even if you do use triggers / code, you might get an exception since other records in C can point to table B. 请注意,即使您确实使用触发器/代码,也可能会遇到异常,因为C中的其他记录可以指向表B。

What you can do is run something like: 您可以执行以下操作:

delete from B where B_id not in (select B_id from C)

after you run the delete 运行删除后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM