Php 为博客构建MySQl数据库,包括;标签;或;标签;
基本上,我想为我正在构建的一个基本博客包含“标签”或“标签”的概念 “posts”表如下所示:Php 为博客构建MySQl数据库,包括;标签;或;标签;,php,mysql,Php,Mysql,基本上,我想为我正在构建的一个基本博客包含“标签”或“标签”的概念 “posts”表如下所示: 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
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)
}
这是最好的方法吗
我看到这种方法的主要“问题”是在插入新帖子时,因为post\u id
在表posts
中是自动递增的,从技术上讲,您不知道在插入标签时要插入的post\u id
。似乎不是最有效的解决方案
有没有办法链接两个表
想法
从技术上讲,您不知道在插入标签时要插入到标签中的post_id
当然可以,它位于LAST\u INSERT\u ID()。一旦您对帖子进行INSERT
查询,您就知道生成的ID,并且可以在插入标签时使用它。这就是链接表格的方式。你知道什么!?谢谢因此,您建议执行两个单独的查询。首先,在帖子中插入值,然后检索mysql\u insert\u id并插入到labels表中?这不是建议,这是唯一可能的方法。不能使用单个insert
查询将数据插入多个表。是否允许使用公共标签?您不需要当前结构中的label\u id
列,因为post\u id
和label
应该足以使行唯一(当然,除非您希望能够使用相同的标签为一篇文章添加两次标签)。