Johnathan Au
Johnathan Au

Reputation: 5362

What are the benefits of creating table with only foreign keys in MySQL?

For example, I have a table of Users and a table of Blogs and a separate table called User_To_Blogs which contain only User and Blog IDs.

What are the benefits of such a table as opposed to having a User ID column in the Blog table?

Upvotes: 1

Views: 916

Answers (2)

Mark Leiber
Mark Leiber

Reputation: 3138

Let's say your blog table has information like blog name, blog start date, blog URL, etc. If you have the user ID in that table, every row will have the same blog information repeated. If you define the blog record in its own table, you define it once and reference it as many times as you want in the user-blog table.

Upvotes: 0

Rowland Shaw
Rowland Shaw

Reputation: 38130

These sorts of tables are useful for many-to-many relationships, where a user can have many blogs, and a blog can have many authors, for example.

Perhaps a better example is the person -> employer relationships, where a person can have worked for many employers, and an employer can have many employees

Upvotes: 3

Related Questions