手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle利用触发器和sequence实现主键自增长

[日期:2013-01-16] 来源:Linux社区  作者:shihuacai [字体:大 中 小]

Oracle利用触发器和sequence实现主键自增长

建立表

-- Create table
create table GEO_BOOKMARK
(
F_ID NUMBER not null,
F_NAME VARCHAR2(100) not null,
F_REMARK VARCHAR2(200),
F_XMIN NUMBER not null,
F_YMIN NUMBER not null,
F_XMAX NUMBER not null,
F_YMAX NUMBER not null,
F_LEVEL NUMBER not null,
F_LAYERIDS VARCHAR2(50)
)
tablespace PRJZC927
pctfree 10
initrans 1
maxtrans 255;
-- Create/Recreate primary, unique and foreign key constraints
alter table GEO_BOOKMARK
add constraint F_PKID primary key (F_ID)
using index
tablespace PRJZC927
pctfree 10
initrans 2
maxtrans 255;

建立Sequence

-- Create sequence
create sequence SEQ_GEO_BOOKMARK
minvalue 1
maxvalue 9999999999999
start with 1
increment by 1
cache 20
order;

建立触发器

create or replace trigger trg_geo_bookmark_id
before insert on geo_bookmark
for each row
begin
SELECT seq_geo_bookmark.nextval INTO :new.f_id FROM dual;
end;

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款