oracle列自增实现

1.Sequence+Trigger实现Oracle列自增
CREATE SEQUENCE 序列名
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE/MINVALUE n|NOMAXVALUE}]
[{CYCLE|NOCYCLE}]
[{CACHE n|NOCACHE}];
创建测试表
[email protected]CDB> CREATE TABLE TEST(
2 ID NUMBER (10) NOT NULL,
3 DESCRIPTION VARCHAR2 (50) NOT NULL,
4 CONSTRAINT test_pk PRIMARY KEY (ID)
5 );

Table created.

Elapsed: 00:00:00.14
[email protected]CDB> CREATE SEQUENCE test_seq;

Sequence created.

Elapsed: 00:00:00.02

Elapsed: 00:00:00.38
[email protected]CDB> CREATE OR REPLACE TRIGGER test_before_insert
2 BEFORE INSERT ON test
3 FOR EACH ROW
4 BEGIN
5 SELECT test_seq.NEXTVAL INTO :new.id FROM dual;
6 END;
7 /

Trigger created.

Elapsed: 00:00:00.12
[email protected]CDB> INSERT INTO TEST (DESCRIPTION) VALUES (‘不指定ID‘);

1 row created.

Elapsed: 00:00:00.08
[email protected]CDB> INSERT INTO TEST (ID, DESCRIPTION) VALUES (TEST_USER.DEPT_SEQ.NEXTVAL, ‘指定ID‘);
INSERT INTO TEST (ID, DESCRIPTION) VALUES (TEST_USER.DEPT_SEQ.NEXTVAL, ‘指定ID‘)
*
ERROR at line 1:
ORA-02289: sequence does not exist

Elapsed: 00:00:00.01
[email protected]CDB> INSERT INTO TEST (ID, DESCRIPTION) VALUES (TEST_SEQ.NEXTVAL, ‘指定ID‘);

1 row created.

Elapsed: 00:00:00.01
[email protected]CDB> SELECT * FROM TEST;

    ID DESCRIPTION

     1 不指定ID
     3 指定ID

Elapsed: 00:00:00.03
2.DEFAULT Values Using Sequences
[email protected]CDB> INSERT INTO TEST (description) VALUES (‘DESCRIPTION only‘);

1 row created.

Elapsed: 00:00:00.01
[email protected]CDB> INSERT INTO TEST (id, description) VALUES (999, ‘ID=999 and DESCRIPTION‘);

1 row created.

Elapsed: 00:00:00.00
[email protected]CDB> INSERT INTO TEST (id, description) VALUES (NULL, ‘ID=NULL and DESCRIPTION‘);

1 row created.

Elapsed: 00:00:00.01
[email protected]CDB> SELECT * FROM test;

    ID DESCRIPTION

     4 DESCRIPTION only
   999 ID=999 and DESCRIPTION
       ID=NULL and DESCRIPTION
Elapsed: 00:00:00.02

[email protected]CDB> CREATE TABLE TEST(
2 ID NUMBER (10) DEFAULT ON NULL test_seq.NEXTVAL,
3 DESCRIPTION VARCHAR2 (50) NOT NULL
4 );

Table created.

Elapsed: 00:00:00.02
[email protected]CDB> INSERT INTO TEST (description) VALUES (‘DESCRIPTION only‘);

1 row created.

Elapsed: 00:00:00.01
[email protected]CDB> INSERT INTO TEST (id, description) VALUES (999, ‘ID=999 and DESCRIPTION‘);

1 row created.

Elapsed: 00:00:00.01
[email protected]CDB> INSERT INTO TEST (id, description) VALUES (NULL, ‘ID=NULL and DESCRIPTION‘);

1 row created.

Elapsed: 00:00:00.01
[email protected]CDB> SELECT * FROM test;

    ID DESCRIPTION

     5 DESCRIPTION only
   999 ID=999 and DESCRIPTION
     6 ID=NULL and DESCRIPTION

