zoukankan      html  css  js  c++  java
  • create table like 和create table select 比较

    语法:
    CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
        [(create_definition,...)]
        [table_options] [select_statement]
     
    CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
        [(] LIKE old_tbl_name [)];
    测试过程:
    原数据表:
    mysql> show create table test_order G
    *************************** 1. row ***************************
           Table: test_order
    Create Table: CREATE TABLE `test_order` (
      `pay_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `origin` int(10) DEFAULT NULL,
      `team_id` int(11) DEFAULT NULL,
      `state` int(11) DEFAULT NULL,
      KEY `team_id` (`team_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)
    mysql> create table cc select * from test_order;
    Query OK, 9900 rows affected (0.11 sec)
    Records: 9900  Duplicates: 0  Warnings: 0
    mysql> create table dd like test_order;                
    Query OK, 0 rows affected (0.22 sec)
    查看数据:
    mysql> select * from cc limit 2;
    +---------------------+--------+---------+-------+
    | pay_time            | origin | team_id | state |
    +---------------------+--------+---------+-------+
    | 2011-06-22 18:04:47 |     10 |     100 |   100 |
    | 2011-06-22 18:04:47 |     10 |     100 |   101 |
    +---------------------+--------+---------+-------+
    2 rows in set (0.00 sec)
    mysql> select * from dd;
    Empty set (0.00 sec)
    结果:cc表中数据与原表test_order中的一致,dd表中无数据
    查看表结构:
    mysql> show create table cc G
    *************************** 1. row ***************************
           Table: cc
    Create Table: CREATE TABLE `cc` (
      `pay_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
      `origin` int(10) DEFAULT NULL,
      `team_id` int(11) DEFAULT NULL,
      `state` int(11) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)
    mysql> show create table dd G
    *************************** 1. row ***************************
           Table: dd
    Create Table: CREATE TABLE `dd` (
      `pay_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `origin` int(10) DEFAULT NULL,
      `team_id` int(11) DEFAULT NULL,
      `state` int(11) DEFAULT NULL,
      KEY `team_id` (`team_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)
    结果:cc表中,原表中的索引消失了;dd表与原表一致
     
    结论:
         create table select 会将原表中的数据完整复制一份,但表结构中的索引会丢失。
         create table like 只会完整复制原表的建表语句,但不会复制数据
  • 相关阅读:
    1030 完美数列 (25 分)
    1029 旧键盘 (20 分)
    数据库命令失败原因汇总
    代码有中文括号,导致错误
    win10笔记本触控板使用指南
    (已解决)vsC#控制台应用添加System.Windows.Forms引用失败(精通C#)
    ildasm中Ctrl+M闪退的问题(已解决, 精通C# 15.1)
    C#控制台应用(.NET Core)添加System.Windows.Forms失败(已解决)
    知识点_指针_增加对指针的理解
    自己写出的Bug_应是%f却写成%d
  • 原文地址:https://www.cnblogs.com/Alight/p/4469671.html
Copyright © 2011-2022 走看看