TRUNCATE TABLE

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

Sintassi

TRUNCATE [TABLE] nome_tabella

Spiegazione

TRUNCATE TABLE svuota completamente una tabella. A partire da MySQL 5.1.16 richiede il privilegio DROP. Prima, richiedeva il privilegio DELETE. Si veda GRANT.

Da un punto di vista logico, TRUNCATE TABLE equivale a un'istruzione DELETE che cancella tutte le righe, ma in alcune circostanze vi sono delle differenze pratiche.

Su una tabella InnoDB, lo Storage Engine esegue TRUNCATE TABLE cancellando le righe una per una se vi sono di vincoli associate alle chiavi esterne. Se non ci sono chiavi esterne, InnoDB esegue un troncamento rapido eliminando la tabella originale e ricreandone una vuota con la stessa definizione, il che è molto più veloce che cancellare le righe una ad una. Il contatore AUTO_INCREMENT viene azzerato da TRUNCATE TABLE, indipendentemente dal fatto che possano esserci o meno dei vincoli di chiavi esterne.

Nel caso in cui vi siano dei vincoli di chiavi esterne, InnoDB cancella le righe una ad una e per ognuna elabora i vincoli. Se il vincolo è di tipo DELETE CASCADE, le righe della tabella figlia vengono eliminate, e la tabella troncata viene svuotata. Se i vincoli non sono CASCADE, l'istruzione TRUNCATE cancella le righe (una ad una) ma si ferma se incontra una riga padre a cui è associata una figlia, restituendo l'errore:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1`
FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))

Questo comportamento è identico a quello dell'istruzione DELETE senza clausola WHERE.

Il conteggio delle righe eliminate da TRUNCATE TABLE è accurato solo nel caso in cui viene mappata a un'istruzione DELETE.

For other storage engines, TRUNCATE TABLE differs from DELETE in the following ways in MariaDB 5.1:

  • Truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables.
  • Truncate operations cause an implicit commit.
  • Truncation operations cannot be performed if the session holds an active table lock.
  • Truncation operations do not return a meaningful value for the number of deleted rows. The usual result is "0 rows affected," which should be interpreted as "no information."
  • As long as the table format file tbl_name.frm is valid, the table can be re-created as an empty table with TRUNCATE TABLE, even if the data or index files have become corrupted.
  • The table handler does not remember the last used AUTO_INCREMENT value, but starts counting from the beginning. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.
  • When used with partitioned tables, TRUNCATE TABLE preserves the partitioning; that is, the data and index files are dropped and re-created, while the partition definitions (.par) file is unaffected.
  • Since truncation of a table does not make any use of DELETE, the TRUNCATE statement does not invoke ON DELETE triggers.

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.