对同一表的列实施外键约束
如何在 SQL 中对同一表的列强制外键约束,同时在下表中输入值:
员工:
- empid 编号,
- 经理编号(必须是现有员工)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 SQL 中对同一表的列强制外键约束,同时在下表中输入值:
员工:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
Oracle 将此称为自引用完整性约束。文档位于此处,
您可以创建一个自引用约束的方式与普通约束相同:
我假设您的
manager_no
可为空。我在此处添加了 set null,因为删除级联
可能会清除表中的大量内容。我想不出更好的方法来做到这一点。删除经理不应导致删除其所有员工,因此您必须
设置 null
并在表上设置一个触发器,以提醒您没有经理的任何人。我一直喜欢这个网站,它非常适合简单的参考。并且不要忘记在 FK 上也有一个索引或 汤姆会对你大喊大叫:-)。
还可以利用标准 Oracle 语法在 create table 语句中创建自引用 FK,如下所示。
编辑:
回答下面@popstack的评论:
虽然您可以在一个语句中执行此操作,但无法更改表是一种相当荒谬的情况。您绝对应该分析要从中选择的表,并且您仍然需要外键上的索引(可能还有更多列和/或更多索引),否则每当您使用外键时,您都会这样做全表扫描。请参阅上面我的 Asktom 链接。
如果您无法更改表格,那么您应该按照重要性降序排列。
Oracle call this a self-referential integrity constraint. The documentation is here for a description,
You create a self-referential constraint in the same manner you would a normal one:
I'm assuming that your
manager_no
is nullable. I've added set null here as adelete cascade
would probably wipe out a significant amount of your table.I can't think of a better way of doing this. Deleting a manager should not result in the deletion of all their employees so you have to
set null
and have a trigger on the table to alert you to anyone with no manager.I always like this site, which is good for simple references. and don't forget to have an index on the FK as well or Tom will yell at you :-).
One can also utilise standard Oracle syntax to create a self-referential FK in the create table statement, which would look like the following.
EDIT:
In answer to @popstack's comment below:
Whilst you can do this in one statement not being able to alter a table is a fairly ridiculous state of affairs. You should definitely analyze a table that you're going to be selecting from and you will still want an index on the foreign key ( and possibly more columns and / or more indexes ) otherwise whenever you use the foreign key you're going to do a full table scan. See my link to asktom above.
If you're unable to alter a table then you should, in descending order of importance.
自引用查询...
EX-
ALTER TABLE Employee ADD CONSTRAINTS Fr_key( mgr_no) 引用员工(Emp_no) ON DELETE CASCADE;
SELF REFERENCES QUERY...
EX-
ALTER TABLE Employee ADD CONSTRAINTS Fr_key( mgr_no) references employee(Emp_no) ON DELETE CASCADE;
希望有帮助!
Hope it helps!