Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 662
Brett Avatar asked Jun 22 '10 05:06

Brett


People also ask

How can remove auto increment and primary key in MySQL?

You don't want to drop the column but moving forwarding you don't need to have auto increment, instead you would like to insert the values in column manually. To disable or remove the auto increment from a column in MySQL Table, you can simply Modify the column with same data type but without auto_increment clause.

Can a primary key be Auto_increment?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Can a primary key be dropped in MySQL?

You can drop a primary key in MySQL using the ALTER TABLE statement.


Video Answer


1 Answers

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); 
like image 184
Lauri Lehtinen Avatar answered Oct 03 '22 05:10

Lauri Lehtinen