惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 一般一般

linux ps命令使用详解 OBIEE的迁移部署 Oracle IN 和 EXISTS比较小结[转] parentNode、parentElement,childNodes、children 它们有什么 css颜色渐变-Z - 一般一般 - 博客园 好久没来了 wuyu的vs2005 地址收藏 test google whether A point is included in the polygon 2008-5-16 my map [转载]深入浅出URL编码 将dom.document存放在文件中 - 一般一般 - 博客园 [zhuan]女性:25岁为生活而工作,35岁为工作而生活 有感于价格上涨 今天碰到一个AO郁闷的问题,解决了,贴出来,大家交流 [转]JSP中,AJAX使用POST方式提交中文乱码问题解决 美女与野兽 Java 的String.Split(arg)?? - 一般一般 - 博客园
Oracle插入数据时获取自增ID
一般一般 · 2009-08-27 · via 博客园 - 一般一般

自增字段:

表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后有效:) 所以不会存在取错值的问题。