Elapsed: 00:00:00.02
3.看上一篇里面

原文地址:http://blog.51cto.com/shurk/2061598

时间: 2024-11-05 15:48:36

oracle列自增实现的相关文章

Oracle列自增实现(2)-Identity Columns in Oracle Database 12c Release 1 (12.1)

Oracle列自增-Identity Columns in Oracle Database 12c Release 1 (12.1) 在ORACLE 12C以前的版本中,如果要实现列自增长,需要通过序列+触发器实现,到了12C ORACLE 引进了Identity Columns新特性,从而实现了列自增长功能. 一.Identity Columns使用语法 GENERATED [ ALWAYS | BY DEFAULT [ ON NULL ] ] AS IDENTITY [ ( identity

Oracle列自增实现(3)-DEFAULT Values Using Sequences

Oracle 12c中,可以使用序列的NEXTVAL and CURRVAL的值作为默认值,来实现列自增! 一.使用序列的NEXTVAL and CURRVAL的值作为默认值 创建序列 CREATE SEQUENCE t1_seq; 建表 CREATE TABLE t1 ( id NUMBER DEFAULT t1_seq.NEXTVAL, description VARCHAR2(30) ); 插入数据 INSERT INTO t1 (description) VALUES ('DESCRIP

oracle 设置标识列自增

设置reg_user表 userid为自增列 1.设置键 2.创建序列 3.创建触发器 oracle 设置标识列自增

ORACLE列值合併

合併列值最通用的方法就是寫一個自定義函數去實現,這裏介紹的是其他方法. 在SQL Server中合併列值可以使用For Xml Path,在Oracle中則可以使用wm_concat 或 ListAgg. 準備數據: CREATE TABLE MyTest(xType NUMBER,City nvarchar2(200)); / INSERT INTO MyTest(xType,City) SELECT 1,N'北京' FROM dual UNION ALL SELECT 1,N'上海' FRO

基本sql语句--列的增删改

列的增删改   列声明包括:列名称,列属性,[列类型],[默认值]. 增加列:add alter table 表名 add 列声明. 如:alter table boy add height tinyint unsigned not null default 0; 增加的列,默认在表的最后一列. 可以用after声明新增列在那一列后面:alter table 表名 add 列声明 after 已存在列名. 如:alter table boy add age tinyint unsigned no

oracle实现自增id

--oracle实现自增id --创建一张T_StudentInfo表 create table T_StudentInfo ( "id" integer not null primary key, xsName nvarchar2(120) not null, xsAge integer not null, Mobile varchar(12), Email varchar(50), Address nvarchar2(300) ); --创建一个序列,序列名字叫SEQ_Studen

19_列的增删改

表的管理之列的增删改 1.增 alter table 表名 add 列声明; 默认这个新增的列在表的最后一列. 如果不想要新增的列在表的最后一列,那么可以: alter table 表名 add 列声明 after 列名; alter table 表名 add 列声明 first; 2.删 alter table 表名 drop 列名; 3.改 alter table 表名 change 被改变的列名 列声明; ---------------------- 1.默认在表的最后一行增加一列身高sg

oracle列合并

在许多场合,我们会需要用到oracle列合并,oracle提供了一些方法用于把某列的多行数据合并成一行. 一.10G以前使用WMSYS.WM_CONCAT wmsys.wm_concat将字段的值用","来隔开. select id,wm_concat(name) from tab_name group by id; 二.sys_connect_by_path sys_connect_by_path(字段名, 2个字段之间的连接符号),这里的连接符号不要使用逗号,oracle会报错,如

Oracle ID 自增

实现Oracle Id自增 1.方法一 create table app_student( id integer generated by default as identity not null primary key, createtime DATE not NULL); insert into app_student(createtime) values(sysdate); 2. 方法二 创建序列 创建表 CREATE TABLE APP_USER( ID number(20) NOT N