表中的自动增量字段和其他主键
我有一个定义了主键的表(post_id,category_id)。他的表实现了Many_Many关系。如何向该表添加自动增量字段(我通常使用自动增量字段来轻松获取记录并在表单中使用自动增量值)。当我尝试添加 auto_increment 字段时,出现此错误:
Multiple primary key defined
当我有复合主键时是否需要有 auto_increment 字段?如果是的话该怎么做?
i have a table with primary key defined (post_id,category_id).his table implement Many_Many relation. how to add a autoincrement field to this table (i usually use auto_increment field to easily fetch records and use auto_increment value in form). when i try to add auto_increment field i get this error:
Multiple primary key defined
is there any need to have auto_increment fields when i have composite primary key? if yes how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果表有自增字段,则该字段必须是主键。只能有一个主键。
我不会仅仅为了它而创建自动增量,因为这意味着您必须在作为 PK 的自然候选者的字段上添加唯一索引。
在这种情况下,我唯一创建 auto_increment 的情况是自然 PK 包含不合适的数据类型(varchar 等),或者 PK 很大(以字节为单位)或跨越相当多的列
If a table has an auto increment field, it must be the Primary Key. There can only be one Primary Key.
I would not create an auto increment just for the sake of it, since it then means you have to add a unique index on the fields which are a natural candidate for the PK.
The only time I would create an auto_increment in this case, is if the natural PK would consist of unsuitable data types (varchar etc), or the PK would be large (in terms of bytes) or spanning quite a lot of columns
post_id,category_id 应该是连接表的外键。
它应该看起来像这样:
post_id,category_id should be foreign keys of your join table.
it should look like this: