oracle 主键自增函数_oracle 实现主键自增
-- 創(chuàng)建表
drop table test;
create table test(id number(10), name varchar2(10));
-- 創(chuàng)建對(duì)列
drop?sequence seq_id;
create sequence seq_id minvalue 1 nomaxvalue start with 1 increment by 1 nocycle nocache;
/*
minvalue 1? ? ?最小值
nomaxvalue? ? ?不設(shè)置最大值(由機(jī)器決定),或根據(jù)表字段的值范圍設(shè)置 maxvalue
start with 1? ?從1開(kāi)始計(jì)數(shù),數(shù)值可變
increment by 1 每次加1,數(shù)值可變
nocycle? ? ? ? 一直累加,不循環(huán)
nocache? ? ? ? 不建緩沖區(qū),如果建立cache那么系統(tǒng)將自動(dòng)讀取cache值個(gè)seq,這樣會(huì)加快運(yùn)行速度;
如果當(dāng)機(jī)或oracle死了,那么下次讀取的seq值將不連貫*/
-- sys 登陸授權(quán)
grant create trigger to scott;
-- 創(chuàng)建觸發(fā)器, 注意創(chuàng)建觸發(fā)器 “end;” 后面必須加一個(gè)回車(chē)和“/”,才可以執(zhí)行;
create or replace trigger test_id_increment
before insert on test
for each row
begin
select seq_id.nextval into:new.id from dual;
end;
/
-- 測(cè)試
select * from test;
insert into test(name) values('張三');
insert into test values(null, '張三');
select * from test;
總結(jié)
以上是生活随笔為你收集整理的oracle 主键自增函数_oracle 实现主键自增的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle数据库怎么导出dat文件_论
- 下一篇: python汉诺塔游戏_Python实战