加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Oracle数据库中使用触发器和序列实现主键自增

发布时间:2020-12-12 15:29:41 所属栏目:百科 来源:网络整理
导读:一、创建序列 create sequence SEQ_USER_IDminvalue 1maxvalue 99999999999start with 1increment by 1; 二、创建数据库表 create table MS_USER( user_id number(11) not null primary key,mobile_phone varchar2(16) not null,user_name varchar2(32) not

一、创建序列

create sequence SEQ_USER_ID
minvalue 1
maxvalue 99999999999
start with 1
increment by 1;

二、创建数据库表

create table MS_USER(
  user_id number(11) not null primary key,mobile_phone varchar2(16) not null,user_name varchar2(32) not null
);

-- 增加数据库表备注和表字段的备注信息
comment on table MS_USER is '用户信息表';
comment on column MS_USER.user_id is '用户ID';
comment on column MS_USER.mobile_phone is '手机号码';
comment on column MS_USER.user_name is '用户名称';

三、创建触发器

create or replace trigger MS_USER_TRIGGER       
before insert on MS_USER       
for each row       
begin       
select SEQ_USER_ID.nextval into :new.user_id from dual;      
end ;  
/

四、测试

插入记录:

insert into MS_USER(mobile_phone,user_name) values('12345678901','Kevin');

查询记录:

select * from ms_user;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读