Structuring a MySQl Database for a Blog to include "labels" or "tags"
Basically, I want to encompass the idea of "labels" or "tags" for a rudimentary blog I'm building.
The "posts" table looks like this:
table posts {
post_id (autoincrement, int, primary key)
post_text (text)
published (boolean)
timestamp (timestamp)
published_date (date)
user_id (varchar)
}
table labels {
post_id
label (varchar)
label_id (autoincrement, int, primary key)
}
Is this the best approach?
The main 'problem' I see with this approach is when inserting a new post, because post_id
is auto incrementing in the table posts开发者_JS百科
you don't technically know the post_id
to insert into labels
at the moment of insertion. Doesn't seem like the most efficient solution.
Is there a way to link two tables...?
Thoughts?
you don't technically know the post_id to insert into labels at the moment of insertion
Sure you do, it's in LAST_INSERT_ID()
, or from your PHP code, mysql_insert_id
. As soon as you make the INSERT
query for the post, you know the ID generated and can use it when inserting your tags/labels. This is how you link the tables.
精彩评论