2011-01-25 42 views
14

Tengo un extraño problema con mysql.Mysql - Agregar auto_increment a la clave principal

Estoy intentando modificar la columna de una tabla que es una clave principal y tiene una restricción auto_increment definida en ella. Esta es también una referencia de clave externa para otras tablas múltiples. Necesito cambiar la longitud de esta columna en ambos, padre y todos los hijos.

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; 

Esto elimina el incremento automático en la tabla principal. ¿Cuál sería la mejor manera de agregar la restricción?

Lo siguiente parece estar fallando.

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 

¿Hay una mejor manera de lograr esto?

Editar: Mostrar crear es (después de 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 | 

antes alter

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

Gracias!

+0

Se puede publicar el resultado de SHOW CREATE TABLE Parent' – Mchl

Respuesta

30

No es necesario especificar PRIMARY KEY en la declaración CAMBIAR:

ALTER TABLE Parent MODIFY Identifier smallint(10) AUTO_INCREMENT; 
+0

He actualizado el post con el resultado de la misma. –

+2

Pruebe a desactivar 'foreign_key_checks' cuando ejecuta esto. – Mchl

+0

set de configuración foreign_key_checks = 0; y ejecutar lo anterior no tiene ningún efecto. :( –

Cuestiones relacionadas