SQL根据现有表新建一张表

时间:2021-03-01 05:55:51

SQL根据现有表新建表,新建的这张表结构要跟现有表结构相同,但不要现有表里面的数据!

执行DML语句依据数据库类型而定:

SQLITE
-----复制表结构及数据到新表
CREATE TABLE TABLE_NEW AS SELECT * FROM TABLE_OLD;
-----只复制表结构到新表
CREATE TABLE TABLE_NEW AS SELECT * FROM TABLE_OLD WHERE 1=0; ORACLE:
-----复制表结构及数据到新表
CREATE TABLE T_NEW AS SELECT * FROM TABLE_OLD;
-----只复制表结构到新表
CREATE TABLE T_NEW AS SELECT * FROM TABLE_OLD WHERE 1=0; MYSQL:
-----复制表结构及数据到新表
CREATE TABLE T_NEW SELECT * FROM T_OLD
-----只复制表结构到新表
CREATE TABLE T_NEW SELECT * FROM T_OLD WHERE 1=2 ---无数据库实例,找的资料
DB2:
-----复制表结构及数据到新表
CREATE TABLE TABLE_NAME_NEW AS (SELECT * FROM TABLE_NAME_OLD) DEFINITION ONLY;
--插入数据
INSERT INTO T_NEW (SELECT * FROM TABLE_NAME_OLD); SQLSERVER :
-----复制表结构及数据到新表
SELECT * INTO T_NEW FROM T_OLD ;
-----只复制表结构到新表
SELECT * INTO T_NEW FROM T_OLD WHERE 1=2;