外键,它们所属的位置取决于关系
我曾经使用过相当多的数据库(MySQL、Oracle),但通常只使用 DML,因为系统之前已经设计好了。
我作为唯一开发人员正在做一个项目,负责应用程序开发和DBA;当然,后者的问题更大。
无论如何 - 我需要刷新外键约束及其在模型中的逻辑位置。假设如下:
dbc_user
.user_id INT, Primary Key, Auto Increment
// arbitrary columns
dbc_user_profile
.user_id INT, Primary Key
// arbitrary columns
这些表是一对一关系。我知道,鉴于这种关系,它们的列可以合并到一个表中,但让我们将它们分开。那么外键属于 dbc_user_profile
表吗?
那么多对多呢?
dbc_user
.user_id INT, Primary Key, Auto Increment
dbc_city
.city_id INT, Primary Key, Auto Increment
dbc_user_city
.city_id INT, Primary Key
.user_id INT, Primary Key
哪些表是引用/被引用表?
由于某种原因,我似乎找不到关于此的快速而肮脏的教程来解释这些关系。
所以分为两部分的问题;
- 有人可以为半懂行的人推荐一个教程,敲定有关外键的 DDL 细节吗?或者;
- 有人可以快速概述一下一对一、一对一和一对一关系以及 FK 参考资料?
I've worked with databases a fair bit (MySQL, Oracle) though typically only DML as the systems had been previously engineered.
I'm working on a project as the sole developer, and am responsible for application development, and DBA; the latter of course proving more problematic.
In any case - I need a refresh on foreign key constraints, and their logical placement in a model. Given the following:
dbc_user
.user_id INT, Primary Key, Auto Increment
// arbitrary columns
dbc_user_profile
.user_id INT, Primary Key
// arbitrary columns
These tables are a 1-to-1 relationship. I understand that given that relationship, their columns could be consolidated into a single table, but let's just keep them separate. The foreign key belongs then, in the dbc_user_profile
table?
What about many-to-many?
dbc_user
.user_id INT, Primary Key, Auto Increment
dbc_city
.city_id INT, Primary Key, Auto Increment
dbc_user_city
.city_id INT, Primary Key
.user_id INT, Primary Key
Which tables are the referencing/referenced tables?
For some reason I can't seem to find a quick and dirty tutorial on this, that explains the relationships.
So two part question;
- Can someone suggest a tutorial for the semi-savvy that hammers out the details of DDL with regard to foreign keys? or;
- Can someone give a quick rundown on 1-to-1, 1-to-m, and m-to-m relationships and FK references?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
外键在子表上声明并引用父表。
在一对一关系中,
DBC_USER
是父级,DBC_USER_PROFILE
是子级。因此,您需要在DBC_USER_PROFILE
表上声明外键并引用DBC_USER
表。类似于(Oracle 语法)在一对多关系中,通常非常清楚哪个表是父表,哪个表是子表。父级有 1 行,子级有 m 行。因此,您在子表上声明外键并引用父表。
对于多对多关系,外键将在映射表上定义并引用两个父表。因此,外键将在
DBC_USER_CITY
表上声明,并引用DBC_USER
和DBC_CITY
。显然,语法往往是相对特定于数据库的。特别是对于 Oracle,PSOUG 站点 上有一个相当完整的语法选项列表。如需包含各种数据库语法的更一般参考,您可以使用 w3schools 网站。
A foreign key is declared on the child table and references the parent table.
In a 1-to-1 relationship, it would appear that
DBC_USER
is the parent andDBC_USER_PROFILE
is the child. So you'd declare the foreign key on theDBC_USER_PROFILE
table and reference theDBC_USER
table. Something like (Oracle syntax)In a 1-to-m relationship, it generally pretty clear which table is the parent and which is the child. The parent has 1 row for m rows in the child. So you declare the foreign key on the child table and reference the parent table.
For an m-to-m relationship, the foreign key would be defined on the mapping table and would reference the two parent tables. So the foreign keys would be declared on the
DBC_USER_CITY
table and would reference bothDBC_USER
andDBC_CITY
. Something likeObviously, the syntax tends to be relatively database specific. For Oracle in particular, there is a pretty complete list of the syntax options on the PSOUG site. For a more general reference that includes syntax for a variety of databases, you could use the w3schools site.