MySQL #1075 - 表定义不正确;只能有一个自动列,并且必须将其定义为键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17633393/
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 18:08:35  来源:igfitidea点击:

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

mysql

提问by Akash Gupta

I have a serial no. column which is auto increment, but I want enrollment id. to be the primary key and MySQL is just not allowing me to do that. Is there any way around to do that?

我有序列号 自动增量列,但我想要注册 ID。成为主键,而 MySQL 只是不允许我这样做。有没有办法做到这一点?

回答by John Woo

You can only define a column as AUTO_INCREMENTif it is a PRIMARY KEYand an INT(not sure of this but BIGINT will work too). Since you want the SerialNoto be set as AUTO_INCREMENT, why not make it as PRIMARY KEYand the EnrollmentIDas UNIQUE?

您只能将一列定义AUTO_INCREMENT为 aPRIMARY KEY和 an INT不确定,但 BIGINT 也可以。既然您希望将 theSerialNo设置为 as AUTO_INCREMENT,为什么不将其设为 asPRIMARY KEYEnrollmentIDasUNIQUE呢?

CREATE TABLE TableName
(
    SerialNo INT AUTO_INCREMENT PRIMARY KEY,
    EnrollmentID INT UNIQUE,
    -- other columns...
)

回答by Niet the Dark Absol

Make sure you define your serial number column as UNIQUE.

确保您将序列号列定义为UNIQUE.