一个真正令人耳目一新的案例 [英] A Really Bizzare Case

查看:80
本文介绍了一个真正令人耳目一新的案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况是这样的.我必须从子表中删除一条记录.
但是在表模式中,有一个来自子代的外键引用
表到父表.

因此,为了删除记录,我需要设置外键
父表中的单元格为null.但是我发现父记录对该列具有非null约束.因此,我无法将其设置为null.

最后,子表无法删除.那么关于如何从子表中删除记录的任何建议?

ParentTable
{
id int PK,
child_id int FK NOT NULL,
Other_id int FK NOT NULL
}

ChildTable
{
child_id int PK
}

解决方案

尝试思考您要做的事的具体示例.
现在考虑一下,父表代表整辆自行车,*必须*包括前轮,而子表代表前轮.您想要做的就是从自行车上偷前轮,并希望它能继续成为自行车.
生活并非如此,即使在数据库中也是如此.即使一个前轮可以属于几辆自行车,同样的想法仍然成立.

我敢肯定周围有更好的例子,但是这个例子应该可以理解.

干杯,
彼得


如果您能够删除子记录,那么预期结果是什么?

又例如,如果您在parenttable中有一条记录,该记录引用了ID为1的childtable.现在,您删除childtable行,child_id其余行的值是多少?

我建议您仔细检查表的设计是否确实存在设计缺陷(如Peter所建议的那样),请与负责对该模式进行修改的人员进行交谈.


The case is like this. I have to delete a record from the child table.
But in the table schema there is a foreign key reference from the child
table to the parent table.

Therefore in order to delete the record, i need to set the foreign key
cell in the parent table null. But i found out that the parent record has a not null constraint on that column. therefore i am not able to set it null.

at last the child table could not be deleted. So any suggestions on how i can delete the record from the child table?

ParentTable
{
id int PK,
child_id int FK NOT NULL,
Other_id int FK NOT NULL
}

ChildTable
{
child_id int PK
}

解决方案

Try thinking about a concrete example of what you are trying to do.
Consider for the moment that the parent table represents whole bicycles, which *MUST* include front wheels, and the child table represents front wheels. What you are trying to do is steal the front wheel from a bicycle and expecting it to remain a bicycle.
Life''s not like that, even in databases. Even if one front wheel could belong to several bicycles, the same idea holds.

I''m sure there are much better example around, but this one should get the point across.

Cheers,
Peter


If you would be able to delete the child record what would be the expected result?

Or example if you have a record in parenttable which is referencing childtable with id 1. Now you delete the childtable row, what would be the value on the remaining row in child_id?

I suggest that you double check the design if the tables and if it really is a design flaw, as Peter suggested,talk to the person who''s responsible for making modifications to that schema.


这篇关于一个真正令人耳目一新的案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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