我们都知道oracle主键自增利用的是序列sequence。我们先创建一个sequence:

 create sequence test_sequence
start with 1
increment by 1
maxvalue 9999999
nocache

然后新建一张表,例如te_user表:

create table te_user(
user_id number(11) primary key,
user_name varchar2(50),
user_pwd varchar2(50)
)

如果不用trigger的话,我们插入数据是用到了sequence的nextval属性,例如:

insert into te_user values(test_sequence.nextval,'张三','')
或者
insertinto te_user(user_id,user_name,user_pwd) values(test_sequence.nextval,'李四','')

那么如果我们定义了trigger的话,就不需要每次都带入sequence来实现了,新建一个trigger来实现这一目的:

create or replace trigger test_trigger before insert
on te_user
for each row
declare
next_user_id number;
begin
select test_sequence into next_user_id from dual;
:new.user_id :=next_user_id;
end test_trigger

这样每次te_user表的insert操作都会触发此触发器,我们再进行表插入时只需要:

insert into te_user(user_id,user_pwd) values ('王五','')

就可以了。

oracle利用触发器实现主键字段自增的更多相关文章

  1. Oracle创建触发器实现主键自增

    CREATE OR REPLACE TRIGGER "trigger_empl" before insert on extjsTest1.t_empl for each row b ...

  2. oracle 查询索引和主键

    ORACLE: 1.查主键名称: select * from user_constraints where table_name = 'AAA' and constraint_type ='P'; 查 ...

  3. MYSQL的分区字段,必须包含在主键字段内

    MYSQL的分区字段,必须包含在主键字段内   MYSQL的分区字段,必须包含在主键字段内 在对表进行分区时,如果分区字段没有包含在主键字段内,如表A的主键为ID,分区字段为createtime ,按 ...

  4. 【mysql优化】mysql count(*)、count(1)、count(主键字段)、count(非主键字段)哪个性能最佳

    测试结果为:count(*)和count(1)基本相等,count(非主键字段)最耗性能 -- 数据量 708254select count(*) from tmp_test1;-- avg 0.22 ...

  5. cassandra——可以预料的查询,如果你的查询条件有一个是根据索引查询,那其它非索引非主键字段,可以通过加一个ALLOW FILTERING来过滤实现

    cassandra的索引查询和排序 转自:http://zhaoyanblog.com/archives/499.html   cassandra的索引查询和排序 cassandra的查询虽然很弱,但 ...

  6. MySQL 获取物理表的主键字段

    参考代码: /** * 获取主键字段 * @param $table * @param $database * @return mixed */ public function get_primary ...

  7. powerdesigner设置主键为自增字段,设置非主键为唯一键并作为表的外键

    转自:https://www.cnblogs.com/CoffeeHome/archive/2014/06/04/3767501.html 这里powerdesigner连接的数据库是以mysql为例 ...

  8. Oracle 获取表的主键、外键以及唯一约束条件

    Oracle 获取表的主键.外键以及唯一约束条件 Select a.Owner 主键拥有者, a.table_name 主键表, b.Column_Name 主键列, b.Constraint_Nam ...

  9. mysql自增主键字段重排

    不带外键模式的 mysql 自增主键字段重排 1.备份表结构 create table table_bak like table_name; 2.备份表数据 insert into table_bak ...

随机推荐

  1. django_rest framework 接口开发(一)

    1 restful 规范(建议) 基于FbV def order(request): if request.method=="GET": return HttpResponse(' ...

  2. mongodb副本集和分片存储理论整理

    目录 理论概述 一.各种集群简述 二.原理 主从复制 Mongodb副本集 理论概述 一.各种集群简述 mongodb有三种集群搭建方式: 分片:sharding.指为处理大量数据,将数据分开存储,不 ...

  3. ORA-28547: connection to server failed, probable Oracle Net admin error

    现象 C:\Users\Administrator>sqlplus scott/tiger@192.168.1.11:1521/orcl SQL*Plus: Release 11.2.0.4.0 ...

  4. python之约束、加密及logging模块

    一.什么是约束? 在生活中的约束大概就是有什么原因,导致你不能做这件事情了,称之为约束.而在python中的约束是在当多个类中,都需要使用某些方法时,需要人为抛出异常或使用基类+异常处理来进行约束 c ...

  5. centos8 安装 mongodb 4.2 (使用yum)

    1.制作 repo 文件 参考 mongodb 官方的安装文档,使用下面的脚本制作Yum库安装mongodb4.2,但安装过程提示 "Failed to synchronize cache ...

  6. 温控PID自测验程序

    #pragma once #ifndef _PID_H_ #define _PID_H_ #include <vector> #include <map> using name ...

  7. Mongodb创建用户Error: couldn’t add user: Use of SCRAM-SHA-256 requires undigested passwords

    解决方案:修改mechanisms加密方式为SCRAM-SHA-1 db.createUser({ user: "admin", pwd: "xxx", rol ...

  8. js基础知识2

    DOM Document Object Model 文档          对象       模型 对象: 属性和方法 属性:获取值和赋值 方法:赋值方法和条用方法 DOM树 document hea ...

  9. mysql对子查询的优化改写

    重庆SEO:<高性能mysql第三版>提到mysql会将in子查询改写成exists查询(书中基于的mysql版本是5.1.50和5.5) 但是在5.6之后,已经优化成使用半连接查询 SE ...

  10. Centos 拒绝ssh远程暴力破解方法

    佳木斯SEO摘要 有一天突然收到一封邮件,邮件内容告知我的ECS服务器作为肉鸡在攻击别的机器,期初一想,一定是我机器的账号密码被泄露,或者是被人暴力破解,于是乎,我就查询了一下我机器的账号登录记录. ...