如何正确使用外键

发布于 2024-11-17 12:26:20 字数 455 浏览 1 评论 0原文

我正在开发一个类似帮助台的系统,我想使用外键,以确保数据库结构正确,但我不知道是否应该使用它们,以及如何正确使用它们。

有关于如何(以及何时)使用外键的好的教程吗?

编辑我最困惑的部分是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-11-24 12:26:20

ON DELETEON UPDATE 指的是您在键表中所做的更改如何传播到从属表。 UPDATE 意味着更改依赖表中的键值以维护关系,DELETE 意味着删除依赖记录以保持完整性。

示例:假设您现在已经

 Users: Name =  Bob, Department = 1
 Users: Name =  Jim, Department = 1
 Users: Name =  Roy, Department = 2

更改

 Departments: id = 1, Name = Sales
 Departments: id = 2, Name = Bales

了 deparments 表以将第一条记录修改为读取 id = 5, Name = Sales,那么使用“UPDATE”您还将更改前两条记录以读取Department = 5 - 如果没有“UPDATE”,您将无法进行更改!

同样,如果你删除了部门 2,那么使用“DELETE”你也会删除 Roy 的记录!如果没有“删除”,则在不先删除罗伊的情况下,您将无法删除该部门。

ON DELETE and ON 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

 Users: Name =  Bob, Department = 1
 Users: Name =  Jim, Department = 1
 Users: Name =  Roy, Department = 2

and

 Departments: id = 1, Name = Sales
 Departments: id = 2, Name = Bales

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 read Department = 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.

一直在等你来 2024-11-24 12:26:20

如果您将数据库拆分为表并且使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文