Oracle中如何创建使用SEQUENCES
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

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



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

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]

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

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

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.

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

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.

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