views:

60

answers:

4

When designing a database, what usually determines what tables will be the primary and foreign table in a relationship?

For example, if I have a table called posts and it contains and id column postid and I have a table called comments and it contains a column called postid. Which of these tables would be the primary one in the relationship. I would assume it is the posts table. I am saying this because this is a one-to-many relationship and it seems like the table with one entry would be the primary one and the table with the many would be the foreign table.

What about many to many relationships or 1 to 1 relationships, what are the primary and foreign tables in those scenarios?

+3  A: 

Given:

table posts
post_id

table comments
comment_id
post_id

table posts_to_comments
post_comment_id
post_id
comment_id
  • posts.post_id is primary key for table posts.
  • comments.comment_id is primary key for table comments.
  • comments.post_id is a foreign key to table posts.

posts would be your "primary" table as you're thinking of it.

For a many-many relationship: (doesn't make too much sense here, but anyway)

  • posts_to_comments.post_comment_id is primary key
  • posts_to_comments.post_id is foreign key to posts
  • posts_to_comments.comment_id is foreign key to comments
Nathan
I guess it makes sense when I think of it that way.
Xaisoft
Your many to many example confused me a bit, what if I used an Authors table as an example where Authors to Posts were many to many, what is the primary and foreign tables in that scenario?
Xaisoft
You'll have to stop thinking about tables as primary and foreign. Primary and foreign keys are attributes of a column, not a table.m-m relationships really end up being two 1-m relationships.So your m-m author to post relationship becomes:1 post to many 'author_to_posts'1 author to many 'author_to_posts'
Nathan
ok, it is making more sense now.
Xaisoft
+2  A: 

I never heard about primary and foreign used to indicate tables. They are characteristic of columns.

In the example you give:

  • in Posts, the postid column is the primary key for the table Posts
  • in Comments, the postid column is a regular field for the table Comments, and designates a unique row in the table Posts ; it is called the foreign key for Posts (=key of the other table).
KLE
Makes sense now when I think about it, I guess if it is the foreign key, it probably should be the foreign table in the relationship.
Xaisoft
Yes, somehow. But the Comments table might also have a commentid column, and another table might reference it also. So you can see that each table would be the primary if you think about one column, and foreign if you think about another ... It feels messy. We normally consider the pair of columns together as one relationship (or constraint in database terms) : one table defines an id (primary key), one table references that id (foreign key). When you make a JOIN query on the two tables, the primary key and the foreign key must have exactly the same values....
KLE
+3  A: 

The primary table contains the parent records, those records that define the root records such as "posts" in this example.

The foreign tables contain child records, those records that add data to parent records in a related way.

So, a "comment" is a child of a "post", therefore: "Post" is parent (primary in your example) "Comment" is child (foreign in your example)

PostId values must be unique in the Post table... but the same PostId value can occur multiple times in the Comment table (because there might be many comments for a single post; comment 1 is for post 1, comment 2 is for post 1).

1-1 relationships are when two entities are peers. i.e. a Student may be a User and a User may be a Student. Two students cannot be the same user. Two users cannot be the same student. Therefore User - Student are 1-1.

Many to many relationships are best modeled with a table in between.

Book (primary)
Author (primary)
AuthorBooks (mapping)

BookId is unique in Books (only one book may have a certain id)
AuthorId is unique in Authors (only one author may have a certain id)

AuthorBooks has both BookId and AuthorId columns and maps books to authors.

This relationship is modeled because an author may have written many books and because a particular book may have many authors.

Stuart Thompson
Nice explanation. Are m1 to 1 relationships best modeled with a table in between as well?
Xaisoft
1-1 relationships typically just have 2 peer tables. There is no need for a table in between because each of the parent keys exists only once in each table.
Stuart Thompson
In a 1-1 relationship, set the primary ley as the table where the record must first be inserted if you can define one. If you can't define one, then you cannot use a primary key-foreign key relationship and will need to enforce the relationship through a trigger.
HLGEM
+1  A: 

For many to many relationships, such as Networks and Users, where networks can have many members, and users can belong to many networks, you'll need to introduce a third table, which can be called something like (choose whichever you find most appropriate):

  • Networks_Have_Members
  • Users_BelongsTo_Networks
  • NetworkMembers

This table will have a composite primary key, consisting of userId and networkId. Additionally, userId will reference Users.id as a foreign key, and networkId will reference Networks.id as a foreign key.

PatrikAkerstrand
I saw this in a few other answers as well. Is this best practice?
Xaisoft
Yes. It will allow you to keep redundant data from the primary tables. If you did not introduce a third table, you would need to duplicate rows in the primary tables and just change the foreign key column. Duplication is bad and will lead to a lot of problems with CRUD.
PatrikAkerstrand
Necessarily we cannot have duplicate rows in our primary tables: a primary key has to be unique.
APC
@APC: True, but most people seem to use AUTO_INCREMENTING primary keys, which make it very easy to duplicate data in the primary tables.
PatrikAkerstrand