Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql - Add auto_increment to primary key

I have a strange problem with mysql.

I am trying to alter a table's column which is a primary key and has an auto_increment constraint defined on it. This is also a foreign key reference for multiple other tables. I need to change the length of this column in both , parent and all children.

set foreign_key_checks=0;
alter table Parent  modify Identifier smallint(10) unsigned;
alter table Child_1 modify FK_Identifier smallint(10) unsigned;
alter table Child_2 modify FK_Identifier smallint(10) unsigned;
alter table Child_3 modify FK_Identifier  smallint(10) unsigned;
alter table Child_4 modify FK_Identifier smallint(10) unsigned;
alter table Child_5 modify FK_Identifier smallint(10) unsigned;
set foreign_key_checks=1;

This removes the auto increment on the parent table. What would be the best way to add the constraint back ?

The below seems to be failing.

mysql> ALTER TABLE Parent MODIFY Identifier smallint(10) PRIMARY KEY AUTO_INCREMENT;
ERROR 1068 (42000): Multiple primary key defined


ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT;
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110125 15:49:08 Error in foreign key constraint of table db/Child_1:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match to the ones in table. Constraint:
,
  CONSTRAINT Child_1_ibfk_1 FOREIGN KEY (FK_Identifier) REFERENCES RoomProfile (Identifier) ON DELETE CASCADE ON UPDATE CASCADE
The index in the foreign key in table is PRIMARY

Is there a better way to achieve this ?

Edit : Show create is (after alter) :

  CREATE TABLE `Parent` (
  `Identifier` smallint(10) unsigned NOT NULL default '0',
  `Name` varchar(20) default NULL,
  `Description` varchar(100) default NULL,
   PRIMARY KEY  (`Identifier`),
   UNIQUE KEY `Name` (`Name`),
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 

before alter

`Identifier` smallint(5) unsigned NOT NULL AUTO_INCREMENT,

Thanks!

like image 905
Ricko M Avatar asked Jan 25 '11 15:01

Ricko M


People also ask

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.

How do I change my primary key to auto increment?

To change a primary key to auto_increment, you can use MODIFY command. Let us first create a table. Look at the above sample output, StudentId column has been changed to auto_increment.

Does primary key auto increment MySQL?

One of the important tasks while creating a table is setting the Primary Key. The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.

How do I make a column auto increment in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.


1 Answers

You don't need to specify PRIMARY KEY in the MODIFY statement:

ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT;
like image 183
Arnaud Le Blanc Avatar answered Oct 19 '22 17:10

Arnaud Le Blanc