在INSERT查询中为主键使用顺序值 [英] Using sequential values for the primary key in an INSERT query

查看:85
本文介绍了在INSERT查询中为主键使用顺序值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为具有顺序主键的Oracle数据库编写插入查询,以便插入语句自动获取序列中的下一个数字?

How can I write an insert query for an Oracle database which has a sequential primary key so that the insert statement automatically takes the next number in the sequence?

INSERT INTO LD_USER_ROLE(USER_ROLE_ID,INS_USER,INS_DATE, USERNAME) 
VALUES (100, 'sp22',to_date('2003/05/03 21:02:44','yyyy/mm/dd hh24:mi:ss'),'JOHN BARRY', )

在上面的语句中,我已将键"USER_ROLE_ID"的值硬编码为100,但我想按照第一段中的说明进行更改.

In the above statement I have hardcoded the value of 100 for the key 'USER_ROLE_ID' but I'd like to alter this as explained in the first paragraph.

推荐答案

除了使用触发器之外,您还可以直接在insert语句中使用序列:

Apart from using a trigger, you can use a sequence directly in the insert statement:

CREATE SEQUENCE LD_USER_ROLE_SEQ;

INSERT INTO LD_USER_ROLE
   (USER_ROLE_ID,INS_USER,INS_DATE, USERNAME) 
VALUES 
   (ld_user_role_seq.nextval, 'sp22',to_date('2003/05/03 21:02:44','yyyy/mm/dd hh24:mi:ss'),'JOHN BARRY', )

这篇关于在INSERT查询中为主键使用顺序值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