简体   繁体   中英

mysql, alter column remove primary key and auto incremement

I'm changing my mysql db table from an id (auto) to a uid.

ALTER TABLE companies DROP PRIMARY KEY;
ALTER TABLE companies ADD PRIMARY KEY (`uuid`);

This is the error I get..

[SQL] ALTER TABLE companies DROP PRIMARY KEY;
[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

Which I understand, I need to change the id to a non-autoincrement because I drop it as the primary key.? What is the syntax to change a column to remove primary key and auto increment?

ALTER TABLE companies change id id ?????????? int(11)

If you need to remove the auto-increment and the primary key from the id column in a single SQL statement, this should do:

ALTER TABLE companies DROP PRIMARY KEY, CHANGE id id int(11);

In fact, you should be able to do everything in a single ALTER TABLE query:

ALTER TABLE companies
DROP PRIMARY KEY,
CHANGE id id int(11),
ADD PRIMARY KEY (uuid);

When you're not changing the name of the column you can use MODIFY :

ALTER TABLE `companies` MODIFY `id` int(11), 
                           DROP PRIMARY KEY, 
                   ADD PRIMARY KEY (`uuid`);

By doing this all in one alter statement it's also treated as atomic, so there's no chance of inconsistency between queries (unlike running multiple statement in row).

The query for remove auto increment is:

alter table companies DROP PRIMARY KEY,
change id id int(11) NOT NULL

Now you can see structure of table is without auto increment.

If you want to add primary key another column, then use this query

alter table companies add PRIMARY KEY(uuid)

If you want to drop auto increment,primary key and add primary key to new column in same query, then use this query

alter table comapnies DROP PRIMARY KEY,
   change id id int(11) NOT NULL,
   add PRIMARY KEY(uuid)

This is the syntax to change a column to remove the primary key and auto_increment

ALTER TABLE table_name DROP PRIMARY KEY,
CHANGE old_key_name new_key_name new_column_datatype

In your case this is the answer:

ALTER TABLE companies DROP PRIMARY KEY, CHANGE ID UID;

If we want to drop a primary key, also we need to drop the auto_increment from that primary key, both at the same time using the next syntax:

ALTER TABLE table_name DROP PRIMARY KEY,
MODIFY primary_column_name datatype

there is no need to use the change command because change is used to change the name and datatype of a column so instead of change we can use modify to change only the datatype of a column like in the previous case.

If you have a foreign key column, you need to delete the constraint first. This is how I did to remove the auto increment:

ALTER TABLE portal_user_privileges DROP FOREIGN KEY FK__privileges; 

alter table privileges DROP PRIMARY KEY;

alter table privileges add PRIMARY KEY(id);

ALTER TABLE portal_user_privileges ADD CONSTRAINT FK__privileges FOREIGN KEY (privileges_id) REFERENCES privileges(id)  ON UPDATE NO ACTION ON DELETE NO ACTION;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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