简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM