zoukankan      html  css  js  c++  java
  • mysql 复制表结构、表数据的方法

    From: http://blog.163.com/yaoyingying681@126/blog/static/109463675201191173221759/

    一,复制表结构
    方法1:
    mysql> create table a like users;         //复制表结构  
    Query OK, 0 rows affected (0.50 sec)   
      
    mysql> show tables;   
    +----------------+   
    | Tables_in_test |   
    +----------------+   
    | a              |   
    | users          |   
    +----------------+   
    2 rows in set (0.00 sec)  
    mysql> create table a like users;         //复制表结构
    Query OK, 0 rows affected (0.50 sec)
    mysql> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | a              |
    | users          |
    +----------------+
    2 rows in set (0.00 sec)

    方法2:
    mysql> create table b select * from users limit 0;   //复制表结构  
    Query OK, 0 rows affected (0.00 sec)   
    Records: 0  Duplicates: 0  Warnings: 0   
      
    mysql> show tables;   
    +----------------+   
    | Tables_in_test |   
    +----------------+   
    | a              |   
    | b              |   
    | users          |   
    +----------------+   
    3 rows in set (0.00 sec)  
    mysql> create table b select * from users limit 0;   //复制表结构
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    mysql> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | a              |
    | b              |
    | users          |
    +----------------+
    3 rows in set (0.00 sec)

    方法3:
    mysql> show create table usersG;          //显示创表的sql  这里也可以用 desc users;显示表的结构
    *************************** 1. row ***************************   
    Table: users   
    Create Table: CREATE TABLE `users` (       //改表名  
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,   
    `user_name` varchar(60) NOT NULL DEFAULT '',   
    `user_pass` varchar(64) NOT NULL DEFAULT '',   
    PRIMARY KEY (`ID`)   
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8  //改auto_increment  
    1 row in set (0.00 sec)  
    mysql> show create table usersG;          //显示创表的sql
    *************************** 1. row ***************************
    Table: users
    Create Table: CREATE TABLE `users` (       //改表名
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `user_name` varchar(60) NOT NULL DEFAULT '',
    `user_pass` varchar(64) NOT NULL DEFAULT '',
    PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8  //改auto_increment
    1 row in set (0.00 sec)把sql语句copy出来,改一下表名和atuo_increment,然后在执行一下。
    二,复制表数据,以及表结构
    方法1:
    mysql> create table c select * from users;      //复制表的sql  
    Query OK, 4 rows affected (0.00 sec)   
    Records: 4  Duplicates: 0  Warnings: 0  
    mysql> create table c select * from users;      //复制表的sql
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0

    方法2:
    mysql> create table d select user_name,user_pass from users where id=1;   
    Query OK, 1 row affected (0.00 sec)   
    Records: 1  Duplicates: 0  Warnings: 0  
    mysql> create table d select user_name,user_pass from users where id=1;
    Query OK, 1 row affected (0.00 sec)
    Records: 1  Duplicates: 0  Warnings: 0上面的2种方法,方便,快捷,灵活性强。
    方法3:
    先创建一个空表, INSERT INTO 新表 SELECT * FROM 旧表 ,或者
    INSERT INTO 新表(字段1,字段2,…….) SELECT 字段1,字段2,…… FROM 旧表
    这种方法不是很方便,也是我以前经常用的。

  • 相关阅读:
    ubuntu安装pyton-pip问题解决
    Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused b
    git入门超详细(转载)
    openpose-opencv更改K分匹配算法实现
    年龄_性别识别
    人脸属性识别
    西门子PLC通过MODBUS控制变频器
    S7-200仿真软件使用
    lib/python3.6/site-packages/torchvision/_C.cpython-36m-x86_64-linux-gnu.so: undefined symbol:
    python_opencv修改视频分辨率
  • 原文地址:https://www.cnblogs.com/joeblackzqq/p/4481707.html
Copyright © 2011-2022 走看看