标题可能不是很明显,我会尽量解释我想要的。 我在这里发现了很多类似我的问题,但答案对我没有帮助。
假设我有这张表,它是视频网站中的字段:
Comments
ID | IDuser | IDvideo | comment
我想让我的主键成为 ID 和 IDuser 字段的组合。 IDuser 来自用户表。 ID 是一个 auto_increment 列。
但我希望它像这个例子一样工作。
1 | 2 | 3 | "this is the first comment of user 2 in the video 3"
2 | 2 | 4 | "this is the first comment of user 2 in the video 4"
3 | 2 | 4 | "this is the second comment of user 2 in the video 4"
1 | 3 | 3 | "this is the first comment of user 3 in the video 3"
你看到了吗?就像每个用户都有自己的 auto_increment 范围。
我想这样做,否则第一列的数字会非常大,因为对于每条评论,您都会有一个连续的 ID。
你对我有什么建议吗?
非常感谢。
最佳答案
- 首先,您不能那样使用 AutoIncrement。
- 第二,你不应该。
AutoIncrement 用于为表创建 ID
行。每行应该有它自己的唯一 ID。
如果你想为用户评论提供一个相对 ID,你应该创建一个新的列,其值应该手动设置。
祝你好运!
编辑
在您的情况下,您的表格应该是这样的:
id
INT 无符号自动递增idUser
INT 未签名索引外键idVideo
INT 未签名索引外键idUserComment
INT 未签名注释
Varchar
但我不建议您使用亲属 ID。除非它对您的应用程序有意义
关于具有自动增量列重新启动的mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068985/