oracle 请问一下,一张已经建好的表,怎样在其中一列上添加自增列???新手,求解!!!
如:表里有个UserID,现要求加自增,从1000开始,增加量为1.谢谢了,刚接触oracle,不怎么会。...
如:表里有个UserID,现要求加自增,从1000开始,增加量为1.谢谢了,刚接触oracle,不怎么会。
展开
1个回答
展开全部
先建一个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;
/
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;
/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询