Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Self-increment implementation of oracle column

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1.Sequence+Trigger implements self-increment of Oracle columns

CREATE SEQUENCE sequence name

[INCREMENT BY n]

[START WITH n]

[{MAXVALUE/MINVALUE n | NOMAXVALUE}]

[{CYCLE | NOCYCLE}]

[{CACHE n | NOCACHE}]

Create a test table

Sys@newtestCDB > 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

Sys@newtestCDB > CREATE SEQUENCE test_seq

Sequence created.

Elapsed: 00:00:00.02

Elapsed: 00:00:00.38

Sys@newtestCDB > 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

Sys@newtestCDB > INSERT INTO TEST (DESCRIPTION) VALUES ('do not specify ID')

1 row created.

Elapsed: 00:00:00.08

Sys@newtestCDB > INSERT INTO TEST (ID, DESCRIPTION) VALUES (TEST_USER.DEPT_SEQ.NEXTVAL, 'specify ID')

INSERT INTO TEST (ID, DESCRIPTION) VALUES (TEST_USER.DEPT_SEQ.NEXTVAL, 'specify ID')

*

ERROR at line 1:

ORA-02289: sequence does not exist

Elapsed: 00:00:00.01

Sys@newtestCDB > INSERT INTO TEST (ID, DESCRIPTION) VALUES (TEST_SEQ.NEXTVAL, 'specify ID')

1 row created.

Elapsed: 00:00:00.01

Sys@newtestCDB > SELECT * FROM TEST

ID DESCRIPTION 1 does not specify ID 3 specify ID

Elapsed: 00:00:00.03

2.DEFAULT Values Using Sequences

Sys@newtestCDB > INSERT INTO TEST (description) VALUES ('DESCRIPTION only')

1 row created.

Elapsed: 00:00:00.01

Sys@newtestCDB > INSERT INTO TEST (id, description) VALUES (999, 'ID=999 and DESCRIPTION')

1 row created.

Elapsed: 00:00:00.00

Sys@newtestCDB > INSERT INTO TEST (id, description) VALUES (NULL, 'ID=NULL and DESCRIPTION')

1 row created.

Elapsed: 00:00:00.01

Sys@newtestCDB > SELECT * FROM test

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

Sys@newtestCDB > 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

Sys@newtestCDB > INSERT INTO TEST (description) VALUES ('DESCRIPTION only')

1 row created.

Elapsed: 00:00:00.01

Sys@newtestCDB > INSERT INTO TEST (id, description) VALUES (999, 'ID=999 and DESCRIPTION')

1 row created.

Elapsed: 00:00:00.01

Sys@newtestCDB > INSERT INTO TEST (id, description) VALUES (NULL, 'ID=NULL and DESCRIPTION')

1 row created.

Elapsed: 00:00:00.01

Sys@newtestCDB > SELECT * FROM test

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

Elapsed: 00:00:00.02

3. Look in the last article.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report