简体   繁体   English

MySQL数据库设计,两种类型的记录-使用一个表还是两个单独的表?

[英]MySQL database design, two types of records - use one table or two separate tables?

I'm building an application that will have two different types of users, lets call one User_type_a and the other User_type_b . 我正在构建一个具有两种不同类型用户的应用程序,让我们分别调用一个User_type_a和另一个User_type_b I'm wondering if I should create 1 table in my database for both types of users and have a property distinguishing what type of user each record is, or if I should create two separate tables for each type of user. 我想知道是否应该在数据库中为两种类型的用户创建一个表,并具有一个属性来区分每个记录是哪种类型的用户,或者是否应该为每种类型的用户创建两个单独的表。

Considerations: 1. 99% of all users will be User_type_a 2. User_type_b will require properties in addition to User_type_a (such as credit card #'s, etc) 注意事项:1.所有用户的99%将是User_type_a。2. User_type_b除了User_type_a还将需要其他属性(例如信用卡号等)

Which design approach is optimal? 哪种设计方法是最佳的? Or does it not really matter. 还是不是很重要。

One table for users, assuming that user type b are real users. 一张用户表,假设用户类型b是真实用户。 Create another table that links to the user table to store the CC details for user type B. 创建另一个链接到用户表的表,以存储用户类型B的抄送详细信息。

This allows you do do all major user changes easily (searching users, changing user details, looking up users for login, etc), but doesn't contain many wasted columns. 这使您可以轻松进行所有主要的用户更改(搜索用户,更改用户详细信息,查找用户以进行登录等),但是其中不包含许多浪费的列。

Note that if you are storing credit card numbers, your datacenter and architecture will have to be PCI compliant , which is expensive. 请注意,如果要存储信用卡号,则数据中心和体系结构必须符合PCI标准 ,这很昂贵。

If type B has only additional information (columns) to the generic user type then use: 如果类型B仅具有通用用户类型的其他信息(列),则使用: 在此处输入图片说明

If types A and B have some common columns and a set of distinct columns for each one, then use 如果类型AB具有一些公共列,并且每个都有一组不同的列,则使用 在此处输入图片说明

I both cases keep all common columns in the User table -- sub-type tables have only columns specific to each one. 两种情况都将所有公共列保留在User表中-子类型表仅具有特定于每个列的列。 Note that UserID propagates to sub-type tables. 请注意, UserID传播到子类型表。

The best way to do this would be to store all users in the same table, and have a foreign key relating to a second table, which contains the extra information. 最好的方法是将所有用户存储在同一表中,并具有与第二个表相关的外键,该表包含额外的信息。

**USER TABLE**
NAME     AGE       TYPE     FK
Grant    25        Adult    1
Susan    4         Child    null
John     65        Adult    2

**EXTRA TABLE**
FK    CREDITCARD    OTHER
1     234234...     blah
2     2334...       blah

This would be more efficient with space. 这样可以节省空间。

So it sounds like User_type_a and User_type_b are both identical in terms of data, with the exception being that User_type_b has additional data above and beyond User_type_a (but User_type_a does not have any unique data like this). 因此,听起来User_type_a和User_type_b的数据相同,不同的是User_type_b在User_type_a之上和之外都有其他数据(但User_type_a没有这样的唯一数据)。

Given this, I would create a single users table that stores the User_type_a data (ie the intersection of the two user types). 鉴于此,我将创建一个存储User_type_a数据(即两种用户类型的交集)的单个用户表。 Then create a second table for the additional User_type_b data, with a foreign key linking that one back to users. 然后为其他User_type_b数据创建第二个表,并使用外键将该表链接回用户。 (Note that there is no column here in the users table defining which users are which type.) (请注意,users表中没有此列定义哪些用户是哪种类型。)

How to tell the difference between the two user types? 如何分辨两种用户类型的区别? Simple: User_type_b has a related row in the second table; 简单:User_type_b在第二个表中具有相关的行; User_type_a does not. User_type_a没有。 This makes it easy for any application functions that don't care about the difference to just get the common user data for everyone, while functions that need the extra User_type_b data (or otherwise only care about one type or the other) can still determine who is what type and get that extra data. 这使得任何不关心差异的应用程序功能都可以轻松获取每个人的通用用户数据,而需要额外的User_type_b数据(或仅关心一种或另一种类型)的功能仍可以确定谁是什么类型并获得额外的数据。

Use one table. 使用一张桌子。 They are both users. 他们都是用户。 Your code will have more general use between both types so you will avoid having to do 2 sql queries when dealing with users (even though they are not relevant 99% of the time) 您的代码将在这两种类型之间有更一般的用法,因此您在与用户打交道时将避免执行2个sql查询(即使它们在99%的时间内都不相关)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM