TEMPORARY table in Mysql - mysql

If two users create two TEMPORARY tables at the same time in my mysql database using a PHP script, will it create two different tables? Can those users use their own table without facing any trouble and will those table be automatically deleted?
thanks :)
CREATE TEMPORARY TABLE TempTable ( ID int, Name char(100) ) TYPE=HEAP;
INSERT INTO TempTable VALUES( 1, "Foo bar" );
SELECT * FROM TempTable;
DROP TABLE TempTable;

As stated in the manual:
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)

Temporary tables are created in a current session; so, two users can create two temp. tables at the same time in their threads. These tables will be removed on session closing.

You can use [IF NOT EXISTS] in this query...
reference

Related

Alter datatypes in the table -mysql

I need to alter the huge table around 200GB (INNODB).
As i seen many blogs, there is suggestion for creating a new structure, copying the table content to new table and rename the tables.
As given below
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
But if do above steps, How could we handle the newly inserted data into original table. Since many process updating and inserting the records into original table.
I have one master and 8 slaves

phpMyAdmin SQL - Temporary Tables

I need some assistance with creating temporary tables in phpMyAdmin sql from data in a current table. For example, I have a table called Animals with several columns, one of these columns is called Animal_Size, and I am required to create a temporary table called Small Animals consisting of the animals in the Animals table where the size is small.
Can someone guide me as to the best way to go about doing this?
I've seen some examples but a lot of them don't seem to work.
If you need to select all small animals into new table, you want to use this query:
CREATE TABLE small_animals SELECT * FROM animals WHERE animal_size = 'small'
If you need real temporary table, then just add TEMPORARY into the query:
CREATE TEMPORARY TABLE small_animals_temp SELECT * FROM animals WHERE animal_size = 'small'
Update: Because of problem within the parser in phpMyAdmin, add AS before the SELECT, i.e.:
CREATE TABLE small_animals AS SELECT * FROM animals WHERE animal_size = 'small'
Note: A TEMPORARY table is visible only to the current connection, and
is dropped automatically when the connection is closed.

Create a temporary table in a SELECT statement without a separate CREATE TABLE

Is it possible to create a temporary (session only) table from a select statement without using a create table statement and specifying each column type? I know derived tables are capable of this, but those are super-temporary (statement-only) and I want to re-use.
It would save time if I did not have to write up a create table command and keep the column list and type list matched up.
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)
From the manual found at http://dev.mysql.com/doc/refman/5.7/en/create-table.html
You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.
In addition to psparrow's answer if you need to add an index to your temporary table do:
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(col_2) )
ENGINE=MyISAM
AS (
SELECT col_1, coll_2, coll_3
FROM mytable
)
It also works with PRIMARY KEY
Use this syntax:
CREATE TEMPORARY TABLE t1 (select * from t2);
Engine must be before select:
CREATE TEMPORARY TABLE temp1 ENGINE=MEMORY
as (select * from table1)
ENGINE=MEMORY is not supported when table contains BLOB/TEXT columns
As I understand it, a SELECT statement will work on the temporary table if you're using it in something like phpMyAdmin, but following that SELECT, the temporary table will be gone. This means set up exactly what you want to do with it first, and don't view any results till your 'action' statements that change the data (DELETE, UPDATE) are complete.

Mysql query to copy the structure of a table to create another table

Looking for help in creating a Mysql query to copy the structure of an existing table to create another table.
To create a table as in an exact replica of another table:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
If you want to also copy the contents of the table you can do:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;
If you want to copy the table structure including its keys, then you should use:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
To copy the whole table
CREATE TABLE `new_table_name` SELECT * FROM `old_table_name`;
It will create the table and insert all the data from the old table but without bringing the keys from the old table. So you will need to set the keys for the new table.
Its old thread but if someone needed. If you want create query that will create exactly copy of existing table.
show create table <existing table name>
MySQL query to copy the structure of a table to create another table structure without data another way is...
CREATE TABLE `table_name_new` select * from `table_name_old` limit 0;
also you can use this:
CREATE TABLE 'new_table_name' SELECT * FROM 'pattern_table' where 1=0;

MySQL temporary vs memory table in stored procedures

What's is better to use in a stored procedure: a temporary table or a memory table?
The table is used to stored summary data for reports.
Are there any trade offs that developers should be aware off?
CREATE TEMPORARY TABLE t (avg (double));
or
CREATE TABLE t (avg (double)) ENGINE=MEMORY;
Why is this restricted to just the two options? You can do:
CREATE TEMPORARY TABLE t (avg double) ENGINE=MEMORY;
Which works, although I'm not sure how to check if the memory engine is actually being used here.
Of the two, I'd use a temporary table for report.
A memory table holds data across user sessions & connections, so you'd have to truncate it every time to make sure you wouldn't be using data from someone else. Assuming you put in whats necessary to maintain a memory table depending on your needs, it's fine - the temp table is a little safer from a maintenance perspective.
A temporary table will only exist for the duration of your session. A table declared with Engine=Memory will persist across user sessions / connections but will only exist in the lifetime of the MySQL instance. So if MySQL gets restarted the table goes away.
In MySQL, temporary tables are seriously crippled:
http://dev.mysql.com/doc/refman/5.6/en/temporary-table-problems.html
You cannot refer to a TEMPORARY table more than once in the same query.
For example, the following does not work:
mysql> SELECT * FROM temp_table, temp_table AS t2;
ERROR 1137: Can't reopen table: 'temp_table'
I Just wanted to point out that, in 2021 using MariaDB-10.3.27, the code #biziclop said doesn't work, is not the case any more, this is possible:
CREATE TEMPORARY TABLE tmp1 AS
SELECT * FROM products LIMIT 10;
SELECT * FROM tmp1, tmp1 AS t2;
(I just tested it)