自增Id和key问题(MySQL)
我有表,我尝试添加
`id` int(11) NOT NULL auto_increment,
到表中,但我得到了
ERROR 1075: Incorrect table definition; There can only be one auto column and it must be defined as a key
所以问题是:
- 我如何找到哪一个是关键列
- 我可以毫无问题地更改它并添加新的 ID 字段(带有自动增量)吗?
i have table and i tried to add
`id` int(11) NOT NULL auto_increment,
to table but i get
ERROR 1075: Incorrect table definition; There can only be one auto column and it must be defined as a key
So the questions are:
- How I can find which one is the key column
- Can I change it without problems and add the new ID field (with autoincrement)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果表已创建,请使用 ALTER:
如果正在创建表,则必须将 auto_increment 设置为主键:
If the table is already created use ALTER:
If the table is being created you must set the auto_increment as primary key:
必须将其设为主键
It must be made the primary key
您只需按照 matthewh 的建议运行 show create table 即可找到当前主键,并在输出中查找类似的内容:
PRIMARY KEY (
old_column_name
),如果可以的话,您还应该检查 MySQL Workbench使用它。它使得这样的改变变得非常容易。
You can find the current primary key just by running show create table as matthewh suggested, and look for something like this in the output:
PRIMARY KEY (
old_column_name
),Also you should check out MySQL Workbench if you can use it. It makes making changes like this really easy.
没有人使用无符号整数作为主键。当您甚至不使用 Zerofill 时,为什么还要使用可选的显示宽度即 int(11) 呢!
Doesnt anyone use unsigned integers for primary keys. And why bother using an optional display width i.e. int(11) when you're not even using zerofill !!
首先备份数据库。然后删除与该表关联的所有外键。截断外键表。截断当前表。添加自增id。使用 sqlyog 或 workbench 或 heidisql 或 dbeaver 或 phpmyadmin。然后使用批量更新从备份插入表。
First backup the database. Then drop any foreign key associated with the table. truncate the foreign key table.Truncate the current table.Add the id with auto increment . Use sqlyog or workbench or heidisql or dbeaver or phpmyadmin. Then insert the table from backup using batch update.