用于存储不同类型“人”的SQL数据库设计

发布于 2024-12-05 10:39:01 字数 233 浏览 2 评论 0原文

我需要使用 SQL 在关系数据库中为具有 NameLastName 等列的人员创建一个表。

我将拥有三种不同类型的人:卖家、买家和客户。

每个人都有其他信息/属性。

我是否需要为每种不同类型的人员创建一个表,还是可以为所有三种类型使用一个表?

如果我使用单个表,如果一种类型的“人员”(例如卖方)具有与另一种人员类型不同的属性怎么办?

I need to create a table in a relational database using SQL for persons having the columns Name, LastName and so on.

I'll have three different kinds of People: Seller, Buyer and Customer.

Every person has other information/attributes.

Do I need to create a table for each different type of Person or can a single table be used for all three types?

If I used a single table, what if one type of "Person", say Seller, has different attributes from another Person type?

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

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

发布评论

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

评论(3

时间你老了 2024-12-12 10:39:01

我将创建一个表 Person ,以 personId 作为主键,其中将包含所有类型的公共属性。(卖方、买方、客户)

然后我将创建 PersonTypes,这是一个小型参考表,它将声明不同类型的代码。

然后,对于每种类型,我将参考 Person 表创建一个单独的表,并
包含所有唯一属性的 PersonType 表。

I would create one table Person , with personId as primary key that will contain common properties for all types.(Seller , Buyer , Customer)

Then I would create PersonTypes, a small reference table , that will declare codes for the different types .

Then for each type I would create a separate table with reference to Person table and
PersonType table that includes all the unique properties.

风尘浪孓 2024-12-12 10:39:01

您可以创建 1 个或两个表。当然,您可以为每个用户角色创建 3 个表。这取决于您想要实现的目标。您的问题的常见解决方案是:创建两张表,一张用于用户,一张用于其角色(mysql 示例:

Create table `person_role` (
id int not null,
roleName varchar(255) not null,
PRIMARY KEY(`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Create table `person`(
id int not null.
name varchar(255) not null,
lastName varchar(255) not null,
role int not null,
PRIMARY KEY(`id`),
CONSTRAINT FOREIGN KEY(`role`) REFERENCES person_role(`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

You can create 1 or two tables. Of course you can create 3 tables for each user role. It depend's on what you would like to achieve. Common solution to your question is: create two tables, one for users and one for their roles (examples for mysql:

Create table `person_role` (
id int not null,
roleName varchar(255) not null,
PRIMARY KEY(`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Create table `person`(
id int not null.
name varchar(255) not null,
lastName varchar(255) not null,
role int not null,
PRIMARY KEY(`id`),
CONSTRAINT FOREIGN KEY(`role`) REFERENCES person_role(`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
赢得她心 2024-12-12 10:39:01

卖家可以成为买家或客户吗?如果可以的话,将它们全部放在同一个表中,这样您就不会拥有相同数据的单独副本。

Can a seller ever be a buyer or a customer? If they can, put it all in the same table so that you don't have separate copies of the same data.

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