如何正确使用外键
我正在开发一个类似帮助台的系统,我想使用外键,以确保数据库结构正确,但我不知道是否应该使用它们,以及如何正确使用它们。
有关于如何(以及何时)使用外键的好的教程吗?
编辑我最困惑的部分是ON DELETE .. ON UPDATE ..部分,假设我有下表
table 'users'
id int PK auto_increment
department_id int FK (departments.department_id) NULL
name varchar
table 'departments'
id int PK auto_increment
name
users.department_id是departments.department_id的外键,如何当我想删除部门或用户时,ON UPDATE 和 ON DELETE 功能在这里起作用吗?
I'm developing a helpdesk-like system, and I want to employ foreign keys, to make sure the DB structure is decent, but I don't know if I should use them at all, and how to employ them properly.
Are there any good tutorials on how (and when) to use Foreign keys ?
edit The part where I'm the most confused at is the ON DELETE .. ON UPDATE .. part, let's say I have the following tables
table 'users'
id int PK auto_increment
department_id int FK (departments.department_id) NULL
name varchar
table 'departments'
id int PK auto_increment
name
users.department_id is a foreign key from departments.department_id, how does the ON UPDATE and ON DELETE functions work here when i want to delete the department or the user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ON DELETE
和ON UPDATE
指的是您在键表中所做的更改如何传播到从属表。 UPDATE 意味着更改依赖表中的键值以维护关系,DELETE 意味着删除依赖记录以保持完整性。示例:假设您现在已经
更改
了 deparments 表以将第一条记录修改为读取
id = 5, Name = Sales
,那么使用“UPDATE”您还将更改前两条记录以读取Department = 5
- 如果没有“UPDATE”,您将无法进行更改!同样,如果你删除了部门 2,那么使用“DELETE”你也会删除 Roy 的记录!如果没有“删除”,则在不先删除罗伊的情况下,您将无法删除该部门。
ON DELETE
andON UPDATE
refer to how changes you make in the key table propagate to the dependent table. UPDATE means that the key values get changed in the dependent table to maintain the relation, and DELETE means that dependent records get deleted to maintain the integrity.Example: Say you have
and
Now if you change the deparments table to modify the first record to read
id = 5, Name = Sales
, then with "UPDATE" you would also change the first two records to readDepartment = 5
-- and without "UPDATE" you wouldn't be allowed to make the change!Similarly, if you deleted Department 2, then with "DELETE" you would also delete the record for Roy! And without "DELETE" you wouldn't be allowed to remove the department without first removing Roy.
如果您将数据库拆分为表并且使用 DBMS(例如 MySQL、Oracle 等),则需要外键。从你的标签来看,我假设你正在使用 MySQL。
如果不使用外键,您的数据库将变得难以管理和维护。规范化过程使用外键确保数据一致性。
请参阅此处了解外键。请参阅此处为什么外键在关系数据库中很重要。
尽管当效率是设计中的主要因素时,通常会使用非规范化。如果是这种情况,您可能想要放弃我告诉您的内容。
希望这有帮助。
You will need foreign keys if you are splitting your database into tables and you are working with a DBMS (e.g. MySQL, Oracle and others). I assume from your tags you are using MySQL.
If you don't use foreign keys your database will become hard to manage and maintain. The process of normalisation ensures data consistency, which uses foreign keys.
See here for foreign keys. See here for why foreign keys are important in a relational database here.
Although denormalization is often used when efficiency is the main factor in the design. If this is the case you may want to move away from what I have told you.
Hope this helps.