oracle数据库配置主键自增+触发器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24521431/article/details/81627600

第一步设置主键自增:

可以用plsql工具,,右键新建,

Owner--属于哪个数据库     Start with ---从哪个id开始(例如你想id自增从1000开始,这就写1000)

Name---为自增的一个名字(一般为seq_加名字)    increment by----每次自增长多少(就是每次id增加多少)

Min value--id的最小值               cache size---写20,(不知道什么意思,没看)

Max value--最大值(写99999999999就行)

这是我配置好的。

这是我的seq,可以在sequences中看见。

第二部,备注触发器,可以直接用语句创建,在表面下面的triggers中也可以看见,若显示红叉则表示有错误,

create or replace trigger seq_distributeid  
--seq_distributeid  是触发器名,我起的和刚才建立主键自增的seq名字一样
  before insert on T_NQ_DISTRIBUTE  
 --T_NQ_DISTRIBUTE   是表名
  for each row
declare
  nextid number;
begin
  IF :new.distributeid IS NULL or :new.distributeid=0 THEN 
--distributeid 是自增的列名
    select seq_distributeid.nextval 
--seq_distributeid正是刚才创建的seq
    into nextid
    from sys.dual;
    :new.distributeid:=nextid;
--distributeid 是自增的列名
  end if;
end tri_test_id;
-----其他地方不用改

这是我创建的语句。

可以在这看见创建好的触发器

建立好后就可以直接插入了,不用给主键值。

猜你喜欢

转载自blog.csdn.net/qq_24521431/article/details/81627600