fsasvari
fsasvari

Reputation: 1951

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 ?

Upvotes: 0

Views: 77

Answers (1)

JimmyB
JimmyB

Reputation: 12610

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, e.g. "recordType", in user to quickly and easily discriminate between the different types of records. This will give the best possible runtime performance.

Upvotes: 1

Related Questions