前言

因为现在所在项目组用的数据库是oracle,相较于序列这块,不是很懂.特此做下笔记,留着备用.

创建序列:

create sequence seq_xx   --创建序列名称
increment by 1  --增长幅度
start with 1  --初始值
maxvalue 9999999999999999;  --最大值

查询序列:

--每查询一次,序列按自定义增长;
select res.res_mnp_ported_num$seq.nextval

删除序列

DROP SEQUENCE seq_xx;

判断序列是否存在,存在则删除:

declare   
 V_NUM number;   
BEGIN  
  ----多次删除时,每次都将v_num设置成为0
    V_NUM := 0;  
    ----判断序列 seq_name_1 是否存在(区分大小写)
    select count(0) into V_NUM from user_sequences where sequence_name = 'SEQ_XX'; 
    ----如果存在立即删除  
    if V_NUM > 0 then   
    execute immediate 'DROP SEQUENCE  SEQ_XX'';   
    end if;

END;

/
--加了“/”之后,可以在后面接其他的SQL语句;

查询序列大小写问题

select * from user_sequences; --查找用户建的序列

我们发现字段SEQUENCE_NAME的值里面有刚才创建的SEQ_XX,这就解释了为什么判断存在时候名字区分大小写了

Oracle 判断数据表是否存在,若存在删除

--copy old table and data to a new table
create table xxx_new as select * from xxx;

--drop old table and create a new table with the same name
drop table xxx;
create table xxx ()----------------新表字段名

--transfer the old data from backup table to the new table
insert into xxx 
()    -------------------旧表字段名
select 
---------------------旧表字段名
from xxx_new;

commit;