zoukankan      html  css  js  c++  java
  • MySQL复制表-CREATE SELECT

    假设存在以下Table:

    mysql> select * from staff;
    +----+----------+-------+
    | id | name     | slary |
    +----+----------+-------+
    |  3 | haofugui | 10000 |
    |  4 | guoming  |  3500 |
    |  5 | haotian  |  2900 |
    +----+----------+-------+
    3 rows in set (0.00 sec)
    
    mysql> describe staff;
    +-------+----------+------+-----+---------+----------------+
    | Field | Type     | Null | Key | Default | Extra          |
    +-------+----------+------+-----+---------+----------------+
    | id    | int(11)  | NO   | PRI | NULL    | auto_increment |
    | name  | char(20) | YES  |     | NULL    |                |
    | slary | int(11)  | YES  |     | NULL    |                |
    +-------+----------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)

    1. 只复制表结构到新表

    语句1:CREATE TABLE new_table_name SELECT [field1,field2... | *] FROM old_table_name WHERE 1=2;

    语句2:CREATE TABLE new_table _name LIKE old_table_name;

    示例:

    mysql> create table staff_bak select id,name from staff where 1=2; //根据旧表的指定属性创建一个新的空表
    Query OK, 0 rows affected (0.03 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> select * from staff_bak;   //新建的数据库为空表
    Empty set (0.00 sec)
    
    mysql> describe staff_bak;       //原表的主键和自动增长不能被复制
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | id    | int(11)  | NO   |     | 0       |       |
    | name  | char(20) | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    mysql> create table staff_bak_1 like staff;  //根据旧表创建一个新的空表,无法指定属性或属性组
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> select * from staff_bak_1;
    Empty set (0.00 sec)
    
    mysql> describe staff_bak_1;    //所有数据类型和完整性约束条件都能被复制,包括主键和自动增长
    +-------+----------+------+-----+---------+----------------+
    | Field | Type     | Null | Key | Default | Extra          |
    +-------+----------+------+-----+---------+----------------+
    | id    | int(11)  | NO   | PRI | NULL    | auto_increment |
    | name  | char(20) | YES  |     | NULL    |                |
    | slary | int(11)  | YES  |     | NULL    |                |
    +-------+----------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)

    注意:语句1可指定复制的属性范围,但无法复制主键类型和自增方式;

              语句2会把旧表的所有字段类型都复制到新表,但无法复制指定属性或属性组。

    2. 复制表结构及数据到新表

    语句:CREATE TABLE new_table_name SELECT [field1,field2... | *] FROM old_table_name;

    mysql> create table staff_bak select id,name from staff;  //根据旧表将指定属性及其数据创建新表
    Query OK, 3 rows affected (0.03 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> describe staff_bak;    //新表结构展示
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | id    | int(11)  | NO   |     | 0       |       |
    | name  | char(20) | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    
    mysql> select * from staff_bak;  //新表数据显示
    +----+----------+
    | id | name     |
    +----+----------+
    |  3 | haofugui |
    |  4 | guoming  |
    |  5 | haotian  |
    +----+----------+
    3 rows in set (0.00 sec)
  • 相关阅读:
    c++ 存储连续性,作用域和链接性注意点
    函数模板的知识点总结
    c++ 左值引用的注意点
    VS2015如何在同一个解决方案下建立多个项目及多个项目之间的引用
    编译opencv4.1.0+tesseract5.0 的Realease x64版本遇见的问题解决
    逻辑化简-卡诺图
    从Word Embedding到Bert模型—自然语言处理中的预训练技术发展史 (转载)
    matlab绘图
    多个EXCEL文件合并成一个
    数学建模及机器学习算法(一):聚类-kmeans(Python及MATLAB实现,包括k值选取与聚类效果评估)
  • 原文地址:https://www.cnblogs.com/yy20141204bb/p/8409820.html
Copyright © 2011-2022 走看看