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 ...
- ftp服务的搭建及调用
首先是搭建 ftp server: 下载:http://archive.apache.org/dist/mina/ftpserver/1.0.6/ 下载到本地, 如下图:
- recycleview + checkbox 实现单选
使用map集合记录checkbox的选中状态 private HashMap<Integer,Boolean> positionMap; positionMap = new HashMap ...
- 【转】启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法! .
转载地址:http://blog.csdn.net/zyz511919766/article/details/7442633 原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者j ...
- java 用socket制作一个简易多人聊天室
代码: 服务器端Server import java.io.*; import java.net.*; import java.util.ArrayList; public class Server{ ...
- c#之线程池
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- android的基础入门
一. A通信技术 1. 1G 仅限语音的蜂窝式通话标准 2. 2G (时分多路复用)数字语音传输技术和增加接收数据的功能 3. 3G 传输声音和数据的速度有很大的 提升 4. 4 ...
- camera isp
1. 目标手机摄像头模组用ISP功能模块的市场走向及研发方向.为能够正确认识手机摄像模组行业提供技术及市场依据.2. ISP在模组上的应用原理2.1 功能区域无论数码相机.摄像机或者摄像手机,其影像数 ...
- jQuery常用技巧-使用的总结
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- 我所了解的cgi(转)
2014-08-25 09:01 by 掸尘, 4410 阅读, 6 评论, 收藏, 编辑 当我们在谈到cgi的时候,我们在讨论什么 最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服 ...