您的位置:首页 > 数据库 > Oracle

oracle如何实现主键的自增1操作

2008-12-02 13:41 225 查看
create sequence seq_zizeng increment by 1 start with 1 maxvalue 999999999
--创建序列实现向表t_test前插入数据前id自加1

--创建表
create table t_test
(
country_id varchar2(10),
country_name varchar2(100)
)

--创建触发器

create or replace trigger befo_zizeng before insert on
t_test referencing old as old_values new as new_values for each row
begin
select seq_zizeng.nextval into :new_values.country_id from dual;
end;

-- 插入数据,测试效果,呵呵
insert into t_test(country_name) values('aa');
commit;
insert into t_test(country_name) values('bb');
commit;
insert into t_test(country_name) values('cc');
commit;
insert into t_test(country_name) values('dd');
commit;
insert into t_test(country_name) values('ee');
commit;-------------------------------------------------------
你会发现有五条记录,它们的 country_id字段分别是1,2,3,4,5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