Solution 1: Prior to Oracle 11g, sequence assignment to a number variable could be done through a SELECT statement only in trigger, which requires context switching from PL/SQL engine to SQL engine. So we need to create before insert trigger for each row, and assign sequence new value to the column using select into clause.

create table test_tab
(
id number primary key
); create sequence test_seq start with 1 increment by 1 nocycle; create or replace trigger test_trg
before insert on test_tab
for each row
begin
select test_seq.nextval into :new.id
from dual;
end;
/

Solution 2: From Oracle 11g, we can directly assign a sequence value to a pl/sql variable in trigger, So we can create before insert trigger for each row, and assign sequence nextval to the column directly.

create table test_tab
(
id number primary key
); create sequence test_seq start with 1 increment by 1 nocycle; create or replace trigger test_trg
before insert on test_tab
for each row
begin
:new.id := test_seq.nextval;
end;
/

Solution 3: With Oracle 12c, we can directly assign sequence nextval as a default value for a column, So you no longer need to create a trigger to populate the column with the next value of sequence, you just need to declare it with table definition.

create sequence test_seq start with 1 increment by 1 nocycle;

create table test_tab
(
id number default test_seq.nextval primary key
);
 
 

Oracle Auto Increment Column - Sequence as Default Value的更多相关文章

  1. SQL AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  2. SQL-W3School-高级:SQL AUTO INCREMENT 字段

    ylbtech-SQL-W3School-高级:SQL AUTO INCREMENT 字段 1.返回顶部 1. Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INC ...

  3. SQL——AUTO INCREMENT(字段自增)

    AUTO INCREMENT -- 在新记录插入表中时生成一个唯一的数字.插入表数据时,该字段不需规定值.    在每次插入新记录时,自动地创建主键字段的值.在表中创建一个 auto-incremen ...

  4. Oracle composite index column ordering

    Question:  I have a SQL with multiple columns in my where clause.  I know that Oracle can only choos ...

  5. Oracle数据库中序列(SEQUENCE)的用法详解

    Oracle数据库中序列(SEQUENCE)的用法详解   在Oracle数据库中,序列的用途是生成表的主键值,可以在插入语句中引用,也可以通过查询检查当前值,或使序列增至下一个值.本文我们主要介绍了 ...

  6. Oracle 字段监控 ( column monitor)

    Oracle 字段监控 ( column monitor) */--> Oracle 字段监控 ( column monitor) Table of Contents 1. 开启与关闭 2. 字 ...

  7. sql server auto increment - trace flag 272

    从 sql 2012 开始, 微软为了让 insert 时 auto increment 快一些,做了一个 cache 的机制. 这个机制虽然好,但是也有麻烦的情况,如果你的 sql 突然 resta ...

  8. 19. AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  9. mysql AUTO INCREMENT字段 语法

    mysql AUTO INCREMENT字段 语法 作用:在新记录插入表中时生成一个唯一的数字 说明:我们通常希望在每次插入新记录时,自动地创建主键字段的值.我们可以在表中创建一个 auto-incr ...

随机推荐

  1. [Swift通天遁地]二、表格表单-(15)自定义表单文本框内容的格式

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. Linux命令(005) -- kill、pkill和killall的比较

    kill命令用来“杀掉”指定进程PID的进程.终止一个前台进程可以使用Ctrl+C,终止一个后台进程就须用kill命令.kill命令是通过向进程发送指定的信号来结束相应进程的.在默认情况下,kill命 ...

  3. [Codeforces]Codeforces Round #489 (Div. 2)

    Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...

  4. 一、CSS的基础样式

    CSS的基础样式 border 边框 复合写法 border:border-width  border-style border-color: border-width:边框宽度 top right ...

  5. 【PostgreSQL-9.6.3】分区表

    PostgreSQL中的分区表是通过表继承来实现的(表继承博客http://www.cnblogs.com/NextAction/p/7366607.html).创建分区表的步骤如下: (1)创建“父 ...

  6. JS——百度背景图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JS——select标签

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. C#——反射动态创建类的实例

    “反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 若要反射当前项目中的类(即当前项目已经引用它了),可以使用下面的写法. ...

  9. 基于证书的MS SQL2005数据库镜像搭建

    一.准备工作: 3台服务器同版本,硬盘分区大小相同,安装相同版本数据库软件. host中分别标注3台服务器IP和主机名称. 主体服务器上创建数据库,并进行完整备份数据库和数据库事务. 拷贝备份文件给镜 ...

  10. THREE.js代码备份——webgl - scene animation(通过加载json文件来加载动画和模型)

    <!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - sc ...