1 首先创建一个表,例如:
CREATE TABLE "DEPARTMENT"
( "DEPARTMENT_ID" NUMBER,
"DEPARTMENT_NAME" VARCHAR2(30) NOT NULL ENABLE,
"MANAGER_ID" NUMBER(6,0),
"LOCATION_ID" NUMBER(4,0),
PRIMARY KEY ("DEPARTMENT_ID") ENABLE
)
/
2 为DEPARTMENT_ID列创建序列:
CREATE SEQUENCE "DEPARTMENT_SEQ"
MINVALUE 1
MAXVALUE 9999
INCREMENT BY 1
START WITH 1
NOCACHE
NOORDER
NOCYCLE
3 为该表创建触发器:
CREATE OR REPLACE TRIGGER "INSERT_DEPARTMENT"
BEFORE
insert on "DEPARTMENT"
for each row
begin
select "DEPARTMENT_SEQ".nextval into :NEW.DEPARTMENT_ID from dual;
end;
/
ALTER TRIGGER "INSERT_DEPARTMENT" ENABLE
/4 完毕,接下来如果你需要往表格里插入数据,则DEPARTMENT_ID就可以自动增长了。列最小值为1,最大值为9999,而且增加基数为1,这几个值你都可以根据自己的需要进行修改。
阅读(816) | 评论(0) | 转发(0) |