Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    [Mysql] Svuotare tabelle con Foreign Key

    devo svuotare delle tabelle che però sono legate ad altre tramite FK.
    ad esempio devo svuotare la tabella pacchetti e quella articoli che sono legate.
    in articoli c'è un FK che la lega a pacchetti.
    come posso svuotare queste tabelle?
    se provo a svuotare pacchetti mi esce questo errore:
    codice:
    mysql> truncate pacchetti;
    ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`compagnia`.`articoli`, CONSTRAINT `pacchetto_codice_FK` FOREIGN KEY (`pacchetto_codice`) REFERENCES `compagnia`.`pacchetti` (`pacchetto_codice`))

  2. #2
    usando le FK non puoi usare il truncate, devi usare per forza il delete
    The fastest Redis alternative ... cachegrand! https://github.com/danielealbano/cachegrand

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    1,469
    set foreign_key_constraints=0;

  4. #4
    Originariamente inviato da franzauker
    set foreign_key_constraints=0;
    tu dici di fare così:
    codice:
    SET foreign_key_constraints= 0;
    TRUNCATE tb1;
    TRUNCATE tb2;
    TRUNCATE tb3;
    TRUNCATE tb4;
    SET foreign_key_constraints= 1;
    tei chiedo conferma xchè vorrei evitare casini.

  5. #5
    si

    se non ti funziona usa
    SET @SESSION.foreign_key_constraints = 0;

    e poi alla fine

    SET @SESSION.foreign_key_constraints = 1;
    The fastest Redis alternative ... cachegrand! https://github.com/danielealbano/cachegrand

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    1,469
    Originariamente inviato da fermat
    tu dici di fare così:
    codice:
    SET foreign_key_constraints= 0;
    TRUNCATE tb1;
    TRUNCATE tb2;
    TRUNCATE tb3;
    TRUNCATE tb4;
    SET foreign_key_constraints= 1;
    tei chiedo conferma xchè vorrei evitare casini.
    l'uomo saggio fa un dump, prima.
    l'uomo molto saggio verifica anche che il dump sia corretto.

  7. #7
    si dump fatto.
    ho provato ma ho problemi in entrambi i modi:
    codice:
    mysql> SET foreign_key_constraints = 0;
    ERROR 1193 (HY000): Unknown system variable 'foreign_key_constraints'
    mysql> SET @SESSION.foreign_key_constraints = 0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> truncate pacchetti;
    ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`compagnia`.`articoli`, CONSTRAINT `pacchetto_codice_FK` FOREIGN KEY (`pacchetto_codice`) REFERENCES `compagnia`.`pacchetti` (`pacchetto_codice`))

  8. #8
    ho risolto con foreign_key_checks = 0;
    grazie!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.