SQL: tabelle temporanee

Cosa sono le tabelle temporanee?

Esistono RDBMS, che supportano le tabelle temporanee. Le tabelle temporanee sono un'ottima funzionalità che ti consentestore and process intermediate results utilizzando le stesse funzionalità di selezione, aggiornamento e unione che è possibile utilizzare con le tabelle tipiche di SQL Server.

Le tabelle temporanee potrebbero essere molto utili in alcuni casi per conservare i dati temporanei. La cosa più importante che dovrebbe essere nota per le tabelle temporanee è che verranno eliminate al termine della sessione client corrente.

Le tabelle temporanee sono disponibili in MySQL dalla versione 3.23 in poi. Se usi una versione precedente di MySQL alla 3.23, non puoi usare tabelle temporanee, ma puoi usareheap tables.

Come affermato in precedenza, le tabelle temporanee dureranno solo finché la sessione è attiva. Se esegui il codice in uno script PHP, la tabella temporanea verrà distrutta automaticamente al termine dell'esecuzione dello script. Se sei connesso al server di database MySQL tramite il programma client MySQL, la tabella temporanea esisterà fino a quando non chiuderai il client o distruggerai manualmente la tabella.

Esempio

Ecco un esempio che mostra l'utilizzo di una tabella temporanea.

mysql> CREATE TEMPORARY TABLE SALESSUMMARY (
   -> product_name VARCHAR(50) NOT NULL
   -> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
   -> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
   -> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO SALESSUMMARY
   -> (product_name, total_sales, avg_unit_price, total_units_sold)
   -> 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 row in set (0.00 sec)

Quando si immette un comando SHOW TABLES, la tabella temporanea non verrà elencata nell'elenco. Ora, se ti disconnetti dalla sessione MySQL e quindi emetti un comando SELECT, non troverai dati disponibili nel database. Anche la tua tabella temporanea non esisterà.

Eliminazione di tabelle temporanee

Per impostazione predefinita, tutte le tabelle temporanee vengono eliminate da MySQL quando la connessione al database viene terminata. Tuttavia, se desideri eliminarli nel frattempo, puoi farlo emettendo un fileDROP TABLE comando.

Di seguito è riportato un esempio su come eliminare una tabella temporanea.

mysql> CREATE TEMPORARY TABLE SALESSUMMARY (
   -> product_name VARCHAR(50) NOT NULL
   -> , total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00
   -> , avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00
   -> , total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
);
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO SALESSUMMARY
   -> (product_name, total_sales, avg_unit_price, total_units_sold)
   -> 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 row in set (0.00 sec)
mysql> DROP TABLE SALESSUMMARY;
mysql>  SELECT * FROM SALESSUMMARY;
ERROR 1146: Table 'TUTORIALS.SALESSUMMARY' doesn't exist