Oracle通过序列+触发器实现主键自增
接触oracle没多久,在建表的时候发现还不会如何设置主键自动增长。和mysql的设置为AUTO_INCREMENT属性相比,要复杂很多,所以现在记录起来。
我使用的是序列+触发器的方式。
现在已经创建好一个tbl_dept表,比较简单就两个字段。建表语句如下:
-- Create table
create table TBL_DEPT
(
dept_id INTEGER not null,
dept_name VARCHAR2(255) not null
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 128K
next 1M
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table TBL_DEPT
add constraint PK_DEPT primary key (DEPT_ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
表有了之后我们首先需要创建一个用来实现自动增长的序列:
create sequence sql_dept
minvalue 1 --最小值
nomaxvalue --不设置最大值
start with 1 --从1开始计数
increment by 1 --每次加1个
nocycle --一直累加,不循环
nocache; --不建缓冲区
最后我们只需要再把触发器设置好就行了:
create or replace trigger tg_dept
before insert on tbl_dept --tbl_dept:表名
for each row
declare
nextid number;
begin
IF :new.dept_id IS NULL or :new.dept_id=0 THEN --dept_id:列名
select sql_dept.nextval --sql_dept:序列
into nextid
from sys.dual;
:new.dept_id:=nextid;
end if;
end tg_dept;
插入前:

我们现在通过插入语句来测试一下吧,我们插入的语句中主键dept_id为空:
insert into tbl_dept(dept_name) values('人事部');
结果:

Oracle通过序列+触发器实现主键自增的更多相关文章
- Oracle - 使用序列+触发器实现主键自增长
Oracle中的自增,不如Sql server那般方便. --.创建序列 CREATE SEQUENCE "TABLE_NAME"."SQ_NAME" MINV ...
- Oracle创建触发器实现主键自增
CREATE OR REPLACE TRIGGER "trigger_empl" before insert on extjsTest1.t_empl for each row b ...
- oracle数据库创建表且主键自增
唠叨几句:几年前的知识忘却了,整理一下笔记,提供一下方便 1.创建数据库表 设置主键 create table users( userid number(10) primary key, /*主键,自 ...
- Oracle创建表(包含、主键自增)
注意:Oracle导出建表语句不会导出触发器及自增索引 第一步:创建一张表 create table member( memberId number primary key, --主键.自增长 mem ...
- oracle建表 和 设置主键自增
1.新建table CREATE TABLE ysb_log( id ) primary key not null , tbdate ) NULL, tb_time ) NOT NULL, tblog ...
- Oracle 创建表并设置主键自增
创建数据库 CREATE TABLE STUDENT(ID NUMBER PRIMARY KEY, NAME VARCHAR(200) NOT NULL, SEX VARCHAR(200), CREA ...
- oracle 触发器实现主键自增
drop table book; --创建表 create table book( bookId varchar2() primary key, name varchar2() ); --创建序列 c ...
- oracle利用触发器实现主键字段自增
我们都知道oracle主键自增利用的是序列sequence.我们先创建一个sequence: create sequence test_sequence start increment maxvalu ...
- oracle主键自增
oracle主键自增 1建立数据表 create table Test_Increase( userid number(10) primary key, /*主键,自动增加*/ ...
随机推荐
- Python 内置os模块的简单实用
获取路径&目录添加文件 在自动化测试的过程,考虑到工程文件的移动或者在其他人的工作环境中运行,所以我们的路径要灵活,不能把路径写死. 推荐使用Python的内置模块OS 参照图 import ...
- mysql 多个字段合并
group_concat 函数默认“,”合并 select p.id as patent_id,p.application_no,p.name,p.inventer,group_concat(pi.` ...
- tensorflow实战讨论
欢迎关注微信公众号:樱园的玻尔兹曼机
- A bean with that name has already been defined in DataSourceConfiguration$Hikari.class
A bean with that name has already been defined in DataSourceConfiguration$Hikari.class 构建springcloud ...
- linux查看文件夹大小du命令
查看1级(--max-depth=1)目录的大小,并排序 参考 -h或–human-readable 以K,M,G为单位,提高信息的可读性. –max-depth= 超过指定层数的目录后,予以忽略 d ...
- 【Django】重定向
view函数中使用重定向方法 return HttpResponseRedirect('redir2.html')的时候不自觉的在前面加了request参数,结果报错: TypeError at /b ...
- python调用webservice接口
使用suds这个第三方模块 from suds.client import Clienturl = 'http://ip:port/?wsdl'cilent=Client(url)print cile ...
- HDU-1160.FatMouse'sSpeed.(LIS变形 + 路径打印)
本题大意:给定一定数量的数对,每个数保存着一只老鼠的质量和速度,让你求出一个最长序列,这个序列按照质量严格递增,速度严格递减排列,让你输出这个序列的最长长度,并且输出组成这个最长长度的序列的对应的老鼠 ...
- 41 【docker】初识
常用的docker命令: docker ps #查看当前正在运行的容器 docker ps -a | grep <keyword> #查看所有的容器,运行的或者停止的 docker sto ...
- [leetcode]11. Container With Most Water存水最多的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...