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 只会完整复制原表的建表语句,但不会复制数据
时间: 2024-11-05 12:13:05

create table like 和create table select 比较的相关文章

[Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Create/Drop/Truncate Table Alter Table/Partition/Column Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function Create/Drop/Grant/Revoke Roles

create table as 和create table like的区别

create table as 和create table like的区别 对于MySQL的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢? /* -- 没有开启gtid的情况下,不拷贝数据,只创建一模一样的表结构,包括索引约束等,结合insert语句可以实现复制一个表的结构和数据的目的 create table tbl_test_bak like tbl_test; insert into tbl_test_bak select

jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.

jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool. 开始钻研工作流的东西,第一颗钉子,笔记之: 错误信息: jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table.   Run the create.jbpm.schema target first in the instal

[Hive - LanguageManual] Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function

Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version information Icon View support is only available in Hive 0.6 and later. Create View CREATE VIEW [IF NOT EXISTS] view_name [(column_name [COMMENT column_com

实验:实现多实例 、数据库的create和alter、create表

多实例:软件程序运行了多次,规划:第一个端口在" 3306 ",第二个端口在" 3307 ",第三个端口在" 3308 ",在对外提供服务时,就感觉是三个mysql服务器一样.(此实验与mariadb的版本无关)1.安装mariadb,[root@centos7 ~]# yum install mariadbLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cac

How To Convert A Partitioned Table To A Non-Partitioned Table Using DataPump In 11g (Doc ID 1276049.1)

APPLIES TO: Oracle Database Exadata Cloud Machine - Version N/A and laterOracle Cloud Infrastructure - Database Service - Version N/A and laterOracle Database Cloud Exadata Service - Version N/A and laterOracle Database Exadata Express Cloud Service

Failed to load slave replication state from table mysql.gtid_slave_pos: 1146: Table 'mysql.gtid_slave_pos' doesn't exist

http://anothermysqldba.blogspot.com/2013/06/mariadb-1003-alpha-install-on-fedora-17.html MariaDB 10.0.3 Alpha install on Fedora 17 x86_64 MariaDB 10.0.3 Alphawas just released. So for those of you that recall my previous MariaDB 5.5 install post, I d

bootstrap table 实现固定悬浮table 表头并可以水平滚动

在开发项目中,需要将表格头部固定,而且表格大多数情况下是会水平滚动的.项目的css框架是bootstrap 3,故也可以叫做bootstrap table. 需要实现的是:表格头部固定,并且支持水平滚动 html code(source table): <table id="top_fix_table" border="0" cellpadding="4" cellspacing="0" class="tabl

通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Atte