[MySQL usage Script] clone data tables, save query data to data tables, and create temporary tables

TakingCoding4Granted 2022-06-24 07:29:10 阅读数:349

mysqlusagescriptclonedata

1. introduction

Subsequent introductions to this article will be based on the name mail Data sheet for , The statements for creating a table and inserting data into the table are as follows :

DROP TABLE IF EXISTS mail;
#@ _CREATE_TABLE_
CREATE TABLE mail
(
t DATETIME, # when message was sent
srcuser VARCHAR(8), # sender (source user and host)
srchost VARCHAR(20),
dstuser VARCHAR(8), # recipient (destination user and host)
dsthost VARCHAR(20),
size BIGINT, # message size in bytes
INDEX (t)
);
#@ _CREATE_TABLE_
INSERT INTO mail (t,srchost,srcuser,dsthost,dstuser,size)
VALUES
('2014-05-11 10:15:08','saturn','barb','mars','tricia',58274),
('2014-05-12 12:48:13','mars','tricia','venus','gene',194925),
('2014-05-12 15:02:49','mars','phil','saturn','phil',1048),
('2014-05-12 18:59:18','saturn','barb','venus','tricia',271),
('2014-05-14 09:31:37','venus','gene','mars','barb',2291),
('2014-05-14 11:52:17','mars','phil','saturn','tricia',5781),
('2014-05-14 14:42:21','venus','barb','venus','barb',98151),
('2014-05-14 17:03:01','saturn','tricia','venus','phil',2394482),
('2014-05-15 07:17:48','mars','gene','saturn','gene',3824),
('2014-05-15 08:50:57','venus','phil','venus','phil',978),
('2014-05-15 10:25:52','mars','gene','saturn','tricia',998532),
('2014-05-15 17:35:31','saturn','gene','mars','gene',3856),
('2014-05-16 09:00:28','venus','gene','mars','barb',613),
('2014-05-16 23:04:19','venus','phil','venus','barb',10294),
('2014-05-19 12:49:23','mars','phil','saturn','tricia',873),
('2014-05-19 22:21:51','saturn','gene','venus','gene',23992)
;

2. Clone a table

problem

You want to create a data table , And ensure that the table and an existing data table have the same table structure .

Solution

Using statements CREATE TABLE ... LIKE To clone the table structure of a data table . If you want to copy all or part of the records in the original table to the cloned table , have access to INSERT INTO ... SELECT Such a statement .

Discuss

To create a table with the same table structure as an existing data table , You can use the following statement :

CREATE TABLE new_table LIKE original_table;

The table structure of the new table cloned with the above statement is the same as that of the original table , Part of the difference is :

  • The above statement does not clone the definition of the foreign key in the original table ;
  • The above statement does not copy any DATA DIRECTORY or INDEX DIRECTORY Table options ( If the original watch is used ).

The newly cloned table is empty , If you also want the data in the new table to be the same as that in the original table , You can use the following statement :

INSERT INTO new_table SELECT * FROM original_table;

If you only need to copy some data from the original table to the new table , You can add... When copying WHERE Limiting conditions , for example :

CREATE TABLE mail2 LIKE mail;
INSERT INTO mail2 SELECT * FROM mail WHERE srcuser = 'barb';

3. Save query data to data table

problem

You hope to pass SELECT Statement to save the results to a data table , Not on the terminal .

Solution

If the data table exists , Then you can use INSERT INTO ... SELECT Implementation requirements . If the data table does not exist , Then you can use CREATE TABLE ... SELECT To meet the needs .

Discuss

Usually ,MySQL The server will SELECT The result of the statement is returned to the client executing the statement . take SELECT The query result of the statement is saved in a data table , There may be the following benefits :

  • You can simply copy all or part of the data of a data table . for example : Suppose you are developing an application that involves modifying data tables , Then you can make a copy of the data table in the way given above , Then operate on the copied table , This will not affect the original table data due to wrong operations . If the original table has a large amount of data , Then you can copy only a part of the data table in the above way , Because it takes less time to query some data ;
  • Some applications may maintain both a large warehouse table and a smaller production table , The application mainly inserts new data into the latter , The latter will regularly import the data into the former for archiving , Then clean up the former , So as to avoid the performance degradation caused by inserting data into the growing data table ;
  • Statistical operations on large data tables are generally time-consuming , To reduce the need to repeat such operations , You can save the execution results in another table , For future use .

