我们都知道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. Vue路由嵌套和命名视图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. c# 虚属性

  3. (转)Java锁性能提高有哪些机制?

    转自:https://forum.idevfun.io/t/topic/235/2 Java 中,Synchronized是通过对象内部的一个叫做监视器锁(monitor)来实现的.但是监视器锁本质又 ...

  4. PHP 把返回的数据集转换成Tree树

    /** * 把返回的数据集转换成Tree * @access public * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * ...

  5. 切换Python环境 linux终端命令行

    切换Python环境 conda info -e // 查看有什么环境 source activate env //切换环境 linux终端分屏 terminator  https://www.jia ...

  6. linux网络编程之posix消息队列

    在前面已经学习了System v相关的IPC,今天起学习posix相关的IPC,关于这两者的内容区别,简单回顾一下: 而今天先学习posix的消息队列,下面开始: 接下来则编写程序来创建一个posix ...

  7. vue mint ui 手册文档

    npm 安装 推荐使用 npm 的方式安装,它能更好地和 webpack 打包工具配合使用. npm i mint-ui -S CDN 目前可以通过 unpkg.com/mint-ui 获取到最新版本 ...

  8. 云计算(9)--Gossip:multicast problem

    Gossip/Epidemic ptotocol 解决的问题是multicast problem Gossip 协议是电脑之间的通信协议,受启发与现实社会的流言蜚语.现代分布式系统通常用gossip协 ...

  9. mnist数据的预测结果以及批量处理

    import sys, os sys.path.append('F:\ml\DL\source-code') from dataset.mnist import load_mnist from PIL ...

  10. 01_第一次如何上传GitHub(转)Updates were rejected because the tip of your current branch is behind

    https://www.cnblogs.com/code-changeworld/p/4779145.html 刚创建的github版本库,在push代码时出错: $ git push -u orig ...