Oracle 创建自增列

类似SqlServer中的标识列   identity,Oracle实现同样的效果有点小复杂额,如下:

 1 --1.创建表
 2 create table Student(
 3        ID integer not null primary key,
 4        Name varchar2(40) not null,
 5        Sex integer,
 6        Address varchar2(100)
 7 )
 8 tablespace MyTest_Data;--指明表空间
 9
10 --2.创建序列
11 create sequence SEQ_StuID
12 minvalue 1
13 start with 1
14 increment by 1
15 nomaxvalue
16 nocache;
17
18 --3.创建触发器
19 create OR REPLACE TRIGGER Trigger_StuID
20    BEFORE insert
21    ON Student
22    FOR EACH ROW
23 BEGIN
24      if inserting then
25        if :NEW."ID" is null then
26           select SEQ_StuID.nextval into :NEW."ID" from dual;
27        end if;
28     end if;
29 END;
30
31 --4.激活触发器
32 alter table "admin"."Student" --用户.表名
33 enable all triggers;
34
35 --5.插入数据
36 insert into Student(NAME,SEX,ADDRESS) values(‘张三‘,1,‘第七街区‘);
37
38 --可以使用【序列名.newxtval】查看下一个要使用的序列号
39 --注意:以下语句执行一次会按指定的增量增加
40 select SEQ_StuID.nextval from dual
时间: 2024-08-08 13:53:20

Oracle 创建自增列的相关文章

Oracle12c:支持通过创建identity columen来实现创建自增列

oracle12c之前如果需要创建自增列必须要通过sequence+trigger来实现.但是oracle12c已经可以像mysql,sqlserver一样通过identity column来设置自增列了. 1 Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 2 Connected as My@TEST 3 4 SQL> 5 SQL> create table tab_test2 6 2 ( 7 3

Oracle动态创建时间分区,以及Oracle12c中快速创建自增列

时间分区可以按照年月日时分秒进行分区,一般按照日或月分区就足够了,这里按照的是日分区 demo如下 create table APDEMO ( OID NUMBER(38) generated as identity (start with 1 increment by 1),--主键,自增列 REPORTTIME DATE , AP_MAC VARCHAR2(64 CHAR) , APALIASNAME VARCHAR2(128 CHAR) not null, HOTSPOTNAME VARC

Oracle创建自增字段和修改方法-ORACLE SEQUENCE的简单介绍

http://blog.csdn.net/zhoufoxcn/article/details/1762351先假设有这么一个表: create table S_Depart  (    DepartId             INT                             not null,    DepartName           NVARCHAR2(40)                   not null,    DepartOrder          INT 

orcle创建自增列

刚刚用orcle,组长让我设计一张表,结果设计完了以后就在PLSQL设计主键ID,因为如果不用自增列的话就得用复合主键,所以就想着用ID的自增列来表示.可是他和SQL Server又有点不太一样,他没又自增列.所以在网上查了一下,两种方法. 方法1:触发器 首先先创建一张测试表t_demo create table t_demo ( id number(20) primary key, username varchar2(20) ) 第一步:创建squence create sequence d

Oracle实现自增列

首先创建一个序列(sequence) ,实现自增 -- Create sequence  create sequence t_user_seq minvalue 1 -- 最小值:1 nomaxvalue -- 没有最大值 start with 1 -- 从1开始 increment by 1 --每次递增1 nocache --不使用缓存 (也可设置为 cache n, 区别见本文最后附注部分) order; 创建触发器,在触发器中使用之前创建好的sequence :t_user_seq --

Oracle创建自增ID

先创建序列sequence create sequence S_User minvalue 1 nomaxvalue  -- 或 maxvalue 999 start with 1 increment by 1 cache 20; -- 或 nocache 参考: oracle 序列中cache 有什么用途 然后创建触发器 create or replace trigger trg_user before insert on T_USER for each row begin select it

Oracle创建自增字段方法-ORACLE SEQUENCE的简介

曾经最头疼的就是对表插入数据的时候,有主键问题. 由于主键不可以反复,所以得用函数自己定义一个规则生成不反复的值赋值给主键. 如今发现oracle有sequence就不用那么麻烦了. 转自:http://zhoufoxcn.blog.51cto.com/792419/166418/ 先如果有这么一个表: create table S_Depart    ( DepartId                         INT                                  

Oracle创建自增字段方法-ORACLE SEQUENCE的简单介绍

引用自 :http://www.2cto.com/database/201307/224836.html 先假设有这么一个表: create table S_Depart ( DepartId INT not null, DepartName NVARCHAR2( 40) not null, DepartOrder INT default 0, constraint PK_S_DEPART primary key (DepartId) ); 在oracle中sequence就是所谓的序列号,每次

oracle创建自增ID 表以及触发器的使用

1.创建一个普通的用户表 create table demo ( id int not null, name varchar(10), pwd varchar(10) default '123', primary key(id) ) 2.创建一个序列Sequence create sequence seq_demo increment by 1 --自增 1 start with 1 --开始值 1 nomaxvalue --无最大值 minvalue 1 --最小值 1 nocycle; --