Domanda

So che questo non funziona, l'ho provato in varie forme e fallito tutti i tempi. Qual è il modo più semplice per ottenere il seguente risultato?

ALTER TABLE XYZ AUTO_INCREMENT = (select max(ID) from ABC);

Questo è grande per progetti di automazione. Grazie!

SELECT @max := (max(ID)+1) from ABC;        -> This works!
select ID from ABC where ID = (@max-1);     -> This works!
ALTER TABLE XYZ AUTO_INCREMENT = (@max+1);  -> This fails :( Why?
È stato utile?

Soluzione

preparati Normativa :

  SELECT @max := MAX(ID)+ 1 FROM ABC; 

  PREPARE stmt FROM 'ALTER TABLE ABC AUTO_INCREMENT = ?';
  EXECUTE stmt USING @max;

  DEALLOCATE PREPARE stmt;

Altri suggerimenti

la documentazione MySQL , questo ha funzionato per me in mysql 5.7:

SET @m = (SELECT MAX(id) + 1 FROM ABC); 
SET @s = CONCAT('ALTER TABLE XYZ AUTO_INCREMENT=', @m);
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

Sto creando uno script di database trasformazione automatizzata per una nuova versione della mia applicazione.

In una tabella, avevo bisogno di modificare il campo di incremento automatico primario a un campo diverso. Dal momento che questa pagina si avvicinò prime molte volte mentre Ho cercato su google una soluzione per esso, ecco una soluzione che alla fine ha funzionato per me:

-- Build a new ID field from entry_id, make it primary and fix the auto_increment for it:
ALTER TABLE  `entries` ADD  `id` INT UNSIGNED NOT NULL FIRST;
UPDATE entries SET id = entry_id;
ALTER TABLE  `entries` ADD PRIMARY KEY (  `id` );

-- ...the tricky part of it:
select @ai := (select max(entry_id)+1 from entries);
set @qry = concat('alter table entries auto_increment=',@ai);
prepare stmt from @qry; execute stmt;

-- ...And now it's possible to switch on the auto_increment:
ALTER TABLE  `entries` CHANGE  `id`  `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT;

che sta avendo problemi con la stmt PREPARARE DA 'ALTER TABLE XYZ AUTO_INCREMENT =?' può usare

CREATE PROCEDURE reset_xyz_autoincrement
BEGIN
      SELECT @max := MAX(ID)+ 1 FROM ABC; 
      set @alter_statement = concat('ALTER TABLE temp_job_version AUTO_INCREMENT = ', @max);
      PREPARE stmt FROM @alter_statement;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;
END

Reset incremento automatico ID.

http://community.spiceworks.com/scripts/ mostra / 3042-reset-auto-incremento-ids

aggiornare tutte le colonne incremento automatico in un database per il valore più piccolo possibile in base a valori correnti nei database. Avevamo bisogno di fare questo dopo la pulizia di un database.

Utilizzare una dichiarazione preparata all'interno di una stored procedure:

drop PROCEDURE if exists reset_autoincrement;
DELIMITER //
CREATE PROCEDURE reset_autoincrement (IN schemaName varchar(255))
 BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE o_name VARCHAR(255);
    DECLARE o_table VARCHAR(255);
    DECLARE cur1 CURSOR FOR SELECT COLUMN_NAME, TABLE_NAME FROM information_schema.`COLUMNS` WHERE extra LIKE '%auto_increment%' and table_schema=schemaName;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    OPEN cur1;
    read_loop: LOOP
     FETCH cur1 INTO o_name, o_table;

     IF done THEN
       LEAVE read_loop;
     END IF;

  set @qry1 = concat('SELECT MAX(`',o_name,'`) + 1 as autoincrement FROM `',o_table,'` INTO @ai'); 
  PREPARE stmt1 FROM @qry1;
  EXECUTE stmt1;

  IF @ai IS NOT NULL THEN
      SELECT  o_name, o_table;
   select @qry1;
   select @ai;
   set @qry2 = concat('ALTER TABLE `',o_table,'` AUTO_INCREMENT = ', @ai);
   select @qry2;
   PREPARE stmt2 FROM @qry2;
   EXECUTE stmt2;
  END IF;

    END LOOP;

    CLOSE cur1;
 END //
DELIMITER ;


call reset_autoincrement('my_schema_name');

Ok ragazzi. Sono venuto su con una soluzione non così intuitivo. La parte migliore è che funziona!

SELECT @max := max(ID) from ABC;       
ALTER TABLE XYZ AUTO_INCREMENT = 1;
ALTER TABLE XYZ ADD column ID INTEGER primary key auto_increment;
UPDATE XYZ SET ContactID = (ContactID + @max);

Personalmente probabilmente sarei utilizzare sia uno script di shell o un'applicazione po 'C # / C ++ o uno script PHP / Rubino / Perl per fare questo in 2 domande:

  • Afferra il valore che si desidera SELECT MAX(ID) FROM ABC;
  • Modificare la tabella utilizzando il ALTER TABLE XYZ AUTO_INCREMENT = <insert value retrieved from first query here> valore

Ovviamente facendo attenzione che il nuovo incremento automatico non causerà alcun scontri chiave con i dati esistenti nella tabella XYZ.

Se si vuole veramente fare questo in MySQL da solo, si può semplicemente scaricare il comando alter costruita dinamicamente in un file su disco e quindi eseguirlo.

In questo modo:

select concat('ALTER TABLE XYZ AUTO_INCREMENT = ',max(ID)+1,';') as alter_stmt
into outfile '/tmp/alter_xyz_auto_increment.sql'
from ABC;

\. /tmp/alter_xyz_auto_increment.sql

Io sono solo un dilettante, ma ho un suggerimento :-) Se si riempie la vostra intera tabella con i nuovi dati, si può semplicemente utilizzare prima del riempimento:

mysql_query( "TRUNCATE TABLE `your_table`" ) or die( mysql_error());

Voglio dire, si resetta anche auto_increment (e cancella tutti i contenuti; Sì, tavolo resterà).

È possibile formare il backup dei dati, il dopo troncare inserto in questo senza id.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top