Home >Database >Mysql Tutorial >Oracle 实现类似SQL Server中自增字段的一个办法

Oracle 实现类似SQL Server中自增字段的一个办法

WBOY
WBOYOriginal
2016-06-07 18:06:411108browse

由于Oracle中没有类似SQL Server中的自增字段,所以我们如果想要通过设定类似ID性质的唯一列的话,需要借助Oracle的sequence,先建立一个序列,然后在每次插入数据的时候,通过前触发器来更新ID值,并将序列的序号加1,这样的迂回方式来实现。

相关代码如下:
1. 创建sequence:
代码如下:
CREATE SEQUENCE SEQU_DATA_DATAINFO INCREMENT BY 1 START WITH 1 NOCYCLE NOCACHE NOORDER;

2. 创建触发器:
代码如下:
create or replace TRIGGER TRIG_TEST
BEFORE INSERT ON TABLE1
FOR EACH ROW
DECLARE
tmpVar NUMBER;
BEGIN
tmpVar := 0;
Select SEQU_PROC_ASSOCIATEINFO.NextVal into tmpVar from dual;
:new.procid:=tmpVar;
EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Oracle 数据库操作类Next article:ORACLE 分区表的设计