表存在则删除(一句sql实现)

来源:互联网 发布:ios看书软件 编辑:程序博客网 时间:2024/06/05 18:04

通过一句sql实现:

1、表存在则删除;2、表不存在则创建;


mysql:

drop table if exists `test`;create table if not exists `test` (`id` integer not null, `name` varchar(10), primary key(`id`));


sqlserver:

if exists (select count(*) from [sys].[schemas] S JOIN [sys].[tables] T ON S.schema_id = T.schema_id where S.name='ms-1')drop table [ms-1];if not EXISTS(select count(*) from [sys].[schemas] S JOIN [sys].[tables] T ON S.schema_id = T.schema_id where S.name='ms-1')CREATE TABLE [ms-1] ([id] INTEGER not null,[name] VARCHAR(10),PRIMARY KEY([id]);

oracle:

declare num number; beginselect count(*) into num from all_tables where TABLE_NAME = 'tt-1' and OWNER='ODBC'; if num=1 then execute immediate 'drop table "tt-1"';end if;end;


dameng:

同oracle。