1、复制表结构及数据到新表
CREATE TABLE 新表 SELECT * FROM 旧表
这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable;来删除。
不过这种方法的一个最不好的地方就是新表中没有了旧表的primary key、Extra(auto_increment)等属性。需要自己用"alter"添加,而且容易搞错。

2、只复制表结构到新表
CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2
或CREATE TABLE 新表  LIKE 旧表

这个和上面一样主键索引和一些属性都会消失。

原来的表:

MariaDB [syw]> desc news_person;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.05 sec)

执行后:

MariaDB [syw]> create table news_person_new select * from news_person where 1=2;
Query OK, 0 rows affected (0.49 sec)
Records: 0 Duplicates: 0 Warnings: 0

查看索引:

MariaDB [syw]> show index from news_person;
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| news_person | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec) MariaDB [syw]> show index from news_person_new;
Empty set (0.00 sec)

正确方法:

CREATE TABLE 新表
LIKE 旧表

MariaDB [syw]> create table news_person_new1 like news_person;
Query OK, 0 rows affected (0.24 sec) MariaDB [syw]> desc news_person_new1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.03 sec) MariaDB [syw]> show index from news_person_new1;
+------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| news_person_new1 | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

Mysql 复制一个新表的更多相关文章

  1. CREATE TABLE AS - 从一条查询的结果中创建一个新表

    SYNOPSIS CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name [ (column_name [, ...] ...

  2. SELECT INTO - 从一个查询的结果中创建一个新表

    SYNOPSIS SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expression [ AS output_name ] [ ...

  3. oracle 根据一个表生成另一个新表和一个现有表给一个新的表赋值

    1,添加表B ,和A表表结构相同(带数据) create table B  as select * from A; 2,添加表B ,和A表表结构相同(不带带数据) create table B  as ...

  4. CREATE TABLE - 定义一个新表

    SYNOPSIS CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( { column_name data_ty ...

  5. sql 如何把查询得到的结果如何放入一个新表中

    如何把这个查询到的结果放到一张新表中? 2014-03-13 15:26   提问者采纳   表已经存在:insert into 表名 (列名1... 列名n) select 列名1....列名n f ...

  6. ORACLE 对一个表进行循环查数,再根据MO供给数量写入另一个新表

    一. 加工处理后要变成如下效果 create table test1 (sonum varchar2(10),lineid varchar2(10),qty int ,qty2 int ,remark ...

  7. MySQL查询结果复制到新表(更新、插入)

    MySQL中可以将查询结果复制到另外的一张表中,复制的话通常有两种情况,一种是更新已有的数据,另一种是插入一条新记录.下面通过例子来说明.首先构建两个测试表. 表t1: 表t2: 1.如果t2表中存在 ...

  8. MySQL完整复制表到另一个新表

    1. 复制表结构 CREATE TABLE newuser LIKE user; 2. 导入数据 INSERT INTO newauser SELECT * FROM user;

  9. mysql复制一个表到其他数据库

    db1为原数据库,db2为要导出到的数据库,fromtable 是要导出的表名1.方法一:登录导出到的数据库,执行create table fromtable select * from db1.fr ...

随机推荐

  1. 基础的基于QT的图像查看程序

    代码来自<QT5.9c++开发指南>,因为实现了图片的遍历显示,对于将来编写ImageShop一类的图像程序来说将非常有用(这个程序目前存在一定问题,在研究过程中进行解决) 一.基本功能 ...

  2. [译]Quartz.NET 3.x 教程

    译者注: 最近有点小浮躁,找点事做做平静下内心的焦作,干脆翻译下 Quartz.NET 3.x Tutorial 好了. Quartz.NET 3.x 教程 选择课程:带划线的表示没完成 课程 1: ...

  3. 622 CircularQueue C#

    public class MyCircularQueue { int[] Queue=null; int _Front = 0; int _Rear = 0; int Length = 0; int ...

  4. leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...

  5. HDFS初次编程

    hadoop是用Java语言实现的开源软件框架,可以支持多种语言,我学习的时候用得自然就是Java了. 在开始编程之前需要做一些配置工作: Hadoop开发:Hadoop为HDFS和Mapreduce ...

  6. VirtualBox for mac

    一. VirtualBox 1.下载 官网: https://www.virtualbox.org/wiki/Downloads 2.安装 傻瓜式安装即可 二.centOS7 1.下载 centOS: ...

  7. spring-cloud-config-server——Environment Repository

    参考资料: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.0.RELEASE/single/spring-cl ...

  8. MVC _Ajax的使用【七】

    一.本篇主要写的是在MVC项目中一种ajax的使用方法 1.  首先在控制器中创建两个方法,showCreate()和AddUserInfo() using System; using System. ...

  9. B站(Bilibili) 视频的下载。

    1) 第一种是众所周知的方法,在URL的 bilibili 前加个 i, 就可以有视频的mp4的地址,然后用下载器下载. 比如 想 下载 https://www.bilibili.com/video/ ...

  10. 利用Fiddler编写Jmeter接口测试

    利用Fiddler抓包APP应用接口,在Jmeter编写接口测试脚本 1.用Fiddler对Android用用进行抓包 Fiddler介绍: Fiddler是一款非常流行并且实用的http抓包工具,它 ...