首页 技术 正文
技术 2022年11月21日
0 收藏 582 点赞 2,990 浏览 2523 个字

oracle 主键自动增长

2009-12-11 16:07:00|  分类: 数据库资料|字号 订阅

  

这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下:

create table simon_example

(

id number(4) not null primary key,

name varchar2(25)

)

— 建立序列:

— Create sequence

create sequence SIMON_SEQUENCE

minvalue 1

maxvalue 999999999999999999999999999

start with 1

increment by 1

cache 20;

— 建立触发器

create trigger “simon_trigger” before

insert on simon_example for each row when(new.id is null)

begin

select simon_sequence.nextval into:new.id from dual;

end;

————————————————————————-

————————————————————————-

————————————————————————-

1、把主键定义为自动增长标识符类型

在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如:

create table customers(id int auto_increment primary key not null, name varchar(15));

insert into customers(name) values(“name1”),(“name2”);

select id from customers;

以上sql语句先创建了customers表,然后插入两条记录,在插入时仅仅设定了name字段的值。最后查询表中id字段,查询结果为:

id

1

2

由此可见,一旦把id设为auto_increment类型,mysql数据库会自动按递增的方式为主键赋值。

在MS SQLServer中,如果把表的主键设为identity类型,数据库就会自动为主键赋值。例如:

create table customers(id int identity(1,1) primary key not null, name varchar(15));

insert into customers(name) values(“name1”),(“name2”);

select id from customers;

查询结果和mysql的一样。由此可见,一旦把id设为identity类型,MS SQLServer数据库会自动按递增的方式为主键赋值。identity包含两个参数,第一个参数表示起始值,第二个参数表示增量。

2、从序列中获取自动增长的标识符

在Oracle中,可以为每张表的主键创建一个单独的序列,然后从这个序列中获取自动增加的标识符,把它赋值给主键。例如一下语句创建了一个名为customer_id_seq的序列,这个序列的起始值为1,增量为2。

create sequence customer_id_seq increment by 2 start with 1

一旦定义了customer_id_seq序列,就可以访问序列的curval和nextval属性。

curval:返回序列的当前值

nextval:先增加序列的值,然后返回序列值

以下sql语句先创建了customers表,然后插入两条记录,在插入时设定了id和name字段的值,其中id字段的值来自于customer_id_seq序列。最后查询customers表中的id字段。

create table customers(id int primary key not null, name varchar(15));

insert into customers values(customer_id_seq.curval, “name1”),(customer_id_seq.nextval, “name2”);

select id from customers;

如果在oracle中执行以上语句,查询结果为:

id

1

3

—————————————————————-

—————————————————————-

—————————————————————-

学oracle不久,在建表时发现这样一个问题,比如我现在创建一个表:student

create table STUDENT
(
ID NUMBER not null,
NAME VARCHAR2(20) default ‘男’,
SEX VARCHAR2(4),
ADDRESS VARCHAR2(40),
MEMO VARCHAR2(60)
)

现在我想实现每插入一条数据,就让id自动增长1.在SQLSERVER中这个很好实现,但在oracle中我搞了半天,查了下资料发现要用到“序列(sequence)”,“触发器”的知识。

首先,创建一个序列:

create sequence STU
minvalue 1
maxvalue 999999999999
start with 21
increment by 1
cache 20;

然后,给表student创建一个触发器:

create or replace trigger stu_tr
before insert on student 
for each row
declare
— local variables here
begin
select stu.nextval into :new.id from dual;
end stu_tr;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844