• MySQL->复制表[20180509]


    MySQL复制表
        通常复制表所采用CREATE TABLE .... SELECT 方式将资料复制,但无法将旧表中的索引,约束(除非空以外的)也复制。
     
    完整复制MySQL数据表所需步骤:
    方式一
        1.使用SHOW CREATE TABLE 命令获取创建数据表的create table语句,语句会包含原表的结构,索引,存储引擎,字符集。
        2.更改其中的表名称,再执行create table语句。
        3.复制表的资料,使用INSERT INTO ... SELECT 语句。
    方式二
        CREATE TABLE <table_name> LIKE <old_table>;
         INSERT INTO <table_name> SELECT * FROM <old_table>;
     
     
    获取创建表的SQL语句:
    mysql> show create table index_tab01G
    *************************** 1. row ***************************
           Table: index_tab01
    Create Table: CREATE TABLE `index_tab01` (
      `id` int(11) NOT NULL DEFAULT '0',
      `col01` varchar(10) NOT NULL DEFAULT '',
      `col02` text,
      UNIQUE KEY `index_un` (`col01`),
      KEY `indx_01` (`col01`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)
     
    更改表名称,后执行创建语句:
    mysql> CREATE TABLE `copy_tab01` (
        ->   `id` int(11) NOT NULL DEFAULT '0',
        ->   `col01` varchar(10) NOT NULL DEFAULT '',
        ->   `col02` text,
        ->   UNIQUE KEY `index_un` (`col01`),
        ->   KEY `indx_01` (`col01`)
        -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8
        -> ;
    Query OK, 0 rows affected (0.01 sec)
     
    复制表的资料
    mysql> insert into copy_tab01 select * from index_tab01;
    Query OK, 4 rows affected (0.01 sec)
    Records: 4  Duplicates: 0  Warnings: 0
     
     
    mysql> select * from copy_tab01;
    +----+--------+---------------+
    | id | col01  | col02         |
    +----+--------+---------------+
    |  0 | Name01 | This is Test! |
    |  0 | Name02 | This is Test! |
    |  0 | Name03 | This is Test! |
    |  0 | Name04 | This is Test! |
    +----+--------+---------------+
    4 rows in set (0.00 sec)
     
    mysql> select * from index_tab01;
    +----+--------+---------------+
    | id | col01  | col02         |
    +----+--------+---------------+
    |  0 | Name01 | This is Test! |
    |  0 | Name02 | This is Test! |
    |  0 | Name03 | This is Test! |
    |  0 | Name04 | This is Test! |
    +----+--------+---------------+
    4 rows in set (0.00 sec)
     
    方式二,复制完整的表
    mysql> create table copy_tab02 like index_tab01;
    Query OK, 0 rows affected (0.00 sec)
     
    mysql> show create table copy_tab02G
    *************************** 1. row ***************************
           Table: copy_tab02
    Create Table: CREATE TABLE `copy_tab02` (
      `id` int(11) NOT NULL DEFAULT '0',
      `col01` varchar(10) NOT NULL DEFAULT '',
      `col02` text,
      UNIQUE KEY `index_un` (`col01`),
      KEY `indx_01` (`col01`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)
     
    mysql> insert into copy_tab02 select * from index_tab01;
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
     
    mysql> select * from copy_tab02;
    +----+--------+---------------+
    | id | col01  | col02         |
    +----+--------+---------------+
    |  0 | Name01 | This is Test! |
    |  0 | Name02 | This is Test! |
    |  0 | Name03 | This is Test! |
    |  0 | Name04 | This is Test! |
    +----+--------+---------------+
    4 rows in set (0.00 sec)
     
     
        
     
  • 相关阅读:
    黑产上演《三体》剧情:蠕虫病毒入侵手机群发“钓鱼”短信
    安天AVL联合小米MIUI首擒顽固病毒“不死鸟”
    阿里云存储OSS服务端签名客户端直传
    mariadb 安装配置
    NPOI 打印设置
    windows2008 r2 网络负载均衡搭建
    SqlServer数据库技巧
    达梦数据库备份还原
    ASP.NET CORE Docker发布记录
    DataReader转换
  • 原文地址:https://www.cnblogs.com/also-brook/p/9014706.html
Copyright © 2020-2023  润新知