This article brings you relevant knowledge about Oracle, and explains the sequence SEQUENCE in Oracle in detail. The article introduces it in detail through sample code. It has certain reference value for everyone's study or work, and I hope it will be helpful to everyone.
Recommended tutorial: "Oracle Video Tutorial"
1. Sequence introduction
Oracle's sequence is a A database object whose main function is to generate unique values. After the sequence is created, the sequence object can be found through the data dictionary, so the sequence can be shared by multiple objects.
2. Create a sequence
The sequence is created using the CREATE SEQUENCE syntax:
CREATE SEQUENCE sequence [INCREMENT BY n] [START WITH n] [{MAXVALUE n | NOMAXVALUE}] [{MINVALUE n | NOMINVALUE}] [{CYCLE | NOCYCLE}] [{CACHE n | NOCACHE}];
- NCREMENT BY: used to define the step size of the sequence, if omitted, it defaults Is 1. If it is a negative value, it means that the value of the sequence is decreasing.
- START WITH: Defines the initial value of the sequence (that is, the first value generated), the default is 1.
- MAXVALUE: Defines the maximum value that the sequence can generate. NOMAXVALUE is the default option, which means there is no maximum value. At this time, for an increasing sequence, the maximum value the system can generate is 10 to the 27th power; for a decreasing sequence, the maximum value is -1.
- MINVALUE: Defines the minimum value that the sequence can generate. NOMINVALUE is the default option, which means there is no minimum value. At this time, for the descending sequence, the minimum value the system can generate is negative 10 to the 26th power; for the descending sequence, the minimum value is 1.
- CYCLE and NOCYCLE: Indicate whether to loop when the value of the sequence generator reaches the limit. If looping, when the increasing sequence reaches the maximum value, it loops to the minimum value; for the descending sequence, when it reaches the minimum value, it loops to the maximum value. If you do not loop, after reaching the limit value, an error will occur if you continue to generate new values.
- CACHE: Defines the size of the memory block to store the sequence, the default is 20. NOCACHE means no memory buffering of the sequence. Memory buffering of sequences can improve sequence performance.
For example:
CREATE SEQUENCE invoice_seq INCREMENT BY 1 START WITH 1 MAXVALUE 9999999 NOCYCLE NOCACHE;
3. Query sequence
Once the sequence is created, the sequence creation code is textualized in the data dictionary and can be found in the user_objects data See it in the dictionary, such as:
SELECT object_name,object_id,object_type FROM user_objects WHERE object_name = 'INVOICE_SEQ';
The sequence details are saved in the user_sequences table:
SELECT sequence_name, min_value, max_value, increment_by, last_number FROM user_sequences;
4. Use the sequence
NEXTVAL and CURRVAL pseudo columns
- NEXTVAL: Returns the next available sequence value. It returns a unique referenced value each time, and this is actually true for different users. When using sequence.NEXTVAL, a new sequence number is generated and the current sequence number is put into CURRVAL.
- CURRVAL: Get the current sequence value. If you use CURRVAL before using NEXTVAL for the first time, an error will be reported.
is used as follows:
SELECT invoice_seq.CURRVAL,invoice_seq.NEXTVAL FROM DUAL; INSERT INTO invoice (invoice_id, vendor_id, invoice_number, invoice_total ) VALUES (invoice_seq.NEXTVAL, 10, 'INV' || invoice_seq.CURRVAL, 100 );
NEXTVAL and CURRVAL can be used in the following context:
- is not part of a subquery The field list of the SELECT statement.
- The SELECT list of the subquery in the INSERT statement.
- VALUES clause in the INSERT statement.
- SET clause in UPDATE statement.
NEXTVAL and CURRVAL cannot be used in the following context:
- The SELECT list of a view.
- SELECT statement with DISTINCT.
- SELECT statement with GROUP BY, HAVING or ORDER BY clause.
- Clauses in SELECT, DELETE or UPDATE statements.
- DEFAULT expression in CREATE TABLE or ALTER TABLE statement.
Also note that ROLLBACK does not roll back the sequence value.
5. Modify the sequence
For example:
ALTER SEQUENCE invoice_seq INCREMENT BY 2 MAXVALUE 10 NOCACHE NOCYCLE;
When modifying the sequence, there are the following restrictions:
- The table sequence cannot be started initial value.
- The minimum value cannot be greater than the current value.
- The maximum value cannot be less than the current value.
- The modified sequence rules will not affect previous sequence values, only future sequence values will be affected.
- Users must have ALTER SEQUENCE permissions.
6. Delete sequence
DROP SEQUENCE invoice_seq;
7. Create auto-increment sequence
1. Create a sequence
create sequence sq_recid minvalue 1 maxvalue 999999 increment by 1 start with 1 noCYCLE;
2. Create a trigger
create or replace trigger trg_test before insert on test for each row begin select sq_recid.nextval into :new.ID from dual; end; alter trigger trg_test enable;
3. In C#, you can also manually insert sequences into the table
string sql="insert into test(ID,otherCol)value (Sql_recid.nextval,***) retuing ID into :ID"
Recommended tutorial: "Oracle Video Tutorial"
The above is the detailed content of Oracle sequence SEQUENCE analysis. For more information, please follow other related articles on the PHP Chinese website!

方法:1、利用“select*from user_indexes where table_name=表名”语句查询表中索引;2、利用“select*from all_indexes where table_name=表名”语句查询所有索引。

oracle asm指的是“自动存储管理”,是一种卷管理器,可自动管理磁盘组并提供有效的数据冗余功能;它是做为单独的Oracle实例实施和部署。asm的优势:1、配置简单、可最大化推动数据库合并的存储资源利用;2、支持BIGFILE文件等。

在oracle中,可以利用“TO_SINGLE_BYTE(String)”将全角转换为半角;“TO_SINGLE_BYTE”函数可以将参数中所有多字节字符都替换为等价的单字节字符,只有当数据库字符集同时包含多字节和单字节字符的时候有效。

在Oracle中,可利用lsnrctl命令查询端口号,该命令是Oracle的监听命令;在启动、关闭或重启oracle监听器之前可使用该命令检查oracle监听器的状态,语法为“lsnrctl status”,结果PORT后的内容就是端口号。

在oracle中,可以利用“drop sequence sequence名”来删除sequence;sequence是自动增加数字序列的意思,也就是序列号,序列号自动增加不能重置,因此需要利用drop sequence语句来删除序列。

在oracle中,可以利用“select ... From all_tab_columns where table_name=upper('表名') AND owner=upper('数据库登录用户名');”语句查询数据库表的数据类型。

方法:1、利用“LOWER(字段值)”将字段转为小写,或者利用“UPPER(字段值)”将字段转为大写;2、利用“REGEXP_LIKE(字符串,正则表达式,'i')”,当参数设置为“i”时,说明进行匹配不区分大小写。

方法:1、利用“alter system set sessions=修改后的数值 scope=spfile”语句修改session参数;2、修改参数之后利用“shutdown immediate – startup”语句重启服务器即可生效。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),