Two tables will be used in the following example , among src_tbl Represents the original table , Data is passed through SELECT Statement to get ,dst_tbl Represents the target table , The data obtained from the previous query will be directly inserted into it .

If the target table exists , So use INSERT ... SELECT Statement can copy the query results directly into it . for example , If dst_tbl Contains an integer field i And a string type field s , Then the function realized by the following statement is to src_tbl Of val Fields and name Fields are copied to dst_tbl Of i and s Field :

INSERT INTO dst_tbl (i, s) SELECT val, name FROM src_tbl;

It should be noted that , from src_tbl The number of fields found in the table must be the same as dst_tbl The number of fields inserted in the table should be consistent , And the field relationship in the two tables only corresponds based on the position in the statement , It has nothing to do with the field name . If both have the same number of fields , And the field type in the same position is the same , You can write this when copying all fields of all records :

INSERT INTO dst_tbl SELECT * FROM src_tbl;

If you only want to copy some records , Can be in SELECT Add after statement WHERE Limiting conditions :

INSERT INTO dst_tbl SELECT * FROM src_tbl
WHERE val > 100 AND name LIKE 'A%';

actually , SELECT Statements can also be more complex . for example , The following statement first counts src_tbl In the table name Occurrence frequency of each value of the field , Then save the frequency and field name in dst_tbl in :

INSERT INTO dst_tbl (i, s) SELECT COUNT(*), name
FROM src_tbl GROUP BY name;

If the target table does not exist , Of course you can use it first CREATE TABLE Statement to create the table first , And then use INSERT ... SELECT sentence ; Another more concise way is to use CREATE TABLE ... SELECT Put the two steps together . for example :

CREATE TABLE dst_tbl SELECT * FROM src_tbl;

In the above statement ,MySQL Will be based on src_tbl Field name of 、 Quantity and type creation dst_tbl surface , Then query the data in the former and insert the latter . If you only want to copy part of the data in the former , You can add the appropriate WHERE Conditions . If you just want to create an empty table , You can use the following statements of type :

CREATE TABLE dst_tbl SELECT * FROM src_tbl WHERE FALSE;

If you just want to copy src_tbl Some fields in the table , Can be in SELECT Statement . for example , If src_tbl Containing fields a ,b ,c and d , If you just want to create dst_tbl Copy fields at the same time b and d , Then you can use the following statement :

CREATE TABLE dst_tbl SELECT b, d FROM src_tbl;

If, in addition to hoping dst_tbl Create from src_tbl in SELECT Out of the field , You also want to add other fields , So you need to CREATE TABLE Add the corresponding statement to the part of the statement . for example , The following statement , In addition to the dst_tbl Table created src_tbl In the field a , b and c outside , There is also a new one with AUTO_INCREMENT Attribute id Field :

CREATE TABLE dst_tbl
(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
)
SELECT a, b, c FROM src_tbl;

If new dst_tbl The values in the table are from src_tbl Expressions in the table , So in the new dst_tbl In the table , The corresponding field name is the name of the expression , This makes the field names less intuitive . Regarding this , You can improve by aliasing fields . for example ,src_tbl The receipt information is included in the , The receipt information contains the quantity and unit price of the goods , that , The following statement implements such a function , First count the total price of items in each receipt , Then insert the receipt number and total price into the new dst_tbl In the table :

CREATE TABLE dst_tbl
SELECT inv_no, SUM(unit_cost*quantity) AS total_cost
FROM src_tbl GROUP BY inv_no;

