Home  >  Article  >  Database  >  mysql 命令行复制表数据

mysql 命令行复制表数据

WBOY
WBOYOriginal
2016-06-07 15:35:081127browse

一,复制表结构 方法1: 查看复制打印? mysqlcreatetablealikeusers; //复制表结构 QueryOK,0rowsaffected(0.50sec) mysqlshowtables; ---------------- |Tables_in_test| ---------------- |a| |users| ---------------- 2rowsinset(0.00sec) 方法2: 查看复

一,复制表结构

方法1:

查看复制打印?

  1. mysql> create table a like users;         //复制表结构  
  2. Query OK, 0 rows affected (0.50 sec)  
  3.   
  4. mysql> show tables;  
  5. +----------------+  
  6. | Tables_in_test |  
  7. +----------------+  
  8. | a              |  
  9. | users          |  
  10. +----------------+  
  11. 2 rows in set (0.00 sec)  

方法2:

查看复制打印?

  1. mysql> create table b select * from users limit 0;   //复制表结构  
  2. Query OK, 0 rows affected (0.00 sec)  
  3. Records: 0  Duplicates: 0  Warnings: 0  
  4.   
  5. mysql> show tables;  
  6. +----------------+  
  7. | Tables_in_test |  
  8. +----------------+  
  9. | a              |  
  10. | b              |  
  11. | users          |  
  12. +----------------+  
  13. 3 rows in set (0.00 sec)  

方法3:

查看复制打印?

  1. mysql> show create table users\G;          //显示创表的sql  
  2. *************************** 1. row ***************************  
  3.  Table: users  
  4. Create Table: CREATE TABLE `users` (       //改表名  
  5.  `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,  
  6.  `user_name` varchar(60) NOT NULL DEFAULT '',  
  7.  `user_pass` varchar(64) NOT NULL DEFAULT '',  
  8.  PRIMARY KEY (`ID`)  
  9. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8  //改auto_increment  
  10. 1 row in set (0.00 sec)  

把sql语句copy出来,改一下表名和atuo_increment,然后在执行一下。

二,复制表数据,以及表结构

方法1:

查看复制打印?

  1. mysql> create table c select * from users;      //复制表的sql  
  2. Query OK, 4 rows affected (0.00 sec)  
  3. Records: 4  Duplicates: 0  Warnings: 0  

方法2:

  1. mysql> create table d select user_name,user_pass from users where id=1;  
  2. Query OK, 1 row affected (0.00 sec)  
  3. Records: 1  Duplicates: 0  Warnings: 0  

上面的2种方法,方便,快捷,灵活性强。

方法3:

先创建一个空表, INSERT INTO 新表 SELECT * FROM 旧表 ,或者

INSERT INTO 新表(字段1,字段2,…….) SELECT 字段1,字段2,…… FROM 旧表

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn