It will be better to explain my question on a example. Here is sample tables (InnoDB):
— posts
- id
- title
- title_slug
- content
— tags
- id
- name
- name_slug
— post_tag_relation
- postID
- tagID
Let say I am creating simple post system with tag and I insert into post_tag_relation which posts have which tags. postID and tagID are referenced from their table. Here is my question, should I specify id (primary key) in post_tag_relation or is there a better way to create it? What do you suggest to create post_tag_relation table?
You don’t need to specify a seperate ID field for the post_tag_relation table. The combination of postID and tagID should be unique and can form a compound key. The posts and tags tables form a many-to-many relationship and a junction table (like your post_tag_relation table) is used to relate them. If you don’t want to store additional information about the relationship between a tag and a post (e.g. a timestamp indicating when the tag was associated with the post) then the only fields you need in the post_tag_relation table are the foreign keys from the posts and tags tables (ie. postID and tagID).