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 ()表变量 在开发过程中,经常会遇到使用表变量和本地临时表的情况.下面是对二者的一个介绍: ...
随机推荐
- PHP 页面编码声明与用header或meta实现PHP页面编码的区别
php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...
- jquery函数和javascript函数的区别
一.窗口加载:http://www.w3school.com.cn/js/js_library_jquery.asp 在 JavaScript 中,您可以分配一个函数以处理窗口加载事件: JavaSc ...
- 单利 复利计算器程序1.0 2.0 3.0 [ 合 ] 之 WEB
对单复利计算器程序进行改进 更新为网页版的. 界面不太美观 请谅解 由于时间问题暂未完善好! 计算部分的主要源代码:
- MFC编程基础
http://www.cnblogs.com/lzmfywz/archive/2012/03/15/2399403.html 一.MFC类库概述 MFC(Microsoft Foundation cl ...
- JAVA基础知识之JVM-——反射和泛型
泛型和Class类 在反射中使用泛型Class<T>可以避免强制类型转换,下面是一个简单例子,如果不使用泛型的话,需要显示转换, package aop; import java.util ...
- springmvc配置文件-2
项目3 WEB.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi= ...
- java的报表下载代码excel
/** * 汇总报表数据下载 * */ private ModelAndView exportSummaryDatadown(HttpServletRequest request, HttpServl ...
- uva 10090 Marbles
Problem F Marbles Input: standard input Output: standard output I have some (say, n) marbles (small ...
- 查看linux发行版本、内核版本命令
查看linux发行版本: $lsb_release -a 查看linux内核版本: $cat /proc/version 或 $uname -a
- 回车和换行在linux下和windows下
今天,我总算搞清楚"回车"(carriage return)和"换行"(line feed)这两个概念的来历和区别了. 1. 在计算机还没有出现之前,有一种叫 ...