Latest web development tutorials

MySQL tabelle temporanee

MySQL tabelle temporanee abbiamo bisogno di risparmiare un po 'di dati temporanei è molto utile. La tabella temporanea è visibile nella connessione corrente, quando la connessione è chiusa, Mysql eliminerà automaticamente la tabella e rilasciare tutto lo spazio.

viene aggiunto tabelle temporanee in MySQL versione 3.23, se la versione di MySQL precedenti alla versione 3.23 non è possibile utilizzare le tabelle temporanee MySQL. Ma ora c'è un altro uso raramente una versione così basso del server di database MySQL.

MySQL tabelle temporanee visibile solo nella connessione corrente, se si utilizza lo script PHP MySQL per creare una tabella temporanea, poi quando ha fatto l'esecuzione dello script PHP è completo, la tabella temporanea viene automaticamente distrutta.

Se si utilizza un altro programma client MySQL per la connessione server di database MySQL per creare una tabella temporanea, quindi solo quando si chiude il programma client distruggerà la tabella temporanea, naturalmente, si può anche distrutto manualmente.

Esempi

Quanto segue mostra un semplice esempio dell'uso di tabella temporanea MySQL, il seguente codice SQL può essere applicata alla funzione () mysql_query script PHP.

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 utilizza MOSTRA TABELLE comando per visualizzare un elenco di tabella di dati, non sarà possibile vedere tabella SalesSummary.

Se si esce dalla sessione di MySQL corrente, quindi utilizzare il comando SELECT per leggere i dati originariamente creato tabella temporanea, allora vi accorgerete che il database non esiste nella tabella, perché quando si esce la tabella temporanea è stata distrutta.


Eliminare MySQL tabelle temporanee

Per impostazione predefinita, quando si scollega la connessione al database, la tabella temporanea sarà automaticamente distrutta. Naturalmente, è anche possibile eliminare manualmente la tabella temporanea utilizzando il comando DROP TABLE nella sessione corrente di MySQL.

Quanto segue è quello di eliminare manualmente la tabella Esempio 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 'w3big.SalesSummary' doesn't exist