如何引用另一个表中的多行? [英] How can you reference multiple rows in another table?

查看:116
本文介绍了如何引用另一个表中的多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一张角色表,例如,

Let's suppose I have a table of roles, for example,

Roles
-----
ID  Name
0   Salesman
1   Client
2   Manager

让我们还假设这些角色不是互斥的:也就是说,一个人可以同时担任推销员,客户和经理。

Let's also suppose that these roles are not mutually exclusive: that is, a person can be a salesman, a client, and a manager all at the same time.

如果我有一个具有角色字段的 Person 表,我如何能够引用<$ c中的多行$ c> Roles 表?

If I have a Person table with a Role field, how am I able to reference multiple rows in the Roles table?

推荐答案

您引入了一个引用了两个原始表的新表:

You introduce a new table that references both of the original tables:

CREATE TABLE PersonRoles (
    PersonID int not null,
    RoleID int not null,
    constraint PK_PersonRoles PRIMARY KEY (PersonID,RoleID),
    constraint FK_PersonRoles_Persons FOREIGN KEY (PersonID) references Person (ID),
    constraint FK_PersonRoles_Roles FOREIGN KEY (RoleID) references Role (ID)
)

假设多重度为 m:n ,则以上是正确的。我做出的假设(未在您的问题中列出)是一个以上的人可以推销员。

Assuming that the multiplicity is m:n, the above is correct. I made the assumption (not listed in your question) that more than one person can be e.g. a Salesman.

这篇关于如何引用另一个表中的多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