首页 技术 正文
技术 2022年11月20日
0 收藏 578 点赞 5,128 浏览 5295 个字
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 setmysql> select * from stu;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime          |
+----+------+-----+---------------------+
|  1 | Andy |  28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in setmysql> create table stu2 as select * from stu;
Query OK, 1 row affected
Records: 1  Duplicates: 0  Warnings: 0mysql> 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 setmysql> select * from stu2;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime          |
+----+------+-----+---------------------+
|  1 | Andy |  28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in setcreate 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 setmysql> select * from stu;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime          |
+----+------+-----+---------------------+
|  1 | Andy |  28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in setmysql> create table stu3 like stu;
Query OK, 0 rows affectedmysql> 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 setmysql> 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 setmysql> select * from stu;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime          |
+----+------+-----+---------------------+
|  1 | Andy |  28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in setmysql> create table stu4 like stu;
Query OK, 0 rows affectedmysql> 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 setmysql> select * from stu4;
Empty setmysql> insert into stu4(name,age,updatetime) select name,age,updatetime from stu;
Query OK, 1 row affected
Records: 1  Duplicates: 0  Warnings: 0mysql> select * from stu4;
+----+------+-----+---------------------+
| Id | Name | Age | updatetime          |
+----+------+-----+---------------------+
|  1 | Andy |  28 | 2015-03-19 15:42:09 |
+----+------+-----+---------------------+
1 row in set
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918