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;
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
ERROR 1075(42000) : 表定义不正确; 只能有一个 auto 列,并且必须将其定义为键
因此,将 primary key添加到 auto_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;
我的 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;
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;
CREATE TABLE book (
id INT AUTO_INCREMENT NOT NULL AUTO_INCREMENT,
accepted_terms BIT(1) NOT NULL,
accepted_privacy BIT(1) NOT NULL,
PRIMARY KEY (accepted_terms, id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
但是下面的工作很好(注意 PRIMARY KEY的顺序) :
CREATE TABLE book (
id INT AUTO_INCREMENT NOT NULL AUTO_INCREMENT,
accepted_terms BIT(1) NOT NULL,
accepted_privacy BIT(1) NOT NULL,
PRIMARY KEY (id, accepted_terms)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;