Home Database Mysql Tutorial Oracle中如何创建使用SEQUENCES

Oracle中如何创建使用SEQUENCES

Jun 07, 2016 pm 04:46 PM

Oracle提供了sequence对象,由系统提供自增长的序列号,通常用于生成数据库数据记录的自增长主键或序号的地方.

Oracle提供了sequence对象,由系统提供自增长的序列号,通常用于生成数据库数据记录的自增长主键或序号的地方.

下面介绍一下关于sequence 的生成,修改,删除等常用的操作:


1. 创建 Sequence

使用如下命令新建sequence(用户需要有CREATE SEQUENCE 或者CREATE ANY SEQUENCE权限):

CREATE  SEQUENCE test_sequence
 INCREMENT  BY   1    --  每次加的个数据 
 START  WITH   1     --  从1开始计数 
 NOMAXVALUE      --  不设置最大值 
 NOCYCLE        --  一直累加,不循环 
 CACHE  10 ; 
 
[注意]
如果设置了CACHE值,ORACLE将在内存里预先放置一些sequence,以使存取速度更快。cache里面的取完后,oracle自动再取一组到cache。 但是,使用cache可能会跳号, 当遇到数据库突然异常down掉(shutdown abort),cache中的sequence就会丢失.
因此,推荐在create sequence的时候使用 nocache 选项。


2. 使用 sequence:

sequence.CURRVAL    -- 返回 sequence的当前值
sequence.NEXTVAL    -- 增加sequence的值,然后返回 sequence 值

[注意]
第一次NEXTVAL返回的是初始值;
随后的NEXTVAL会自动增加你定义的INCREMENT BY值,然后返回增加后的值。

CURRVAL 总是返回当前SEQUENCE的值,但是在第一次NEXTVAL初始化之后才能使用CURRVAL,,否则会出错。
一次NEXTVAL会增加一次 SEQUENCE的值,所以如果你在同一个语句里面使用多个NEXTVAL,其值就是不一样的。


sequence 存储在数据字典中,存储于user_sequences表
 
LAST_NUMBER 为最终序列号,也就是sequence游标当前所在的位置。

//get sequence last_number

SELECT LAST_NUMBER FROM USER_SEQUENCES WHERE SEQUENCE_NAME=TEST_SEQNAME

// NEXTVAL 使游标指向下一位(增一或减一)

SELECT SEQNAME.NEXTVAL FROM USER_SEQUENCES 得到下一位游标的值


3. 修改 Sequence

用户必须拥有ALTER ANY SEQUENCE 权限才能修改sequence. 可以alter除start至以外的所有sequence参数.
如果想要改变start值,必须 drop sequence 再 re-create.

命令格式如下:

ALTER  SEQUENCE test_sequence
 INCREMENT  BY   10 
 MAXVALUE  10000 
 CYCLE       --  到10000后从头开始 
 NOCACHE ;

4.  删除 Sequence
 DROP  SEQUENCE order_seq;

更多Oracle相关信息见Oracle 专题页面 ?tid=12

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles