如下操作将对已存在的表进行删除,并创建新表,适用于首次初始化过程。
SqlServer:
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'tableA')drop table tableAgocreate table tableA ( id bigint identity (1, 1) not null primary key, operator_id bigint not null, res_source_key numeric(10, 0) not null, res_source_type int not null)
go
mysql:
drop table if exists tableA;create table tableA ( id bigint(20) not null primary key AUTO_INCREMENT, operator_id bigint(20) not null, res_source_key bigint(20) not null, res_source_type int not null);
commit;
ORACLE:Declare num number;begin select count(1) into num from all_tables where table_name = upper('tableA') ; if num > 0 then execute immediate 'drop table tableA' ; end if;end;/create table tableA ( id number(20) not null primary key, operator_id number(20) not null, res_source_key number(20) not null, res_source_type int not null);
commit;
++++++++++++++++++++++++++++++++++++++++++++++++++
增加表字段时增加保护:
sqlserver:If (not exists (select 1 from sysobjects where id in (select id from syscolumns where name = ‘columnA’) and name = ‘tableA’))begin alter table tableA add columnA varchar(256) null;endgomysql:Delimiter //Create PROCEDURE upgrade()BEGIN declare exist_flag int; select count(0) into exist_flag from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA=‘vnm_db’ and COLUMN_NAME=‘columnA’ and table_name=‘tableA’; if exist_flag = 0 then alter table tableA add columnA varchar(256) null; end ifend;Call upgrade();Drop PROCEDURE upgrade;Commit;//Delimiter;ORACLE:declare column_exist_flag int;BEGIN select count(0) into column_exist_flag from user_tab_columns where column_name=upper(‘columnA’) and table_name=upper(‘tableA’); if column_exist_flag = 0 then execute immediate ‘alter table tableA add columnA varchar(256) null’; end ifend;/
