简体   繁体   中英

Use one-to-one MySQL Relation or not?

I have user table, and I need to expand it with author and partner data. Should I put additional columns in same table or create 2 more tables ( user_author and user_partner ) with one-to-one relationship ?

Tables and columns are simplified for example.

user

id
name
email
password
avatar
is_active

user_author

user_id => user.id (PK, UNQ)
description
amount_per_text
max_text
is_active

user_partner

user_id => user.id (PK, UNQ)
name
description
image_logo
is_active

User cannot be both author and partner. And then I can have classes like User.php for normal users, UserPartner.php and UserAuthor.php which extends User.php class.

Does it make sense or should I just put those columns in user table ?

Both approaches are valid and frequently used.

The normalization theory of a relational schema would not allow to put all the data in a single table for reasons of data consistency. This would favor a multi-table approach.

In the single-table approach you'll end up with a lot of empty (null) fields in every record in the table.

When using multiple tables, however, you may have to perform one or two joins just to find out if a record is a partner or an author. Hence, from a performance viewpoint, the single-table solution is preferrable.

In both cases, you could include an extra field, eg "recordType", in user to quickly and easily discriminate between the different types of records. This will give the best possible runtime performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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