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

Oracle creates a fixed-length self-increasing ID

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

The number of ORACLE is different from that of SQL SERVER. SQL SERVER can set a value of self-increment.

ORACLE needs to create a SEQUENCE first, and then use NEXTVAL.

For example, if you want to generate such a code, 0001 # 0002 # 0003.

First, an integer cyclic sequence of 1murn is established.

CREATE SEQUENCE DATE_SEQ START WITH 1-starting value (default is 1 can be omitted) INCREMENT BY 1-can be omitted (default 1 can be omitted) MAXVALUE 9999-maximum value 9999 CYCLE-loop NOCACHE-not cached (to avoid discontiguous values) ORDER-ensure that values are generated in order example: create sequence TEST_ID minvalue 1 maxvalue 9999start with 1 increment by 1 nocache

With a cyclic sequence of 1-9999, we can

TO_CHAR (DATE_SEQ.NEXTVAL) converts it to character form first.

Then LPAD (TO_CHAR (DATE_SEQ.NEXTVAL), 4Magne0') completes the length of it with 4 bits plus'0'. For example 2, the completion is 0002.

Then add the inserted date (as long as the year and month)

TO_CHAR (SYSDATE,'YYYYMM') | | LPAD (TO_CHAR (DATE_SEQ.NEXTVAL), 4 record 0')-this enables the date + encoding format.

Original address: http://plat.delit.cn/thread-144-1-1.html

Please indicate the source of the reprint:

Written by: measurement Technology http://www.delit.cn

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