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 ()表变量 在开发过程中,经常会遇到使用表变量和本地临时表的情况.下面是对二者的一个介绍: ...
随机推荐
- POJ 3903:Stock Exchange(裸LIS + 二分优化)
http://poj.org/problem?id=3903 Stock Exchange Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- [团队项目]SCRUM项目6.0 7.0 (新)
6.0----------------------------------------------------- sprint演示 1.坚持所有的sprint都结束于演示. 团队的成果得到认可,会感觉 ...
- winform——绑定DataGridView
==========================================================================================重点需要掌握==Au ...
- Cookie案例
实现的功能是:在页面上点击,各种书名,跳转到另一个页面,在点解return 回到原来的页面,并且最下面出现刚才点击的书名:进行多次操作,都会出现刚刚点击的书名: 类似于:在浏览器上显示你登录的记录: ...
- JAVA基础知识之多线程——控制线程
join线程 在某个线程中调用其他线程的join()方法,就会使当前线程进入阻塞状态,直到被join线程执行完为止.join方法类似于wait, 通常会在主线程中调用别的线程的join方法,这样可以保 ...
- InputStream和Reader区别
InputStream,OutputStream 前者为字节输入流,后者为字节输出流.Reader Writer 前者为字符输入流,后者为字符输出流. 四个均为抽象类.fileInputStr ...
- 利用Velocity结合Spring发email
在spring中发mail是一件容易的事,如果利用Velocity做mail的模板来发送就更得心应手了. 首先,还是简单描述sping中的配置,发mail需要一个mail的engin: <bea ...
- Codeforces Round #372 (Div. 2) A
Description ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecut ...
- Codeforces Round #370 (Div. 2) A
Description There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai ...
- 网页3D效果库Three.js初窥
网页3D效果库Three.js初窥 背景 一直想研究下web页面的3D效果,最后选择了一个比较的成熟的框架Three.js下手 ThreeJs官网 ThreeJs-github; 接下来我会陆续翻译 ...