Oracle sequence SEQUENCE analysis
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:
1 2 3 4 5 6 7 |
|
- 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:
1 2 3 4 5 |
|
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:
1 |
|
The sequence details are saved in the user_sequences table:
1 |
|
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:
1 2 3 4 |
|
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:
1 |
|
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
1 |
|
7. Create auto-increment sequence
1. Create a sequence
1 2 |
|
2. Create a trigger
1 2 3 4 5 6 7 |
|
3. In C#, you can also manually insert sequences into the table
1 2 |
|
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

Data import method: 1. Use the SQLLoader utility: prepare data files, create control files, and run SQLLoader; 2. Use the IMP/EXP tool: export data, import data. Tip: 1. Recommended SQL*Loader for big data sets; 2. The target table should exist and the column definition matches; 3. After importing, data integrity needs to be verified.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

Creating an Oracle table involves the following steps: Use the CREATE TABLE syntax to specify table names, column names, data types, constraints, and default values. The table name should be concise and descriptive, and should not exceed 30 characters. The column name should be descriptive, and the data type specifies the data type stored in the column. The NOT NULL constraint ensures that null values are not allowed in the column, and the DEFAULT clause specifies the default values for the column. PRIMARY KEY Constraints to identify the unique record of the table. FOREIGN KEY constraint specifies that the column in the table refers to the primary key in another table. See the creation of the sample table students, which contains primary keys, unique constraints, and default values.

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Oracle provides multiple deduplication query methods: The DISTINCT keyword returns a unique value for each column. The GROUP BY clause groups the results and returns a non-repetitive value for each group. The UNIQUE keyword is used to create an index containing only unique rows, and querying the index will automatically deduplicate. The ROW_NUMBER() function assigns unique numbers and filters out results that contain only line 1. The MIN() or MAX() function returns non-repetitive values of a numeric column. The INTERSECT operator returns the common values of the two result sets (no duplicates).

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

An AWR report is a report that displays database performance and activity snapshots. The interpretation steps include: identifying the date and time of the activity snapshot. View an overview of activities and resource consumption. Analyze session activities to find session types, resource consumption, and waiting events. Find potential performance bottlenecks such as slow SQL statements, resource contention, and I/O issues. View waiting events, identify and resolve them for performance. Analyze latch and memory usage patterns to identify memory issues that are causing performance issues.
