English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tablas de copia MySQL

If we need to completely copy the MySQL data table, including the structure, index, default values, etc. If you only useCREATE TABLE ... SELECT command cannot be implemented.

This chapter will introduce how to completely copy the MySQL data table, the steps are as follows:

  • Use SHOW CREATE TABLE command to get the creation of the data table (CREATE TABLE) statement, which includes the structure and indexes of the original data table.

  • Copy the SQL statement displayed below, modify the table name, and execute the SQL statement. Through the above command To completely copy the data table structure.

  • If you want to copy the content of the table, you can use INSERT INTO ... SELECT realizar esta declaración.

Online example

Try the following example to copy table w3codebox_tbl .

Step 1:

Get the complete structure of the data table.

mysql> SHOW CREATE TABLE w3codebox_tbl \G;
*************************** 1. row ***************************
       Table: w3codebox_tbl
Create Table: CREATE TABLE `w3codebox_tbl` (
  `w3codebox_id` int(11) NOT NULL auto_increment,
  `w3codebox_title` varchar(100) NOT NULL default '',
  `w3codebox_author` varchar(40) NOT NULL default '',
  `submission_date` date default NULL,
  PRIMARY KEY  (`w3codebox_id`),
  UNIQUE KEY `AUTHOR_INDEX` (`w3codebox_author`)
) ENGINE=InnoDB 
1 row in set (0.00 sec)
ERROR:
No query specified

Step 2:

Modify the table name in the SQL statement and execute the SQL statement.

mysql> CREATE TABLE `clone_tbl` (
  -> `w3codebox_id` int(11) NOT NULL auto_increment,
  -> `w3codebox_title` varchar(100) NOT NULL default '',
  -> `w3codebox_author` varchar(40) NOT NULL default '',
  -> `submission_date` date default NULL,
  -> PRIMARY KEY  (`w3codebox_id`),
  -> UNIQUE KEY `AUTHOR_INDEX` (`w3codebox_author`)
-> ) ENGINE=InnoDB;
Consulta OK, 0 filas afectadas (1.80 sec)

Paso tres:

Después de completar el segundo paso, creará una nueva tabla clonada clone_tbl en la base de datos. Si desea copiar los datos de la tabla, puede usar INSERT INTO... SELECT realizar esta declaración.

mysql> INSERT INTO clone_tbl (w3> codebox_id,
    -> w3> codebox_title,
    -> w3> codebox_author,
    -> submission_date)
    -> SELECT w3> codebox_id,w3> codebox_title,
    -> w3> codebox_author,submission_date
    -> FROM w3codebox_tbl;
Consulta OK, 3 filas afectadas (0.07 sec)
Registros: 3  Duplicados: 0    Advertencias: 0

Después de ejecutar los pasos anteriores, se copiará completamente el contenido de la tabla, incluyendo la estructura de la tabla y los datos de la tabla.