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

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks 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后有效:) 所以不会存在取错值的问题。