As mentioned above ,CREATE TABLE ... SELECT The function of the statement is very convenient , However, the information that can be specified when creating a new table is not directly used CREATE TABLE More comprehensive . for example :MySQL I don't know if I need to add an index to a field when creating a new table , Or whether you need to specify a default value . Regarding this , Can be improved in the following ways :

  • You can clone a table as described above , To create a table exactly like the original table ;

  • Explicitly specify the required constraints while creating the target table . for example , If src_tbl stay id Field has PRIMARY KEY constraint , At the same time state and city There is a union index on the field , Then you can use the following statement :

    CREATE TABLE dst_tbl (PRIMARY KEY (id), INDEX(state,city))
    SELECT * FROM src_tbl;
    
  • The properties of the field, for example AUTO_INCREMENT And the default values are not copied to the target table , If you really need , You can use it first CREATE TABLE ... SELECT sentence , And then use ALTER TABLE Statements are modified accordingly . for example :

    CREATE TABLE dst_tbl (PRIMARY KEY (id)) SELECT * FROM src_tbl;
    ALTER TABLE dst_tbl MODIFY id INT UNSIGNED NOT NULL AUTO_INCREMENT;
    

4. Create a temporary table

problem

You need a data table that only exists for a short time , The table needs to disappear automatically after it is used .

Solution

Use TEMPORARY Keyword to create a temporary table , thereafter MySQL It will manage its life cycle .

Discuss

Some operations may only require the data table to exist temporarily , And it is required to automatically disappear when the table is no longer used . Of course , After you really don't need a form , You can use DROP TABLE Statement to explicitly delete a table .

Another more elegant and secure approach is to use CREATE TEMPORARY TABLE sentence . The statement and CREATE TABLE The function is similar to , The former creates a temporary table , After the current client and server end the session , If you don't explicitly delete this table , that MySQL Will automatically delete this temporary table for you .

  • Create temporary tables by explicitly defining table fields :
CREATE TEMPORARY TABLE tbl_name (...column definitions...);
  • Create a temporary table based on an existing table :
CREATE TEMPORARY TABLE new_table LIKE original_table;
  • Create a temporary table based on the existing table and insert the data in the existing table :
CREATE TEMPORARY TABLE tbl_name SELECT ... ;

It should be noted that , Because the temporary table is only related to a client session , So multiple clients can create temporary tables with the same name , At the same time, these temporary tables will not affect each other . This is convenient for applications that use temporary tables , Because you don't need to make sure that the temporary table has a unique name .

A temporary table can have the same name as a permanent table , In this case , During the duration of the interim table , The permanent table with the same name is “ Invisible ”, The advantage of this is , You can copy out a temporary table , Then modify it without affecting the original table . for example , Below DELETE The statement will only start from a statement named mail Delete data from the temporary table of , Without affecting the data in the original table :

mysql> CREATE TEMPORARY TABLE mail SELECT * FROM mail;
Query OK, 16 rows affected (0.01 sec)
Records: 16 Duplicates: 0 Warnings: 0
mysql> SELECT COUNT(*) FROM mail;
+----------+
| COUNT(*) |
+----------+
| 16 |
+----------+
1 row in set (0.01 sec)
mysql> DELETE FROM mail;
Query OK, 16 rows affected (0.01 sec)
mysql> SELECT COUNT(*) FROM mail;
+----------+
| COUNT(*) |
+----------+
| 0 |
+----------+
1 row in set (0.03 sec)
mysql> DROP TEMPORARY TABLE mail;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT COUNT(*) FROM mail;
+----------+
| COUNT(*) |
+----------+
| 16 |
+----------+
1 row in set (0.00 sec)

It should be noted that , Although the use of CREATE TEMPORARY TABLE The temporary table created by the statement has many advantages , But there are a few things to keep in mind :

  • In the same conversation , Creating a second temporary table with the same name will result in an error , Therefore, the correct way is to explicitly delete the first temporary table first , Then create a second temporary table ;

  • If your application expects to modify a temporary table instead of a hidden permanent original table with the same name , So if your program interface has the ability to re-establish the connection , Then make sure your program will detect when the connection is disconnected due to unexpected circumstances . If it is detected that the connection is disconnected due to abnormal conditions, it will be automatically reconnected , So after reconnection , Changes to the data table with the same name will be made to the permanent original table , Not a temporary table , Because the temporary table has been automatically deleted because the session is disconnected ;

  • Somewhat API Support persistent connections or connection pools . under these circumstances , When your script is finished , Temporary tables may not be automatically deleted as you would like , Because the connection will remain established because it is used by other scripts , under these circumstances , It is recommended to execute the following statements before creating a temporary table :

    DROP TEMPORARY TABLE IF EXISTS tbl_name
    
copyright:author[TakingCoding4Granted],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240152297161.html