You can see this post in english too.
Olá!
No último mês precisei copiar a estrutura de uma tabela para uma nova. Como mais de duas pessoas ficaram surpresas com a forma e facilidade com a qual fiz isso, decidi publicá-la aqui ao invés de realizar outra comparação de performance com o PHP.
Segue abaixo o exemplo. Divirta-se e sinta-se à vontade para compartilhar idéias sobre isso.
[arglbr@t64 ~]$ mysql -AD test
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 20
Server version: 5.1.47 Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> CREATE TABLE `test`.`t1` (
-> `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
-> `name` VARCHAR(50) NOT NULL,
-> PRIMARY KEY (`id`),
-> INDEX `idx_01`(`name`)
-> ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.10 sec)
mysql> insert into t1 values (1, 'Adriano'), (2, 'Andréa'), (3, 'Arlete');
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+----+---------+
| id | name |
+----+---------+
| 1 | Adriano |
| 2 | Andréa |
| 3 | Arlete |
+----+---------+
3 rows in set (0.00 sec)
mysql> create table t2 like t1;
Query OK, 0 rows affected (0.10 sec)
mysql> select * from t2;
Empty set (0.00 sec)
mysql> show create table t1;
+-------+---------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------+
| t1 | CREATE TABLE `t1` ( |
| `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, |
| `name` varchar(50) NOT NULL, |
| PRIMARY KEY (`id`), |
| KEY `idx_01` (`name`) |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table t2;
+-------+---------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------+
| t2 | CREATE TABLE `t2` ( |
| `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, |
| `name` varchar(50) NOT NULL, |
| PRIMARY KEY (`id`), |
| KEY `idx_01` (`name`) |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------+
1 row in set (0.00 sec)
mysql>
Como vê, este método é interessante pois traz somente a a estrutura (índices, engine, encoding) e naturalmente, reseta o valor AUTO_INCREMENT.
Até!
Pingback: Adriano Laranjeira » Blog Archive » MySQL: Copy only the structure of table
Muito bom! basta copiar para um simples editor de texto substituir alguma coisa colar novamente para linha de comando ou outro ambiente
e POW! dependendo do tamanho da tabela esse seu método nos fará economizar vários minutos! vários mesmo.
LikeLike
Legal saber disso Aloisio!
Grande abraço!
—
Adriano Laranjeira
http://about.me/arglbr
LikeLike
Deu muito certo, me poupou um bom tempo criando uma nova tabela de 40 colunas.
LikeLike
Oi Anderson!
Fico feliz por ter lhe ajudado!
Abs!
LikeLike