create table xxx as select 与 create table xxx like
create table xxx as select xxx,创建新表,没有原表的完整约束,会把原表的数据拷贝一份,如下:
mysql> desc stu;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| Id | int(9) | NO | PRI | NULL | auto_increment |
| Name | varchar(100) | NO | | NULL | |
| Age | int(9) | NO | | 0 | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set mysql> select * from stu;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime |
+----+------+-----+---------------------+
| 1 | Andy | 28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in set mysql> create table stu2 as select * from stu;
Query OK, 1 row affected
Records: 1 Duplicates: 0 Warnings: 0 mysql> desc stu2;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| Id | int(9) | NO | | 0 | |
| Name | varchar(100) | NO | | NULL | |
| Age | int(9) | NO | | 0 | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
4 rows in set mysql> select * from stu2;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime |
+----+------+-----+---------------------+
| 1 | Andy | 28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in set create table xxx like xxx,创建新表,约束和原表相同,只拷贝表结构,没有拷贝表的数据,如下:
mysql> desc stu;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| Id | int(9) | NO | PRI | NULL | auto_increment |
| Name | varchar(100) | NO | | NULL | |
| Age | int(9) | NO | | 0 | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set mysql> select * from stu;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime |
+----+------+-----+---------------------+
| 1 | Andy | 28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in set mysql> create table stu3 like stu;
Query OK, 0 rows affected mysql> desc stu3;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| Id | int(9) | NO | PRI | NULL | auto_increment |
| Name | varchar(100) | NO | | NULL | |
| Age | int(9) | NO | | 0 | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set mysql> select * from stu3;
Empty set 如果我想拷贝表的结构(约束和原表相同),同时拷贝表的数据,怎么办?
先create table xxx like xxx,创建表结构,再insert into xxx select xxx 拷贝数据。注意:这里没有as
mysql> desc stu;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| Id | int(9) | NO | PRI | NULL | auto_increment |
| Name | varchar(100) | NO | | NULL | |
| Age | int(9) | NO | | 0 | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set mysql> select * from stu;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime |
+----+------+-----+---------------------+
| 1 | Andy | 28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in set mysql> create table stu4 like stu;
Query OK, 0 rows affected mysql> desc stu4;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| Id | int(9) | NO | PRI | NULL | auto_increment |
| Name | varchar(100) | NO | | NULL | |
| Age | int(9) | NO | | 0 | |
| updatetime | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set mysql> select * from stu4;
Empty set mysql> insert into stu4(name,age,updatetime) select name,age,updatetime from stu;
Query OK, 1 row affected
Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from stu4;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime |
+----+------+-----+---------------------+
| 1 | Andy | 28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in set
create table xxx as select 与 create table xxx like的更多相关文章
- hive基本的操作语句(实例简单易懂,create table XX as select XX)
hive建表语句DML:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Cr ...
- select into 、 insert into select 、create table as select复制表
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但 ...
- create table b1 as select * from b建表锁表测试
A: create table a1 like a; insert into a1 as select * from a; B: create table b1 as select * from b; ...
- create table repo_folder_operate_log_bak as select * from repo_folder_operate_log;
create table repo_folder_operate_log_bak as select * from repo_folder_operate_log;
- MongoDB 实现 create table tab2 as select
1. var result = db.foo.aggregate(...);db.bar.insert(result.result); 2. var temp1 = db.mtb1.find(name ...
- create table:使用SELECT语句创建表
oracle下直接(创建表) create table newtablename as select * from oldtablename sqlserver的语法是(自动创建表) : select ...
- 用复制方式创建表 Create Table tbname as select * from user.tab where ...
用复制方式创建表 Create Table tbname as select * from user.tab where ...
- (转)create table #temptable 临时表 和 declare @bianliang table ()表变量
在开发过程中,经常会遇到使用表变量和本地临时表的情况.下面是对二者的一个介绍: 1. 为什么要使用表变量 表变量是从2000开始引入的,微软认为与本地临时表相比,表变量具有如下优点: a.与其他变量 ...
- create table #temptable 临时表 和 declare @bianliang table ()表变量
create table #temptable 临时表 和 declare @bianliang table ()表变量 在开发过程中,经常会遇到使用表变量和本地临时表的情况.下面是对二者的一个介绍: ...
随机推荐
- Meisell-Lehmer算法(统计较大数据里的素数)
http://acm.hdu.edu.cn/showproblem.php?pid=5901 1e11的数据量,这道题用这个算法花了202ms. #include<bits/stdc++.h&g ...
- ACM题目————一笔画问题
描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整数N(N< ...
- rtc关机闹钟5 AlarmManager研究
AlarmManager研究 侯 亮 转自 http://blog.csdn.net/codefly/article/details/17058425 1.概述 在Android系统中,闹钟和唤醒功能 ...
- easyrtc-server在ubuntu14.04上的安装方法
easyrtc 官网 http://easyrtc.com/ 1.安装nodejs,安装npm (不知道如何安装请google一下) 2. 查看运行easyrtc 所需要的js 包,在easyrtc ...
- 【转】在Eclipse中建立第一个Servlet程序
转载地址:http://kin111.blog.51cto.com/738881/163354 继上篇在Eclipse中搭好了tomcat环境后,我们建立一个最简单的servlet程序,这个serve ...
- MultiSelectComboBox(一)
1. MultiSelectComboBox.xaml <UserControl x:Class="MultiSelectComboBox.MultiSelectComboBox&qu ...
- LINUX 产生PPM 驱动例子
APP: //author:DriverMonkey //phone:13410905075 //mail:bookworepeng@Hotmail.com //qq:196568501 #inclu ...
- Thread的六种状态
线程共有6种状态:在某一时刻只能是这6种状态之一.这些状态由Thread.State这个枚举类型表示,并且可以通过getState()方法获得当前具体的状态类型. 包括(new,runnable,bl ...
- percona-toolkit工具包的使用教程之开发类工具
percona-toolkit工具包的使用教程之开发类工具 1. pt-duplicate-key-checker l 功能介绍: 功能为从mysql表中找出重复的索引和外键,这个工具会将重复的索 ...
- SqlSever基础 dateadd day 增加五天
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...