时间:2021-07-01 10:21:17 帮助过:9人阅读
一旦定义了emp_sequence,你就可以用CURRVAL,NEXTVAL
CURRVAL=返回 sequence的当前值
NEXTVAL=增加sequence的值,然后返回 sequence 值
比如:
emp_sequence.CURRVAL
emp_sequence.NEXTVAL
可以使用sequence的地方:
不包含子查询、snapshot、VIEW的 SELECT 语句
可以看如下例子:
INSERT INTO emp VALUES
(empseq.nextval, ‘LEWIS‘, ‘CLERK‘,7902, SYSDATE, 1200, NULL, 20);
SELECT empseq.currval FROM DUAL;
但是要注意的是:
由 macro 发布于: 2001-04-13 14:44
作者:Macro Zeng
CREATE TABLE "SPORTS"."LINEUP"("ID" NUMBER NOT NULL, "TYPE"
NUMBER(3) NOT NULL, "BODY" VARCHAR2(100) NOT NULL, "HITS" NUMBER(
10) DEFAULT 0 NOT NULL, PRIMARY KEY("ID"))
TABLESPACE "TS_SPORTS"
CREATE SEQUENCE "SPORTS"."SPORTS_LINEUP_ID_SEQ" INCREMENT BY 1
START WITH 1 MAXVALUE 1.0E28 MINVALUE 1 NOCYCLE
CACHE 50 NOORDER
CREATE OR REPLACE TRIGGER "SPORTS"."SPORTS_LINEUP_ID_TRIGGER"
BEFORE INSERT
ON "SPORTS"."LINEUP"
FOR EACH ROW
DECLARE
next_id NUMBER;
BEGIN
--Get the next id number from the sequence
SELECT sports_lineup_id_seq.NEXTVAL
INTO next_id
FROM dual;
--Use the sequence number as the primary key
--for the record being inserted.
:new.id := next_id;
END;
CREATE OR REPLACE TRIGGER "SPORTS"."LINEUP_ID_UPDATE_TRIGGER"
BEFORE UPDATE OF "ID" ON "SPORTS"."LINEUP"
FOR EACH ROW
BEGIN
RAISE_APPLICATION_ERROR (-20000,
‘sports_lineup_id_update_trigger : Updates of the ID field‘
|| ‘are not allowed. ‘);
END
虽然这个保护 PRIMARY KEY 的触发器图标有个叉叉,而且,执行更新自增长字段语句报错了,但是,还是阻止了其更新。如果删掉此触发器,则可以更新成功。
oracle sequence的简单介绍(自增长字段)
标签:declare arc 参数 record inf raise 如何 一个 end