简体   繁体   English

基于另一个表的动态表插入记录mysql

[英]Dynamic table based on another table insert record mysql

I want to create a posting system to a profile. 我想为个人资料创建一个过帐系统。 I created a database for storing all users posts each user have a table. 我创建了一个数据库来存储所有用户的帖子,每个用户都有一个表。

Ihad created another database for storing the comments of each posts. 我创建了另一个数据库来存储每个帖子的评论。 My logic is to create each table in the comments database and store each comment in that. 我的逻辑是在评论数据库中创建每个表,并在其中存储每个评论。

Is there a logic to link the post and the comments. 有逻辑链接帖子和评论。 I thought to use mysql last insert id but it will return last id which will create error because one of the post will not have a table. 我以为使用mysql的上一个插入ID,但它会返回上一个ID,这会产生错误,因为其中一个帖子没有表格。

Is there any other way? 还有其他办法吗?

Another way would be to have a single table for posts, and identify a user post in the table using a userid column. 另一种方法是拥有一个用于发布的表,并使用userid列在表中标识一个用户发布。 To find all posts by a particular user, simply query by the user's ID. 要查找特定用户的所有帖子,只需按该用户的ID查询即可。 By doing so, you have a single table to manage, and you can do a lookup easily. 通过这样做,你有一个单一的表来管理,你可以很容易地进行查找。 If you create separate tables for each user, you have to create additional logic to first figure out which table to use. 如果为每个用户创建单独的表,则必须创建其他逻辑以首先弄清楚要使用哪个表。 If a user is removed, you delete a table, rather than simply removing some rows from a common table. 如果删除了用户,则删除表,而不是简单地从公用表中删除一些行。

The same logic applies to the comments table - add columns for postid', commentid , userid`. 注释表也采用相同的逻辑-为postid', commentid , userid添加列。 Again, a single table contains all the comments. 同样,单个表包含所有注释。 To find comments on a particular post, you would do a simple query such as 要查找特定帖子的评论,您可以执行简单的查询,例如

  select comment_text
  from   comments_table
  where  postid = ?

The whole purpose of using MySQL is to leverage relationships between entities, ie a user owns posts , a post is linked to comments . 使用MySQL的全部目的是利用实体之间的关系,即user拥有postspost链接到comments

If you do not want to use a relational schema like this, take a look at NoSQL DBs. 如果您不想使用这样的关系模式,请查看NoSQL DB。

You have a couple options here: 您在这里有几个选择:

  • Add a user_id column to your posts table, and a post_id, and user_id column to your comments table. 在您的帖子表中添加一个user_id列,在您的评论表中添加一个post_id和user_id列。 You can then setup foreign keys with one-to-many relationships. 然后,您可以设置具有一对多关系的外键。

  • Only use a single table that has (in addition to your existing) a user_id, and type column. 仅使用具有(除了现有的之外)user_id的单个表,然后键入列。 Type will define comment/post/etc. 类型将定义评论/帖子/等。 This can be defined with intermediary tables as a number mapped to a CONST, string, or any other way that you see fit (intermediary best option imho). 可以使用中间表将其定义为映射到CONST的数字,字符串或您认为合适的任何其他方式(中间最佳选项imho)。

  • Vary the above example and use 2 intermediary tables to match users to posts and comments to posts (possibly also users to comments). 改变上面的示例,并使用2个中间表将用户与帖子匹配,并将评论与帖子匹配(也可能将用户与评论匹配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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