易截截图软件、单文件、免安装、纯绿色、仅160KB

Oracle插入数据时获取自增ID

自增字段:
表atable(id,a) id需要自增 首先建立一个序列:
create sequence seq_atable minvalue 1 maxvalue 999999999999999999 start with 1 increment by 1 nocache
有二种方式使用自增字段:
使用序列+触发器实现自增,插入语句不需要管自增字段
如:create or replace trigger trg_atable before insert on atable for each row begin select seq_atable.nextval into :new.id from dual; end;
插入数据:insert into atable(a) values('test');
注:我创建了sequence 和trigger :,之后在procedure中插入数据,插入的时候没有管ID字段,在应用中,使用了hibernate,虽然hibernate在增加记录的时候也会处理ID,但是添加记录之后,查询记录发现,ID还是根据序列和触发器的规则设置的
仅使用序列,需要在插入数据时,自增字段插入序列下一个值
如:insert into atable(id,a) values(seq_atable.nextval,'test');
三、返回刚插入记录的自增字段值
如上面的例子,我们插入一条记录后,我想马上返回刚插入的记录的ID号,我该怎么处理呢?
首先要解决自增字段的问题,上面的二种方法哪种更适合这种用法呢? 建议使用第二种自增序列,否则处理起这个问题来比较麻烦。
使用自增字段的第二种方法,在插入一条记录后马上执行一下下面的语句即返回当前插入数据的ID。
$query="select seq_atable.currval from dual";
seq_atable.currval 的值只有在同一次会话中,发生seq_atable.nextval后有效:) 所以不会存在取错值的问题。


相关文档:

oracle学习(简单查询)

    (1)查询C01课程成绩不为Null的学生的姓名和成绩。
         分析:因为涉及到课程成绩和学生姓名,需要对student表和sc表进行连接。
代码:
select sname,grade from student,sc where student.sno=sc.sno and c ......

oracle常用命令详解

oracle里的常用命令详解
本文针对oracle日常的名令做了介绍,欢迎各位大侠多提宝贵意见和多多补充!

日志管理
1.forcing log switches
sql> alter system switch logfile;
2.forcing checkpoints
sql> alter system checkpoint;
3.adding online redo log groups
sql> alter database add logfil ......

PHP 中使用oracle 环境配置

http://www.oracle.com/technology/global/cn/pub/notes/technote_php_instant.html
为 Linux 和 Windows 安装 PHP 和 Oracle 10g Instant Client
作者:Christopher Jones,甲骨文公司的咨询技术人员
发布日期:2004 年 12 月
Oracle 10g Instant Client(免费下载)是PHP 与远程 Oracle 数据库连接的最简单方式,它 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号