If we have one of the following tables:
ID Username password
-----------------------------------
1 Admin *************
2 Sameer *************
3 Stewart *************
- CREATE TABLE IF not EXISTS ' admin ' (
- ' ID ' int (6) unsigned not NULL auto_increment,
- ' username ' varchar (not NULL default ' '),
- ' Password ' varchar default NULL,
- PRIMARY KEY (' id ')
- ) engine=MyISAM DEFAULT charset=latin1 auto_increment=4;
1. The following statement copies the table structure into the new table newadmin. (Data in the table is not copied)
- CREATE TABLE newadmin like admin
2. The following statement copies the data to the new table. Note: This statement actually only constructs a table for the result of the SELECT statement. So newadmin This table will not have a primary key, index.
- CREATE TABLE Newadmin as
- (
- SELECT *
- From admin
- )
3. If you want to actually copy a table. You can use the following statement.
- CREATE TABLE newadmin like admin;
- INSERT into Newadmin SELECT * from admin;
4. We can operate different databases.
- CREATE TABLE newadmin like shop.admin;
- CREATE TABLE newshop.newadmin like shop.admin;
5. We can also copy some of the fields in a table.
- CREATE TABLE Newadmin as
- (
- SELECT username, password from admin
- )
6. We can also rename the fields of the newly created table.
- CREATE TABLE Newadmin as
- (
- SELECT ID, username as uname, password as pass from admin
- )
7. We can also copy part of the data.
- CREATE TABLE Newadmin as
- (
- SELECT * FROM admin WHERE Left (username,1) = ' s '
- )
8. We can also define the field information in the table while creating the table.
- CREATE TABLE Newadmin
- (
- ID INTEGER not NULL auto_increment PRIMARY KEY
- )
- As
- (
- SELECT * from admin
- )
MySQL copy tables in several ways