数据库设计,哪个表有外键

发布于 2024-08-13 21:36:21 字数 222 浏览 6 评论 0原文

我有一个表USER(USER_ID、PASSWORD、NAME,...)和一个用于用户的表ACCESS_ROLES,每个用户可以有一个ACCESS_ROLE >(一对一)。

哪个表有外键?
我会将 USER_ID 放入 ACCESS_ROLES 表中。有没有最佳实践方法?

I have an Table USER (USER_ID, PASSWORD, NAME, ...) and an Table ACCESS_ROLES for the Users, every user can have one ACCESS_ROLE (ONE-TO-ONE).

Which table has the Foreign Key?
I would put the USER_ID into ACCESS_ROLES table. Is there any best practice approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

陌若浮生 2024-08-20 21:36:21

如果您有一组固定的访问角色和任意数量的用户,其中为用户分配了一个且仅一个访问角色,并且可以为任意数量的用户分配给定的访问规则[这就是我解释您的问题的方式],那么您可以在 USERS 表中放置一个类似“AccessRoleId”的列,并将外键约束添加到 ACCESS_ROLES 中。

If you have a fixed set of access roles and any number of users, where a user is assigned one and only one access role, and any number of users can be assigned a given access rule [this is how I interpret your question], then you would put a column like "AccessRoleId" in your USERS table and add a foreign key constraint into ACCESS_ROLES.

强者自强 2024-08-20 21:36:21

由于你们将建立一对一的关系,菲利普·凯利建议的解决方案更好。只需在 USER 中添加一个名为 access_role_id 的新列,然后在 USER 表上执行外键操作,如下所示:

ALTER TABLE USER ADD CONSTRAINT fk_access_roles_users
                 FOREIGN KEY (access_role_id) 
                 REFERENCES ACCESS_ROLES(access_role_id);

不要添加 USER_ID按照您的建议,将 放入 ACCESS_ROLES 表中。

Since you will be having a one-to-one relationship, the solution suggested by Philip Kelley is better. Just add a new column in USER called access_role_id and then do the foreign key on the USER table like this:

ALTER TABLE USER ADD CONSTRAINT fk_access_roles_users
                 FOREIGN KEY (access_role_id) 
                 REFERENCES ACCESS_ROLES(access_role_id);

Do not add USER_ID into ACCESS_ROLES table, as you suggested.

淡淡的优雅 2024-08-20 21:36:21
  • 如果每个 USER 恰好(或最多)有一个 ACCESS_ROLE。
  • 一个 ACCESS_ROLE 可以有多个 USER

那么:

  • FK 父级是 ACCESS_ROLE 并链接到 ACCESS_ROLES 表的 PK
  • FK 子级是 USERS 并且 FK 列是 ACCESS_ROLE 列

注意:外键“父”列必须被限制为唯一。如果每个 ACCESSROLE 有多个用户,则 FK必须

在 SQL Server 中从 USERS 到 ACCESSROLES

ALTER TABLE USERS WITH CHECK ADD
CONSTRAINT FK_USERS_ACCESS_ROLES FOREIGN KEY (ACCESS_ROLE) REFERENCES ACCESS_ROLES (ACCESS_ROLE /*PK?*/)
  • If every USER has exactly (or at most) one ACCESS_ROLE.
  • One ACCESS_ROLE can have multiple USERs

Then:

  • FK parent is ACCESS_ROLE and links to the PK of the ACCESS_ROLES table
  • FK child is USERS and the FK column is the ACCESS_ROLE column

Note: the foreign key "parent" column(s) must have be constrainted unique. If you have multiple users per ACCESSROLE, the FK must be from USERS to ACCESSROLES

In SQL Server

ALTER TABLE USERS WITH CHECK ADD
CONSTRAINT FK_USERS_ACCESS_ROLES FOREIGN KEY (ACCESS_ROLE) REFERENCES ACCESS_ROLES (ACCESS_ROLE /*PK?*/)
一曲琵琶半遮面シ 2024-08-20 21:36:21

如果您有一对一映射,我会将外键放入您大多数时间都会查询的表中。

If you have a One-To-One mapping, I would put the foreign key into the table that you will be querying most of the time.

没有伤那来痛 2024-08-20 21:36:21

您可以将外键添加到 ACCESS_ROLES

和以下内容以强制执行一对一映射

UNIQUE (ACCESS_ROLES_ID, USER_ID)

You can add a foreign key to ACCESS_ROLES

and the following to enforce one to one mapping

UNIQUE (ACCESS_ROLES_ID, USER_ID)
渡你暖光 2024-08-20 21:36:21

有没有最佳实践方法?

是的,有!

主键一样,外键也是一种施加在一个对象上的约束或表中的多个列。

外键在键列和另一个表中的相关列之间建立链接。 (您还可以将外键列链接到同一表中的列。)

包含外键的表被视为子表,外键引用的表被视为父表。

关键点

  1. 外键必须引用主键唯一约束,尽管该引用可以位于同一个桌子上或另一张桌子上。
  2. 外键还必须具有与引用约束中的列数相同的列数,并且相应列之间的数据类型必须匹配。
  3. 主键不同,外键列可以包含NULL值。

Is there any best practice approach?

Yes, there is!

Like a primary key, a foreign key is also a type of constraint placed on one or more columns in a table.

The foreign key establishes a link between the key columns and related columns in another table. (You can also link the foreign key columns to columns within the same table.)

The table that contains the foreign key is considered the child table, and the table that the foreign key references is the parent table.

Key Points

  1. The foreign key must reference a primary key or unique constraint, although that reference can be on the same table or on a different table.
  2. A foreign key must also have the same number of columns as the number of columns in the referenced constraint, and the data types must match between corresponding columns.
  3. Unlike Primary key, Foreign key columns can contain NULL values.
┊风居住的梦幻卍 2024-08-20 21:36:21

不确定我是否理解这个问题:我认为 access_table 应该有外键。

Not sure I understand the question or not: I think the access_table should have the Foreign Key.

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