简体   繁体   English

如何使用主键而不是自动增量创建MySQL

[英]How to create a MySQL with Primary Key not an auto increment

Am trying to create the following table: 我正在尝试创建下表:

create table Cust (Name varchar(50) UNIQUE, Cat1 varchar(50), RowID int NOT NULL AUTO_INCREMENT, PRIMARY KEY (Name));

the error I get is 我得到的错误是

Incorrect table definition; 表定义不正确; there can be only one auto column and it must be defined as a key 只能有一个自动列,并且必须将其定义为键

I want to index by the "Name", not the RowID. 我想按“名称”而不是RowID进行索引。 So even if I end it with: 因此,即使我以:

...PRIMARY KEY (Name, RowID));

It fails. 它失败。 Of course ...PRIMARY KEY (RowID, Name)); 当然...PRIMARY KEY (RowID, Name)); works but is not what I want. 工作,但不是我想要的。

Can someone help me see the light please? 有人可以帮我看看灯吗?

Thanks 谢谢

You just need to make a KEY (aka index) for the auto-inc column. 您只需要为auto-inc列创建一个KEY(又名索引)。 It doesn't have to be the primary key, but it must be the left-most column in some index. 它不必是主键,但必须是某个索引中最左边的列。

create table Cust (
  Name varchar(50), 
  Cat1 varchar(50), 
  RowID int NOT NULL AUTO_INCREMENT, 
  PRIMARY KEY (Name),
  KEY (RowId)
);

Don't add the UNIQUE option to the name column. 不要将UNIQUE选项添加到名称列。 That creates an extra superfluous unique index, which you don't need. 这会创建多余的唯一索引,而您不需要。 Any PRIMARY KEY is already unique. 任何主键已经是唯一的。

I'll comment that auto-inc is not the same thing as rowid. 我要评论一下,自动增量功能与rowid不同。 Don't expect auto-inc to have consecutive values. 不要期望auto-inc具有连续的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM