oracle 查看自增,oracle 兑现自增auto_increament

oracle 实现自增auto_increament

在mysql中,实现字段自增,只用给他设置为auto_increase就ok了

这是简单的傻子啊(但是多数据库共享,分布式计算就不适用了,呵呵)

在oracle中没有这样便捷的功能,所以只能用sequence加trigger来解决这个问题

以下是一个Demo:

创建sequence

-- create sequence of id of the table sql_template_set

create sequence sql_template_id_seq

increment by 1

start with 60

nomaxvalue

nominvalue

nocache;

创建表 sql_template_set (id为主键,并且为自增字段)

create table SQL_TEMPLATE_SET

(

SENTENCE VARCHAR2(2000),

TEMPLATESQL VARCHAR2(300),

TABLEIDS VARCHAR2(100),

ID NUMBER(6) not null

)

alter table SQL_TEMPLATE_SET

add constraint TEMPLATE_ID primary key (ID)

创建触发器,在insert数据之前,添加id的值

-- 创建自增触发器

create or replace trigger sql_template_con_tri

before insert on sql_template_set for each row

begin

-- 设置sql主键自增

select sql_template_id_seq.nextval into :new.id from dual;

end;

完毕,现在可以测试一下下

-- 测试表trigger

insert into sql_template_set(sentence,templatesql,tableids) values('dirktest','dirktest','dirktest')

commit;

结果主键id就从60开始,每加条数据递增1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值