Several ways to copy a table in mysql

Source: Internet
Author: User

Mysql copy table operations are often used. The following describes how to copy tables from mysql in detail. It is helpful for you to learn how to copy tables from mysql.

Suppose we have the following table:

Id username password
-----------------------------------
1 admin *************
2 sameer *************
3. stewart *************

 
 
  1. CREATE TABLE IF NOT EXISTS `admin` (   
  2. `id` int(6) unsigned NOT NULL auto_increment,   
  3. `username` varchar(50) NOT NULL default '',   
  4. `password` varchar(100) default NULL,   
  5. PRIMARY KEY (`id`)   
  6. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;  

1. The following statement copies the table structure to newadmin. Will not copy the data in the table)

 
 
  1. CREATE TABLE newadmin LIKE admin  
  2.  

2. The following statement copies the data to the new table. Note: This statement only creates a table for the results of the select statement. Therefore, the newadmin table does not have a primary key and an index.

 
 
  1. CREATE TABLE newadmin AS   
  2. (   
  3. SELECT *   
  4. FROM admin   
  5. )  
  6.  

3. If you want to copy a table. You can use the following statement.

 
 
  1. CREATE TABLE newadmin LIKE admin;   
  2. INSERT INTO newadmin SELECT * FROM admin;  
  3.  

4. We can operate different databases.

 
 
  1. CREATE TABLE newadmin LIKE shop.admin;   
  2. CREATE TABLE newshop.newadmin LIKE shop.admin;  
  3.  

5. We can also copy some fields in a table.

 
 
  1. CREATE TABLE newadmin AS   
  2. (   
  3. SELECT username, password FROM admin   
  4. )  
  5.  

6. We can also change the name of the field in the new table.

 
 
  1. CREATE TABLE newadmin AS   
  2. (   
  3. SELECT id, username AS uname, password AS pass FROM admin   
  4. )  
  5.  

7. We can also copy part of the data.

 
 
  1. CREATE TABLE newadmin AS   
  2. (   
  3. SELECT * FROM admin WHERE LEFT(username,1) = 's'   
  4. )  
  5.  

8. You can also define field information in the table while creating the table.

 
 
  1. CREATE TABLE newadmin   
  2. (   
  3. id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY   
  4. )   
  5. AS   
  6. (   
  7. SELECT * FROM admin   
  8. )  

In-depth study on MySQL's data deletion from multiple tables

Simple MySQL large table backup method

Analysis of MySQL Chinese Table creation Problems

MySQL authorization table usage example

Disadvantages of MySQL memory tables

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.