我需要在MySQL中将不是主键的字段自动递增 [英] I need to auto_increment a field in MySQL that is not primary key
本文介绍了我需要在MySQL中将不是主键的字段自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在,我有一个主键为auto_increment
字段的表.但是,我需要将主键设置为username
,date
(以确保没有重复的用户名和日期).
Right now, I have a table whose primary key is an auto_increment
field. However, I need to set the primary key as username
, date
(to ensure that there cannot be a duplicate username with a date).
但是,为了更改行信息(添加和删除),我需要auto_increment
字段.
I need the auto_increment
field, however, in order to make changes to row information (adding and deleting).
在这种情况下通常会做什么?
What is normally done with this situation?
谢谢!
推荐答案
只需在(用户名,日期)的组合上设置唯一索引.
Just set a unique index on composite of (username, date).
ALTER TABLE `table` ADD UNIQUE INDEX `name` (`username`, `date`);
或者,您可以尝试
ALTER TABLE `table` DROP PRIMARY KEY, ADD PRIMARY KEY(`username`,`date`);
我认为在后一种情况下,您需要将这些列声明为NOT NULL.
and I think in the latter case you need those columns to be declared NOT NULL.
这篇关于我需要在MySQL中将不是主键的字段自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文