在oracle里创建列自动增长步骤如下:
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
)
/
("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
NOCACHENOORDERNOCYCLE
3 为该表创建触发器:
CREATE
OR
REPLACE
TRIGGER
"INSERT_DEPARTMENT"
BEFORE
insert on "DEPARTMENT"
for eachrow
begin
select "DEPARTMENT_SEQ".nextval into :NEW.DEPARTMENT_ID from dual;
end ;
/
ALTER TRIGGER "INSERT_DEPARTMENT"ENABLE
/
BEFORE
insert on "DEPARTMENT"
for eachrow
begin
select "DEPARTMENT_SEQ".nextval into :NEW.DEPARTMENT_ID from dual;
end ;
/
ALTER TRIGGER "INSERT_DEPARTMENT"ENABLE
/
4 完毕,接下来如果你需要往表格里插入数据,则DEPARTMENT_ID就可以自动增长了。列最小值为1,最大值为9999,而且增加基数为1,这几个值你都可以根据自己的需要进行修改。
5 Oracle里另一种实现自动增长的方法如下(仍然以上面建的表为例):
首先创建序列:
drop
sequencedepartment_seq;
create sequencedepartment_seq
increment by 1
start with 1
maxvalue 9999999999
nocache;
create sequencedepartment_seq
increment by 1
start with 1
maxvalue 9999999999
nocache;
接下来直接就可以写插入语句了,具体的SQL如下:
insert
into
department
values
(department_seq.nextval,
'
研发部
'
,
8
,
10
);