mysql表格自动增量,MySQL:#1075-错误的表格定义;自动增量与另一个键?

Here is a table in MySQL 5.3.X+ db:

CREATE TABLE members` (

`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,

`memberid` VARCHAR( 30 ) NOT NULL ,

`Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,

`firstname` VARCHAR( 50 ) NULL ,

`lastname` VARCHAR( 50 ) NULL ,

UNIQUE (memberid),

PRIMARY KEY (id)

) ENGINE = MYISAM;

Id column is never used in queries, it is just for visual convenience (so it's easy to see how the table grows). Memberid is an actual key, is unique, and memberid is used in queries to identify any member (WHERE memberid='abcde').

My question is: how to keep auto_increment, but make memberid as a primary key? Is that possible?

When I try to create this table with PRIMARY KEY (memberid), I get an error:

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

What is the best choice (Hopefully, there is a way to keep id column so performance is good and queries identify any user by memberid, not by id), if the performance is very important (although the disk space is not)?

解决方案

You can have an auto-Incrementing column that is not the PRIMARY KEY, as long as there is an index (key) on it:

CREATE TABLE members (

id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,

memberid VARCHAR( 30 ) NOT NULL ,

`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,

firstname VARCHAR( 50 ) NULL ,

lastname VARCHAR( 50 ) NULL ,

PRIMARY KEY (memberid) ,

KEY (id) --- or: UNIQUE KEY (id)

) ENGINE = MYISAM;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值