当前位置:网站首页>[MySQL usage Script] clone data tables, save query data to data tables, and create temporary tables
[MySQL usage Script] clone data tables, save query data to data tables, and create temporary tables
2022-06-24 07:29:00 【TakingCoding4Granted】
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 DIRECTORYorINDEX DIRECTORYTable 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_tblstay id Field hasPRIMARY KEYconstraint , At the same timestateandcityThere 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_INCREMENTAnd the default values are not copied to the target table , If you really need , You can use it firstCREATE TABLE ... SELECTsentence , And then useALTER TABLEStatements 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
边栏推荐
- [image fusion] image fusion based on directional discrete cosine transform and principal component analysis with matlab code
- 游戏思考14:对cache_server缓冲服务器的问题思考(读云峰博客有感)
- 【WordPress建站】5. 设置代码高亮
- 关于取模数据序号定位的说明 区码定位是指GBK编码
- [机缘参悟-29]:鬼谷子-内揵篇-与上司交往的五种层次
- 两个链表的第一个公共节点_链表中环的入口(剑指offer)
- 【帧率倍频】基于FPGA的视频帧率倍频系统verilog开发实现
- 【Vulhub靶场】】zabbix-SQL注入(CVE-2016-10134)漏洞复现
- Huawei cloud database advanced learning
- Introduction to game design and development - layered quaternion - dynamic layer
猜你喜欢

【帧率倍频】基于FPGA的视频帧率倍频系统verilog开发实现

【WordPress建站】5. 设置代码高亮

get_ started_ 3dsctf_ two thousand and sixteen

Huawei cloud database advanced learning

二分专题训练

20 not to be missed ES6 tips

JVM debugging tool -jmap

How can win11 set the CPU performance to be fully turned on? How does win11cpu set high performance mode?
![[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code](/img/b3/26cfa385aa357c3a7a77e9db47e94c.png)
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code

華為雲數據庫進階學習
随机推荐
The initial user names and passwords of Huawei devices are a large collection that engineers involved in Huawei business should keep in mind and collect!
[WUSTCTF2020]爬
Canal installation configuration
An example of MySQL accidental deletion recovery - using Myflash
Graduation season advance technology
现货黄金有哪些眩人的小技术?
Huawei experimental topology set, learning methods are attached at the end of the article!
PCL point cloud random sampling by ratio
Serviceworker working mechanism and life cycle: resource caching and collaborative communication processing
Introduction to game design and development - layered quaternion - dynamic layer
MySQL enable binlog
How can win11 set the CPU performance to be fully turned on? How does win11cpu set high performance mode?
什么是CC攻击?如何判断网站是否被CC攻击? CC攻击怎么防御?
2、 What is the principle of layer 3 and 4 switching technology? Recommended collection!
Accelerate large-scale data analysis based on Apache iceberg through data organization optimization
A case study of apiserver avalanche caused by serviceaccount
MFC多线程 信号量CSemaphore 临界区与互斥 事件
華為雲數據庫進階學習
伦敦金的资金管理比其他都重要
Big factories are not the only way to measure ability. The three years' experience of Shangcai's graduation