Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a temporary table in MySQL with an index from a select

I have a stored function where I use temporary tables. For performance reasons, I need an index in that table. Unfortunately, I cannot use ALTER TABLE because this causes an implicit commit.

Therefore I'm looking for the syntax to add the INDEX for tempid during creation. Can anyone be of help?

CREATE TEMPORARY TABLE tmpLivecheck  (     tmpid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY ) SELECT * FROM   tblLivecheck_copy WHERE  tblLivecheck_copy.devId = did; 
like image 347
solick Avatar asked Jan 18 '13 11:01

solick


People also ask

Can we create index on temp table in MySQL?

Create temporary table and insert data. Create temporary table in a select statement without a separate create table. Create a temporary table in MySQL with an index. Add a column to temporary table in MySQL.

Can we use index in temporary table?

SQL temp tables support adding clustered and non-clustered indexes after the SQL Server temp table creation and implicitly by defining Primary key constraint or Unique Key constraint during the tables creation, but table variables support only adding such indexes implicitly by defining Primary key constraint or Unique ...

How do you create a temporary table with different specifications?

To create a temporary table, you must have the CREATE TEMPORARY TABLES privilege. After a session has created a temporary table, the server performs no further privilege checks on the table. The creating session can perform any operation on the table, such as DROP TABLE , INSERT , UPDATE , or SELECT .

How do I SELECT a temporary table in MySQL?

The general syntax would be like this: INSERT INTO temporary_tabel_name SELECT * FROM existing table_name; Following the general syntax, we will copy the data from the existing table, named, Guys into the newly created temporary table, named, “temporary_data”.


Video Answer


1 Answers

I wrestled quite a while with the proper syntax for CREATE TEMPORARY TABLE SELECT. Having figured out a few things, I wanted to share the answers with the rest of the community.

Basic information about the statement is available at the following MySQL links:

CREATE TABLE SELECT and CREATE TABLE.

At times it can be daunting to interpret the spec. Since most people learn best from examples, I will share how I have created a working statement, and how you can modify it to work for you.

  1. Add multiple indexes

    This statement shows how to add multiple indexes (note that index names - in lower case - are optional):

    CREATE TEMPORARY TABLE core.my_tmp_table  (INDEX my_index_name (tag, time), UNIQUE my_unique_index_name (order_number)) SELECT * FROM core.my_big_table WHERE my_val = 1 
  2. Add a new primary key:

    CREATE TEMPORARY TABLE core.my_tmp_table  (PRIMARY KEY my_pkey (order_number), INDEX cmpd_key (user_id, time)) SELECT * FROM core.my_big_table 
  3. Create additional columns

    You can create a new table with more columns than are specified in the SELECT statement. Specify the additional column in the table definition. Columns specified in the table definition and not found in select will be first columns in the new table, followed by the columns inserted by the SELECT statement.

    CREATE TEMPORARY TABLE core.my_tmp_table  (my_new_id BIGINT NOT NULL AUTO_INCREMENT,   PRIMARY KEY my_pkey (my_new_id), INDEX my_unique_index_name (invoice_number)) SELECT * FROM core.my_big_table 
  4. Redefining data types for the columns from SELECT

    You can redefine the data type of a column being SELECTed. In the example below, column tag is a MEDIUMINT in core.my_big_table and I am redefining it to a BIGINT in core.my_tmp_table.

    CREATE TEMPORARY TABLE core.my_tmp_table  (tag BIGINT, my_time DATETIME,   INDEX my_unique_index_name (tag) ) SELECT * FROM core.my_big_table 
  5. Advanced field definitions during create

    All the usual column definitions are available as when you create a normal table. Example:

    CREATE TEMPORARY TABLE core.my_tmp_table  (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, value BIGINT UNSIGNED NOT NULL DEFAULT 0 UNIQUE, location VARCHAR(20) DEFAULT "NEEDS TO BE SET", country CHAR(2) DEFAULT "XX" COMMENT "Two-letter country code",   INDEX my_index_name (location)) ENGINE=MyISAM  SELECT * FROM core.my_big_table 
like image 125
IvanD Avatar answered Sep 21 '22 12:09

IvanD