简体   繁体   English

具有外键约束的SQL删除查询

[英]SQL delete query with foreign key constraint

I know that this question belongs to the very early stages of the database theory, but I have not encountered such a problem since several months. 我知道这个问题属于数据库理论的早期阶段,但是几个月以来我还没有遇到过这样的问题。 If someone has a database with some tables associated together as "chain" with foreign keys and they want to delete a record from a table which has some "dependent" tables, what obstacles arise? 如果某人的数据库中的某些表与外键“链”关联在一起,并且他们想从具有某些“相关”表的表中删除一条记录,那么会遇到什么障碍? In particular, in a database with tables: Person, Profile, Preference, Filter exist the associations as Person.id is foreign key in Profile and Profile.id is foreign key in Preference and Filter.id is foreign key in Preference , so as that all the associations enter code here are OneToMany . 特别是,在表的数据库:个人,档案,偏好,筛选存在协会评为Person.id是外键ProfileProfile.id是外键的PreferenceFilter.id是外键的Preference ,所以作为所有在enter code here的关联都是OneToMany Is it possible to delete a Person with a simple query: 是否可以通过简单查询删除人员:

Delete from Person p where p.id= 34;

If no, how should look like the query in order to perform the delete successfully? 如果不是,为了成功执行删除,查询应该看起来如何? If the database in the application is managed by hibernate, what constraints ( annotations ) should I apply to the associated fields of each entity, so as to be able with the above simple query to perform the delete? 如果应用程序中的数据库由休眠管理,我应该对每个实体的关联字段应用哪些约束( annotations ),以便能够通过上述简单查询执行删除?

FOR SQL VERSION 对于SQL版本

在此处输入图片说明

Look at the Screenshot. 看截图。 you can use the Insert Update Specificaiton Rules. 您可以使用插入更新规范规则。 as it has Delete and Update Rules. 因为它具有删除和更新规则。 you can set either of these values. 您可以设置这些值之一。

Foreign key constraints may be created by referencing a primary or unique key. 可以通过引用主键或唯一键来创建外键约束。 Foreign key constraints ensure the relational integrity of data in associated tables. 外键约束可确保关联表中数据的关系完整性。 A foreign key value may be NULL and indicates a particular record has no parent record. 外键值可以为NULL,表示特定记录没有父记录。 But if a value exists, then it is bound to have an associated value in a parent table. 但是,如果存在一个值,则它必然在父表中具有关联的值。 When applying update or delete operations on parent tables there may be different requirements about the effect on associated values in child tables. 在父表上应用更新或删除操作时,对子表中关联值的影响可能会有不同的要求。 There are four available options in SQL Server 2005 and 2008 as follows: SQL Server 2005和2008中有四个可用选项,如下所示:

No Action
Cascade
SET NULL
SET Default

Use this article for Refrence. 将此文章用于Refrence。

http://www.mssqltips.com/sqlservertip/2365/sql-server-foreign-key-update-and-delete-rules/ http://www.mssqltips.com/sqlservertip/2365/sql-server-foreign-key-update-and-delete-rules/

ORACLE VERSION 甲骨文版本

you can use one of below. 您可以使用以下之一。

alter table sample1 add foreign key (col1) references sample (col2) on delete no action; alter table sample1在不执行删除操作时添加外键(col1)引用样本(col2);

alter table sample1 add foreign key (col1) references sample (col2) on delete restrict; alter table sample1在删除限制上添加外键(col1)引用样本(col2);

alter table sample1 add foreign key (col1) references sample (col2) on delete cascade; alter table sample1在删除级联上添加外键(col1)引用样本(col2);

for refrance. 换弗朗西。

http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm

answer is no if there is foreign key constraint then you have to delete leaf node table data first 答案是否定的,如果有外键约束,那么您必须先删除叶节点表数据

that is first delete from Preference table 首先从Preference表中删除

then from Profile and Filter Table 然后从Profile and Filter

then delete record from Person table 然后从“ Person table删除记录

This is the generic concept that you apply anywhere 这是您可以在任何地方应用的通用概念

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

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