每张表有一个以上的自动增量 [英] Have more than one AutoIncrement Per Table
问题描述
大家好,
我正在尝试创建一个表,该表将具有一堆引用用户的ID,并且对于每个用户,将有多个帖子也应具有自己的ID.我的问题是如何制作表格,以便在为特定用户插入记录时,其帖子ID应该增加1.这意味着用户1将具有帖子1,2,3和4,用户2将具有帖子1,2,3,4,5,6和7,等等
Hey Everybody,
I'm trying to make a table that will have a bunch of ids referring to a user, and for each user there will be multiple posts that should also have their own ids. My question is how do I make the table so that when a record is inserted for a certain user, their post id should be incremented by 1. Meaning user 1 will have posts 1,2,3, and 4, user 2 will have posts 1,2,3,4,5,6, and 7, etc...
event_id是表的主键,FK_user_ID是映射回用户表的外键,post_id是该用户的帖子是哪个数字(这是我的问题列),而post是该帖子>
event_id is the primary key for the table, FK_user_ID is the foreign key that maps back to a user table, post_id is which number the post for that user is (which is my problem column), and post is the post
----------------------------------------------------
| event_id | FK_user_ID | post_id | post |
| 1 | 1 | 1 | hey |
| 2 | 1 | 2 | you too |
| 3 | 1 | 3 | ok |
| 4 | 2 | 1 | foo |
| 5 | 2 | 2 | bar |
----------------------------------------------------
推荐答案
您是否不需要单独的表格来存储每个用户的帖子?然后,您可以通过用户ID链接它们,并在需要时使用联接获取信息:
Would you not require a separate table to store the posts per user? Then you can link them via the user id and use joins to get the information out when required:
---------------------
| User_ID | Post_ID |
| 1 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 5 |
---------------------
等等等
这篇关于每张表有一个以上的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!