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;

oracle 主键自动增长的更多相关文章

  1. Mysql,SqlServer,Oracle主键自动增长的设置

    1.把主键定义为自动增长标识符类型 MySql 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: )); insert into customers ...

  2. Mysql,SqlServer,Oracle主键自动增长的设置

    在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: CREATE TABLE google(id INT AUTO_INCREMENT PRIMARY ...

  3. PowerDesigner 15设置mysql主键自动增长及基数

    PowerDesigner 15设置mysql主键自动增长及基数 1.双击标示图,打开table properties->columns,  如图点击图标Customize Columns an ...

  4. oracle添加数据时主键自动增长

    CREATE TABLE STUDENT( --创建学生表  ID NUMBER(10) PRIMARY KEY,   --主键ID  SNAME VARCHAR2(20), ); 此时给学生表添加数 ...

  5. MySQL 和 Oracle 主键自增长

    1.MySQL 1)建表 auto_increment:每插入一条数据,客户表(customers)的主键id就自动增1,如下所示 create table customers -- 创建客户表 ( ...

  6. oracle主键自增长

    这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create table simon_example ( id number(4) not null primary key, nam ...

  7. mysql——主键自动增长&唯一索引

    首先说一下主键和唯一索引的区别 主键:一个数据库的一张表有且仅有一个主键,而且主键不能重复 唯一索引:一个数据库的一张表上唯一索引可以有多个,只是所在唯一索引上的值不能重复,这一点和主键一样 下面我们 ...

  8. PostgreSQL 主键自动增长

    建立主键并设置自动增加的办法好好几种,这里记录我测试过的: drop table pro_process; CREATE TABLE "public"."pro_proc ...

  9. 设置oracle主键自增长

    创建test表后,创建序列: CREATE sequence seq_test INCREMENT BY 1 START WITH 1 minvalue 1   成功后,插入一条语句进行测试:   I ...

随机推荐

  1. 转载-SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart

    [原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart 摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有 ...

  2. Box of Bricks

    Box of Bricks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  3. python2.6.6在centos6.4下安装

    1.wget http://www.python.org/ftp/python/2.6.6/Python-2.6.6.tar.bz2 2. tar xvjf Python-2.6.6.tar.bz2 ...

  4. 自己写的select元素可编辑、可筛选JQuery插件 jquery.inputselectbox.js

    /* 功能:实现对select下拉框可输入的功能, 输入时会对下拉框的内容进行动态过滤. 参数:没有选择任何值时默认显示的文字 如何使用:$("#firstLevel").inpu ...

  5. qmake pro工程设置

    ----我的生活,我的点点滴滴!! #以下是在terminal里面编译,想编译哪个就修改pro文件 #================================================= ...

  6. psy

    本文的重点是讲解如何运用心理线指标看盘,运用周线月线的心理线来抓住大盘的顶部和底部的研究.分析研究的材料都来源于沪市历史上的顶部和底部的历史数据.从psy数据所得出的结论大多数是有效的,只有个别时期的 ...

  7. libevent在windows下使用步骤详解

    一 环境 官方下载地址:http://libevent.org/版本:libevent-2.0.22-stable 二 编译静态库 1 解压把上面下载到libevent-2.0.22-stable.t ...

  8. ASP.NET中使用Server.Transfer()方法在页间传值 实例

    以下代码在VS2008中测试通过 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  9. C/C++ - 结构体实际申请的空间

    C/C++ - 结构体实际申请的空间 如下的结构体,sizeof()大小,实际申请的空间以及理论上申请最佳空间 struct Spot { int x; int y; bool visible; in ...

  10. Simple But Useful Samples About 'grep' Command(简单实用的grep 命令)

    Do the following: grep -rnw '/path/to/somewhere/' -e "pattern" -r or -R is recursive, -n i ...