外键,它们所属的位置取决于关系

发布于 2024-11-18 14:06:25 字数 931 浏览 3 评论 0原文

我曾经使用过相当多的数据库(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 技术交流群。

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

发布评论

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

评论(1

大姐,你呐 2024-11-25 14:06:25

外键在子表上声明并引用父表。

在一对一关系中,DBC_USER 是父级,DBC_USER_PROFILE 是子级。因此,您需要在 DBC_USER_PROFILE 表上声明外键并引用 DBC_USER 表。类似于(Oracle 语法)

CREATE TABLE dbc_user_profile (
  user_id int primary key,
  <<more columns>>
  constraint fk_user_profile_user_id foreign key (user_id)
    references dbc_user( user_id )
)

在一对多关系中,通常非常清楚哪个表是父表,哪个表是子表。父级有 1 行,子级有 m 行。因此,您在子表上声明外键并引用父表。

CREATE TABLE parent (
  parent_id int primary key,
  <<additional columns>>
);

CREATE TABLE child (
  child_id int primary key,
  parent_id int references parent( parent_id ),
  <<additional columns>>
);

对于多对多关系,外键将在映射表上定义并引用两个父表。因此,外键将在 DBC_USER_CITY 表上声明,并引用 DBC_USERDBC_CITY。显然

CREATE TABLE dbc_user_city (
  city_id int,
  user_id int,
  constraint pk_dbc_user_city primary key( city_id, user_id ),
  constraint fk_dbc_user_city_city_id foreign key( city_id )
    references dbc_city( city_id ),
  constraint fk_dbc_user_city_user_id foreign key( user_id )
    references dbc_user( user_id )
)

,语法往往是相对特定于数据库的。特别是对于 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 and DBC_USER_PROFILE is the child. So you'd declare the foreign key on the DBC_USER_PROFILE table and reference the DBC_USER table. Something like (Oracle syntax)

CREATE TABLE dbc_user_profile (
  user_id int primary key,
  <<more columns>>
  constraint fk_user_profile_user_id foreign key (user_id)
    references dbc_user( user_id )
)

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.

CREATE TABLE parent (
  parent_id int primary key,
  <<additional columns>>
);

CREATE TABLE child (
  child_id int primary key,
  parent_id int references parent( parent_id ),
  <<additional columns>>
);

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 both DBC_USER and DBC_CITY. Something like

CREATE TABLE dbc_user_city (
  city_id int,
  user_id int,
  constraint pk_dbc_user_city primary key( city_id, user_id ),
  constraint fk_dbc_user_city_city_id foreign key( city_id )
    references dbc_city( city_id ),
  constraint fk_dbc_user_city_user_id foreign key( user_id )
    references dbc_user( user_id )
)

Obviously, 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.

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