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

2024-12-13 15:38:03
推荐回答(4个)
回答1:

看你用的什么数据库:
Sql server :
select * into table_new from table_old ; 复制结构和数据
select * into table_new from table_old where 1=2;只复制结构
Oracle:
create table table_new as select * from table_old;复制结构和数据
create table table_new as select * from table_old where 1=0;只复制结构
DB2:
--复制表结构
create table table_name_new as (select * from table_name_old) definition only;

--插入数据
insert into table_name_new (select * from table_name_old);
MySql:
----- 复制表结构及数据到新表
CREATE TABLE 新表 SELECT * FROM 旧表

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

回答2:

//复制了表student 的表结构到 studets中了
select top 0 * into studets from student

回答3:

SQLSERVER:select * into b from a where 1 <> 1 或者select top 0 * into b from a
a是原表,b是现有表。
ORACLE:create table b as select * from a where 1 <> 1

回答4:

create table newtable as
select * from oldtable where 1=2