自增Id和key问题(MySQL)

发布于 2024-10-22 07:07:36 字数 321 浏览 0 评论 0原文

我有表,我尝试添加

`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

所以问题是:

  1. 我如何找到哪一个是关键列
  2. 我可以毫无问题地更改它并添加新的 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:

  1. How I can find which one is the key column
  2. Can I change it without problems and add the new ID field (with autoincrement)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

烙印 2024-10-29 07:07:36

如果表已创建,请使用 ALTER:

ALTER TABLE `table` ADD COLUMN `id` INT(11) NOT NULL AUTO_INCREMENT, 
DROP PRIMARY KEY, 
ADD PRIMARY KEY (`id`)

如果正在创建表,则必须将 auto_increment 设置为主键:

CREATE TABLE IF NOT EXISTS `database`.`table` (
`id`INT(11) NOT NULL AUTO_INCREMENT ,
`a` VARCHAR(45) NULL ,
`b` VARCHAR(255) NULL ,
`c` VARCHAR(45) NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci

If the table is already created use ALTER:

ALTER TABLE `table` ADD COLUMN `id` INT(11) NOT NULL AUTO_INCREMENT, 
DROP PRIMARY KEY, 
ADD PRIMARY KEY (`id`)

If the table is being created you must set the auto_increment as primary key:

CREATE TABLE IF NOT EXISTS `database`.`table` (
`id`INT(11) NOT NULL AUTO_INCREMENT ,
`a` VARCHAR(45) NULL ,
`b` VARCHAR(255) NULL ,
`c` VARCHAR(45) NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
笑叹一世浮沉 2024-10-29 07:07:36
`id` int(11) NOT NULL auto_increment primary key,

必须将其设为主键

`id` int(11) NOT NULL auto_increment primary key,

It must be made the primary key

杀手六號 2024-10-29 07:07:36
ALTER TABLE `database`.`table_name` ADD COLUMN `new_column_name` INT NOT NULL AUTO_INCREMENT  AFTER `last_column_name_in_the_table` , CHANGE COLUMN `old_column_name` `old_column_name` INT(11) NOT NULL  

, DROP PRIMARY KEY 

, ADD PRIMARY KEY (`new_column_name`) ;

您只需按照 matthewh 的建议运行 show create table 即可找到当前主键,并在输出中查找类似的内容:

PRIMARY KEY (old_column_name),

如果可以的话,您还应该检查 MySQL Workbench使用它。它使得这样的改变变得非常容易。

ALTER TABLE `database`.`table_name` ADD COLUMN `new_column_name` INT NOT NULL AUTO_INCREMENT  AFTER `last_column_name_in_the_table` , CHANGE COLUMN `old_column_name` `old_column_name` INT(11) NOT NULL  

, DROP PRIMARY KEY 

, ADD PRIMARY KEY (`new_column_name`) ;

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.

与君绝 2024-10-29 07:07:36

没有人使用无符号整数作为主键。当您甚至不使用 Zerofill 时,为什么还要使用可选的显示宽度即 int(11) 呢!

drop table if exists foo;
create table foo
(
 id int unsigned not null auto_increment primary key,
 ...
)
engine=innodb;

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

drop table if exists foo;
create table foo
(
 id int unsigned not null auto_increment primary key,
 ...
)
engine=innodb;
坏尐絯℡ 2024-10-29 07:07:36

首先备份数据库。然后删除与该表关联的所有外键。截断外键表。截断当前表。添加自增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.

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