oracle 请问一下,一张已经建好的表,怎样在其中一列上添加自增列???新 ...
发布网友
发布时间:2022-04-26 13:30
我来回答
共1个回答
热心网友
时间:2022-04-10 02:50
先建一个sequence:有按钮可以直接建,也可以sql建
DROP SEQUENCE _SEQUENCE;
CREATE SEQUENCE _SEQUENCE
START WITH 1000
MAXVALUE 99999999
MINVALUE 1
NOCYCLE
NOCACHE
ORDER;
再建一个trigger:
DROP TRIGGER _TRIGGER;
CREATE OR REPLACE TRIGGER _TRIGGER
BEFORE INSERT
ON tablename FOR EACH ROW
DECLARE
nextid NUMBER;
BEGIN
IF :new.UserID IS NULL OR :new.UserID = 0
THEN
SELECT _SEQUENCE.NEXTVAL INTO nextid FROM sys.DUAL;
:new.UserID := nextid;
END IF;
END _TRIGGER;
/