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. TPS54331 TPS54332 3.5V to 28V Input, 3A, 570kHz Step-Down Converter with Eco-mode

    The TPS54331 is a 28-V, 3-A non-synchronous buck converter that integrates a low RDS(on) high side M ...

  2. Flutter从零到∞学习笔记

    有状态widget:StatefulWidget和无状态widget:StatelessWidget 前者不需要实现Widget build(BuildContext context). 具体的选择取 ...

  3. Html table、thead、tr、th、td 标签

    Html table.thead.tr.th.td 标签 案例一 <!-- table 表格标签,配置表格使用.border="1" 添加表格框架 --> <ta ...

  4. elasticsearch 索引备份恢复

    备份脚本 es_backup.sh : #!/bin/bash#备份昨天数据,删除30天前索引 host=`hostname`address="xxx@xxx.com" es_us ...

  5. StringUtils工具类常用方法汇总(判空、转换、移除、替换、反转)

    Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常方便.最近自己也经常在项目中使用到了里面的一些方法,在这里将常用的方法总 ...

  6. [LspTrace]ReadAccConfig returns false! QT

    当qt运行出现以下信息时 [LspTrace]ReadAccConfig returns false! [LspTrace]FindMatchingProvider: inInfo is one of ...

  7. Ansible在Ubuntu上的安装

    #apt安装 apt-get install software-properties-common apt-add-repository ppa:ansible/ansible apt-get upd ...

  8. sublime汉化

    1.打开sublime使用快捷键 shift+ctrl+p调出package control; 2.键入Package Control:install package 会弹出一个输入框,然后再搜索lo ...

  9. CentOS7设置阿里镜像

    1. 备份原来的yum源 sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2.设置ali ...

  10. Rancher 容器管理平台-实战训练营-免费视频培训

      2018年5月-2019年5月免费培训视频(共21期): http://e.vhall.com/user/home/20160226 培训简介: Rancher2.0版基于开源的Kubernete ...