SQL statements and time functions for copying table structures and table data in MySQL

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.