我们都知道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. Mac命令行指定特定程序打开文件

    如果文件已被指定默认程序 open httpd.conf 指定一个特定程序打开文件 # 用 sublime text 打开 httpd.conf open -a /Applications/Subli ...

  2. 100行代码打造属于自己的代理ip池

    经常使用爬虫的朋友对代理ip应该比较熟悉,代理ip就是可以模拟一个ip地址去访问某个网站.我们有时候需要爬取某个网站的大量信息时,可能由于我们爬的次数太多导致我们的ip被对方的服务器暂时屏蔽(也就是所 ...

  3. Flutter——TextField组件(文本框组件)

    TextField组件的常用属性: 属性 描述 maxLines 设置此参数可以把文本框改为多行文本框 onChanged 文本框改变的时候触发的事件 decoration hintText 类似 h ...

  4. 【python cookbook】找出序列中出现次数最多的元素

    问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...

  5. vue中读取excel中数据

    安装xlsx npm install xlsx --save-dev 安装好后在需要的页面 引入插件 import xlsx from 'xlsx' 调用 $('#uploadFile').chang ...

  6. 多任务5-协程(IO密集型适用)--gevent完成多任务及monkey补丁

    代码: import gevent def f1(n): for i in range(n): print(gevent.getcurrent(),i) gevent.sleep(1) def f2( ...

  7. vuex的使用介绍

    1.vuex是什么? vuex是一个专为vue.js应用程序开发的状态管理模式(它采用集中式存贮管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化). 2.vuex的核心概念? ...

  8. hive 的 beeline用法

    先开启服务端: nohup hive --service metastore & nohup  hive --service hiveserver2 & 进入beeline: beel ...

  9. sql server join ,inner join ,left join ,right join 的使用

    测试数据脚本 CREATE TABLE Atable ( S# INT, Sname nvarchar(32), Sage INT, Sfrom nvarchar(8) ) insert into A ...

  10. bootstrap Table 的使用方法

    然后添加css  找到bootstrap-table.min.css 添加进去 再添加JS Js添加时  按照顺序添加 然后初始化bootstrap-table <script type=&qu ...