2011-01-25 23 views
14

Ho uno strano problema con mysql.Mysql - Aggiungi auto_increment alla chiave primaria

Sto tentando di modificare la colonna di una tabella che è una chiave primaria e ha un vincolo auto_increment definito su di essa. Questo è anche un riferimento a chiave esterna per più tabelle. Ho bisogno di cambiare la lunghezza di questa colonna in entrambi, genitore e tutti i bambini.

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; 

Questo rimuove l'incremento automatico sulla tabella padre. Quale sarebbe il modo migliore per aggiungere il vincolo?

Il seguente sembra non funzionare.

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 

C'è un modo migliore per raggiungere questo obiettivo?

Edit: Mostra il creare è (dopo l'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 | 

prima alter

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

Grazie!

+0

potete inserire il risultato di 'SHOW CREATE TABLE Parent' – Mchl

risposta

30

Non è necessario specificare PRIMARY KEY nella dichiarazione MODIFICA:

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

Ho aggiornato il post con il risultato dello stesso. –

+2

Provare a disabilitare 'foreign_key_checks' quando si esegue questo. – Mchl

+0

impostazione set foreign_key_checks = 0; e l'esecuzione di quanto sopra non ha alcun effetto. :( –

Problemi correlati