MySQL statement for copying table structure and table data: completes table structure and table data copying at the same time (Create Table and copy data)
MySQL statement for copying table structure and table data: completes table structure and table data copying at the same time (Create Table and copy data)
1. MySQL statement for copying table structures and table data: completes simultaneous replication of table structures and table data (creating tables and copying data)
Mysql> create table tmp_table SELECT * FROM dede_news;
Query OK, 628 rows affected (2.04 sec)
Records: 628 Duplicates: 0 Warnings: 0
Note: The worst thing about this method is that the new table does not have the primary key, Extra (auto_increment) and other attributes of the old table. You need to manually add them.
2. MySQL statements used to copy table structures but no longer tabulation data: (create a table and copy data, and use conditional statements to control data to null)
Mysql> create table tmp_table SELECT * FROM dede_news WHERE 1 = 2;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
Note: Given the where Condition 1 = 2, the final selected data is null, but only the table structure is copied.
3. You can also copy only the table structure using the following method:
Mysql> create table tmp_table like dede_news;
Query OK, 0 rows affected (0.06 sec)
4. Use insert into Table 1 SELECT field FROM table 2 to INSERT the specified or all data in Table 2 to Table 1 (provided that the data types are consistent)
Mysql> insert into tmp_news SELECT * FROM dede_news;
Query OK, 628 rows affected (0.57 sec)
Records: 628 Duplicates: 0 Warnings: 0
Others:
1) Delete tables at the same time:
Drop table tmp_news, tmp_table;
2) time functions date_sub () and date_add ()
Mysql> SELECT now () AS now, date_add (now (), interval 5 day) AS add5d, date_add (now (), interval 5 month) AS add5m, date_add (now (), interval 5 year) AS add5y, date_sub (now (), interval 5 day) AS sub5d, date_sub (now (), interval 5 month) AS sub5m, date_sub (now (), interval 5 year) AS sub5y \ G
* *************************** 1. row ***************************
Now: 2014-03-19 21:35:24
Add5d: 2014-03-24 21:35:24
Add5m: 21:35:24
Add5y: 2019-03-19 21:35:24
Sub5d: 2014-03-14 21:35:24
Sub5m 5 m: 21:35:24
Sub5y: 21:35:24
1 row in set (0.00 sec)
UPDATE dede_news SET createtime = date_sub (createtime, interval 1 year) WHERE createtime> now ();
Note: Comparison between the INSERT xxx INTO xxx SELECT xxx FROM xxx and SELECT xxx INTO xxx FROM xxx statements
In MySQL, the INSERT xxx INTO xxx SELECT xxx FROM xxx statement can INSERT query results INTO database tables.
The SELECT xxx INTO xxx FROM xxx statement is used to assign the query result to the variable in the storage function.