Domanda

I have a MySQL database table that has many columns. The primary key is PKEY and one of the non primary key column is USERID. This column is set to be autoincrement & it is not a primary key. I would also like to set a default start value of 1000 so that with row insertions it would start incrementing as 1000, 1001, 1002 etc... How can I accomplish this?

Thanks

È stato utile?

Soluzione

There can only be one Auto increment column, but it can be any. I'm assuming it's currently your PKEY field, so removing it will mean you need to manually create a unique PKEY to keep it as your primary key.

alter table tablename modify PKEY int(11) unsigned NOT NULL;
alter table  tablename add index (USERID);
alter table  tablename modify USERID int(11) unsigned NOT NULL AUTO_INCREMENT;
ALTER TABLE tablename AUTO_INCREMENT = 1000;

a fresh create would look like this:

CREATE TABLE `foo` (
  `PKEY` int(11) unsigned NOT NULL,
 `USERID` int(11) NOT NULL AUTO_INCREMENT,
 PRIMARY KEY (`PKEY`),
 KEY `USERID` (`USERID`)
) ENGINE=InnoDB  AUTO_INCREMENT = 1000;

However, I'd recommend you make USERID a primary key

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top