每个表有多个自动增量

发布于 2024-11-05 22:09:29 字数 692 浏览 4 评论 0原文

大家好,
我正在尝试创建一个表,其中包含一堆引用用户的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云裳 2024-11-12 22:09:29

您不需要一个单独的表来存储每个用户的帖子吗?然后,您可以通过用户 ID 链接它们,并在需要时使用连接来获取信息:

---------------------
| User_ID | Post_ID |
|    1    |    1    |
|    2    |    2    |
|    2    |    3    |
|    3    |    4    |
|    4    |    5    |
---------------------

等等

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

etc etc

久光 2024-11-12 22:09:29

每个表只能有一个自动增量。如果这是一项家庭作业,则“开箱即用”的方法可能是为每个用户创建单独的表,而不是一个包含多个列的表。然后,当使用单独的表时,可以为每个表使用自动增量。然后,您只需创建一个视图将表连接在一起以便于阅读。

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.

猫烠⑼条掵仅有一顆心 2024-11-12 22:09:29

恐怕我认为每个表不能有多个 AUTO_INCRMENT 列。您可以将其放在复合索引的辅助列上,如下所述 此处,但不能有多个。

我认为您需要创建一个单独的用户表,将用户 ID (user.id) 作为对 post.user_id 的外键引用。

现在,您可以在 (user_id, post_sequence)post 上创建一个 PRIMARY KEY 并使用 定义 post_sequence >AUTO_INCRMENT 在其列规范中。

I'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 to post.user_id.

Now you can create a PRIMARY KEY on the post of (user_id, post_sequence) and define post_sequence with AUTO_INCREMENT in its column spec.

别再吹冷风 2024-11-12 22:09:29

使用MyISAM,这是可能的:

CREATE TABLE posts (user_id INT NOT NULL, post_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (user_id, post_id)) ENGINE=MyISAM;

INSERT
INTO    posts
VALUES
(1, NULL),
(1, NULL),
(1, NULL),
(2, NULL),
(2, NULL);

结果是:

1, 1
1, 2
1, 3
2, 1
2, 2

With MyISAM, this is possible:

CREATE TABLE posts (user_id INT NOT NULL, post_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (user_id, post_id)) ENGINE=MyISAM;

INSERT
INTO    posts
VALUES
(1, NULL),
(1, NULL),
(1, NULL),
(2, NULL),
(2, NULL);

results in:

1, 1
1, 2
1, 3
2, 1
2, 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文