每个表有多个自动增量
大家好,
我正在尝试创建一个表,其中包含一堆引用用户的 id,并且对于每个用户,将有多个帖子也应该有自己的 id。我的问题是如何制作表格,以便当为某个用户插入记录时,他们的帖子 ID 应增加 1。这意味着用户 1 将有帖子 1、2、3 和 4,用户 2 将有帖子1,2,3,4,5,6 和 7 等...
event_id 是表的主键,FK_user_ID 是映射回用户表的外键,post_id 是该用户的帖子编号是(这是我的问题栏),帖子是帖子
----------------------------------------------------
| 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 |
----------------------------------------------------
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 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 |
----------------------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要一个单独的表来存储每个用户的帖子吗?然后,您可以通过用户 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:
etc etc
每个表只能有一个自动增量。如果这是一项家庭作业,则“开箱即用”的方法可能是为每个用户创建单独的表,而不是一个包含多个列的表。然后,当使用单独的表时,可以为每个表使用自动增量。然后,您只需创建一个视图将表连接在一起以便于阅读。
You can only have one auto-increment per table. If this is a homework assignment, the "outside the box" way to do this may be to create separate tables for each user, instead of one table with multiple columns. Then, when using separate tables, you can use auto-increment for each table. Then you would simply create a view to join the tables together for easy reading.
恐怕我认为每个表不能有多个
AUTO_INCRMENT
列。您可以将其放在复合索引的辅助列上,如下所述 此处,但不能有多个。我认为您需要创建一个单独的用户表,将用户 ID (
user.id
) 作为对post.user_id
的外键引用。现在,您可以在
(user_id, post_sequence)
的post
上创建一个PRIMARY KEY
并使用定义
在其列规范中。post_sequence
>AUTO_INCRMENTI'm afraid I don't think you can have more than one
AUTO_INCREMENT
column per table. You can have it on a secondary column in a compound index as described here but you can't have more than one.I think that you need to create a separate user table, have the user id (
user.id
) as a foreign key reference topost.user_id
.Now you can create a
PRIMARY KEY
on thepost
of(user_id, post_sequence)
and definepost_sequence
withAUTO_INCREMENT
in its column spec.使用
MyISAM
,这是可能的:结果是:
With
MyISAM
, this is possible:results in: