English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL临时表

Le tabelle temporanee MySQL sono molto utili quando dobbiamo salvare alcuni dati temporanei. Le tabelle temporanee sono visibili solo nella connessione corrente, quando si chiude la connessione, MySQL elimina automaticamente la tabella e libera tutto lo spazio.

Le tabelle temporanee sono state aggiunte in MySQL 3.23, se la versione del tuo MySQL è inferiore a 3.23 non puoi usare le tabelle temporanee di MySQL. Tuttavia, oggi è raro usare versioni così vecchie di MySQL database server.

Le tabelle temporanee MySQL sono visibili solo nella connessione corrente, se usi uno script PHP per creare tabelle temporanee MySQL, la tabella temporanea viene automaticamente distrutta ogni volta che lo script PHP viene eseguito.

Se hai usato altri programmi client MySQL per connetterti al server MySQL per creare tabelle temporanee, le tabelle temporanee vengono distrutte solo quando si chiude il programma client, ovviamente puoi anche distruggerle manualmente.

Esempio online

Ecco un esempio semplice di come usare le tabelle temporanee MySQL, il codice SQL seguente può essere utilizzato con la funzione mysql_query() di PHP script.

mysql> CREATE TEMPORARY TABLE SalesSummary (
    -> nome_prodotto VARCHAR(50) NON NULL
    -> , vendite_totali DECIMAL(12,2) NON NULL DEFAULT 0.00
    -> , prezzo_unitario_medio DECIMAL(7,2) NON NULL DEFAULT 0.00
    -> , unita_vendute_totali INT NON NULL SIGNED DEFAULT 0
;
Query OK, 0 righe affette (0.00 sec)
mysql> INSERT INTO SalesSummary
    -> (nome_prodotto, vendite_totali, prezzo_unitario_medio, unita_vendute_totali)
    -> VALUES
    -> ('cucumber', 100.25, 90, 2);
mysql> SELECT * FROM SalesSummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber     |      100.25 |          90.00 |                2 |
+--------------+-------------+----------------+------------------+
1 riga nel set (0.00 sec)

Quando usi SHOW TABLESComando per visualizzare l'elenco delle tabelle quando non vedrai la tabella SalesSummary.

Se esci dalla sessione corrente di MySQL e poi usi SELECTComando per leggere i dati della tabella temporanea precedentemente creata, scoprirai che non esiste questa tabella nel database, perché quando sei uscito la tabella temporanea è stata distrutta.

Eliminare le tabelle temporanee MySQL

Per default, quando si interrompe la connessione con il database, le tabelle temporanee vengono automaticamente distrutte. Certo, puoi anche usarla nella sessione corrente di MySQL DROP TABLE Comando per eliminare manualmente una tabella temporanea.

Ecco un esempio di come eliminare manualmente una tabella temporanea:

mysql> CREATE TEMPORARY TABLE SalesSummary (
    -> nome_prodotto VARCHAR(50) NON NULL
    -> , vendite_totali DECIMAL(12,2) NON NULL DEFAULT 0.00
    -> , prezzo_unitario_medio DECIMAL(7,2) NON NULL DEFAULT 0.00
    -> , unita_vendute_totali INT NON NULL SIGNED DEFAULT 0
;
Query OK, 0 righe affette (0.00 sec)
mysql> INSERT INTO SalesSummary
    -> (nome_prodotto, vendite_totali, prezzo_unitario_medio, unita_vendute_totali)
    -> VALUES
    -> ('cucumber', 100.25, 90, 2);
mysql> SELECT * FROM SalesSummary;
+--------------+-------------+----------------+------------------+
| product_name | total_sales | avg_unit_price | total_units_sold |
+--------------+-------------+----------------+------------------+
| cucumber     |      100.25 |          90.00 |                2 |
+--------------+-------------+----------------+------------------+
1 riga nel set (0.00 sec)
mysql> DROP TABLE SalesSummary;
mysql> SELECT * FROM SalesSummary;
ERROR 1146: Tabella 'w3codebox.SalesSummary' non esiste