开发者

SQL database design for storing different types of "Person"

开发者 https://www.devze.com 2023-04-06 04:34 出处:网络
I need to create a table in a relational database using SQL for persons having the columns Name, LastName and so on.

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?


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.


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号