Search code examples
mysqldatabaseschemasails.jsrdbms

When should we go for one-to-one relation model vs adding attributes to same table?


I'm using sails framework to build a website and MySQL as my database. I have a table called "Property". And there are other details like "Total budget", "Hold period", "Rentable area", "investor share", "company share", "sale price" and many more attributes like this which are related to the property in a one-to-one fashion.

I'm unable to decide whether to create separate small tables for these details (by grouping them under small tables like "Equity", "Funding",...) with the "Property ID" as the primary key for each of the small tables(creating one-to-one association on sails) or simply make all these as attributes under the "Property" table. Adding them as attributes to the "Property" table would make the table too big with about 40 columns(Is that a problem?).

NOTE: All these attributes would be mandatory details and not "NULL" anytime.


Solution

  • I would go for only one table, specialy if all fields are mandatory.

    You are very far from the MySQL limit and it will not be a problem.

    Your code will be easier to write and to read.

    It would be better for MySQL performances too because it won't need to JOIN tables when retrieving data.


    Edit. A one-to-one relation could be used in other use cases. For example, let's say you have a table "animal" which contains the columns "varchar name", "integer age" and "decimal weight".

    But the stored animals could have some specific properties stored in another table depending on their family.

    • a "dog" table with columns "integer owner_id", "enum breed"
    • a "bird" table with columns "boolean can_fly", "integer number_of_eggs"
    • a "fish" table with columns "integer number_of_gils", "boolean live_in_freshwater"

    Rantanplan the dog, Donald the duck and Nemo the fish could all be stored in the "animal" table and have their specific properties stored in another table with a one-to-one relation.

    This is a silly example but it gives an idea of a use case.