Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are relationship tables really needed?

Tags:

sql

database

Relationship tables mostly contain two columns: IDTABLE1, and IDTABLE2.

Only thing that seems to change between relationship tables is the names of those two columns, and table name.

Would it be better if we create one table Relationships and in this table we place 3 columns:
TABLE_NAME, IDTABLE1, IDTABLE2, and then use this table for all relationships?

Is this a good/acceptable solution in web/desktop application development? What would be downside of this?

Note:
Thank you all for feedback. I appreciate it.
But, I think you are taking it a bit too far... Every solution works until one point.
As data storage simple text file is good till certain point, than excel is better, than MS Access, than SQL Server, than...
To be honest, I haven't seen any argument that states why this solution is bad for small projects (with DB size of few GB).

like image 671
This is it Avatar asked Feb 08 '11 11:02

This is it


People also ask

Why do we need relationships among tables within database?

Table relationships are the foundation upon which you can enforce referential integrity to help prevent orphan records in your database. An orphan record is a record with a reference to another record that does not exist — for example, an order record that references a customer record that does not exist.

Why is it useful to create a relationship between tables?

It helps to further refine table structures and minimize redundant data. As you establish a relationship between a pair of tables, you will inevitably make minor modifications to the table structures. These refinements will make the structures more efficient and minimize any redudant data that the tables may contain.

Is it better to have more tables or more rows?

Which one is more efficient in terms of performance and why? More rows. That is what relational databases are designed for. Multiple tables with the same structure are usually a sign of a bad design.

Are relationships tables in a database?

Database relationships are associations between tables that are created using join statements to retrieve data. The following table describes the database relationships. Both tables can have only one record on each side of the relationship. Each primary key value relates to none or only one record in the related table.


2 Answers

It would be a monster of a table; it would also be cumbersome. Performance-wise, such a table would not be a great idea. Also, foreign keys are impossible to add to such a table. I really can't see a lot of advantages to such a solution.

like image 100
alex Avatar answered Sep 20 '22 11:09

alex


Bad idea.

How would you enforce the foreign keys if IDTABLE1 could contain ids from any table at all?

To achieve acceptable performance on joins without a load of unnecessary IO to bring in completely unrelated rows you would need a composite index with leading column TABLE_NAME that basically ends up partitioning the table into sections anyway.

Obviously even with this pseudo partitioning going on you would still be wasting a lot of space in the table/indexes just repeating the table name for each row.

like image 34
Martin Smith Avatar answered Sep 18 '22 11:09

Martin Smith