MySQL 只能有一个自动列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8645889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:10:34  来源:igfitidea点击:

There can be only one auto column

mysqlddl

提问by BuddyJoe

How do I correct the error from MySQL 'you can only have one auto increment column'.

如何更正 MySQL 中的错误“您只能有一个自动增量列”。

CREATE TABLE book (
   id INT AUTO_INCREMENT NOT NULL,
   accepted_terms BIT(1) NOT NULL,
   accepted_privacy BIT(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

回答by emstol

My MySQL says "Incorrect table definition; there can be only one auto column and it must be defined as a key" So when I added primary key as below it started working:

我的 MySQL 说“不正确的表定义;只能有一个自动列,它必须被定义为一个键”所以当我添加如下主键时,它开始工作:

CREATE TABLE book (
   id INT AUTO_INCREMENT NOT NULL,
   accepted_terms BIT(1) NOT NULL,
   accepted_privacy BIT(1) NOT NULL,
   primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

回答by TMS

The full error message sounds:

完整的错误消息听起来:

ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

ERROR 1075 (42000):表定义不正确;只能有一个自动列,并且必须将其定义为键

So add primary keyto the auto_incrementfield:

所以添加primary keyauto_increment字段:

CREATE TABLE book (
   id INT AUTO_INCREMENT primary key NOT NULL,
   accepted_terms BIT(1) NOT NULL,
   accepted_privacy BIT(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

回答by Matthew Read

Note also that "key" does not necessarily mean primarykey. Something like this will work:

另请注意,“键”不一定表示键。像这样的事情会起作用:

CREATE TABLE book (
    isbn             BIGINT NOT NULL PRIMARY KEY,
    id               INT    NOT NULL AUTO_INCREMENT,
    accepted_terms   BIT(1) NOT NULL,
    accepted_privacy BIT(1) NOT NULL,
    INDEX(id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

This is a contrived example and probably not the best idea, but it can be very useful in certain cases.

这是一个人为的例子,可能不是最好的主意,但它在某些情况下非常有用。

回答by Deept Raghav

CREATE TABLE book (
   id INT AUTO_INCREMENT primary key NOT NULL,
   accepted_terms BIT(1) NOT NULL,
   accepted_privacy BIT(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1