Oracle:序列

本文介绍如何在数据库中创建、使用及管理序列。包括创建序列的方法、通过序列生成整数序列、查询序列信息、使用序列填充主键及指定默认值等实用技巧。

序列

是一种数据库项,可以生成整数序列。

1.创建序列
create sequence s_test
start with 10 increment by 5
minvalue 10 maxvalue 20
cycle cache 2 order;
2.获取有关序列的信息
select
sequence_name, min_value, max_value, increment_by, cycle_flag,order_flag, cache_size, last_number
from user_sequences
order by sequence_name;
3.使用序列
select s_test.nextval
from dual;

NEXTVAL
--------
1


select s_test.currval
from dual;

CURRVAL
--------
1
4.使用序列填充主键
create table order (
id integer constraint order_pk primary key,
status varchar2(10);
last_modified DATE default sysdate
);

create sequence s_order nocache;

insert into order(id, status, last_modified)
values(s_order.nextval, 'placed', '2017-11-09');
5.使用序列指定默认列值
create sequence s_default_value_for_column;

create table test_with_sequence(
id integer constraint test_with_pk primary key,
sequence_value integer default s_default_value_for_column.nextval
)
6.使用标识列

oracle database 12c的新功能

create table test_with_identity(    id integer constraint test_with__identity_pk primary key,   identity_value integer generated by default as identity(    start with 5 increment by 2 ) )
7.修改序列
alter sequence s_test increment by 2;
8.删除序列
drop sequence s_test;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值