Oracle创建自动增长列
在Oracle数据库中创建序列,在使用sql语句向数据库中写入数据的时候,利用序列产生的唯一值,实现表中主键值自增。
序列(Sequence)是一种可以被多个用户使用的用于产生一系列唯一数字的数据库对象。序列定义存储在数据字典中,通过提供唯一数值的顺序表来简化程序设计工作,可以使用序列自动产生主键的键值。当一个序列第一次被查询调用时,它将返回一个预定值。在随后的每次查询中,序列将产生一个按指定的增量增长的值。序列可以循环,或者是连续增加的,直到指定的最大值为止。 创建序列语法如下:create sequence [模式]序列名称[start with 起始数字] [increment by 增量][maxvalue 最大值|nomaxvalue][minvalue 最小值|nominva lue][cycle|nocuyle][cache 数目|nocache][order|noorder]。
通过序列起始数字、最大值、最小值和增量值可以确定序列是增序还是减序,每次增或减的多少。Nocyle选项用于确定在序列达到最大值(增序列)或最小值(减序列)之后不能再产生更多的值,用以防止序列回转。
在Oracle数据库中创建序列,在使用sql语句向数据库中写入数据的时候,利用序列产生的唯一值,实现表中主键值自增。例如:
SQL>createtable tablename
(id number notnull,…);
SQL >createsequence autoID increment by 1 start with 1 maxvalue 999999 cycle;
SQL >insertinto tablename values(autoID.nextval,...);
多个用户可以共用一个序列,但它是针对所有的表,因此产生的序号对一个表而言产生的主键值唯一但是不连续。
使用触发器产生主键值。
在数据表中,有时候需要主键值自动增加,但在Oracle数据库中,没有象Mysql的Autoincrement一样自动增长的数据类型。在实现Oracle数据库字段自增功能时,利用DML触发器来完成。
触发器(trigger)是一些过程,当发生一个特定的数据库事件时就执行这些过程,可以使用触发器扩充引用的完整性。DML即数据操纵语言,用于让用户或程序员使用,实现对数据库中数据的操作。基本的数据操作分成两类四种:检索(查询)和更新(插入、删除、修改)。触发器类似于函数和过程,其在数据库中以独立身分存在。触发事件可以是对数据库表的DML(insert、update或delete)操作等。DML触发器是目前最广泛使用的一种触发器,即由DML语句激发的触发器,并有该语句决定DML触发器的类型。其触发事件包括insert(插入)、update(更新)和delete(删除)。无论哪种触发事件,都能为每种触发事件创建before触发器和after触发器。如可以在表上建立一个before insert 语句,表示在insert事件发生之前采取行动。
创建触发器的语法如下:
create[orreplace]trigger 触发器名称
{before|after|insteadof}激发触发事件
referencing_clause
[WHEN trigger_condition]
[FOR EACH ROW]
referencing_clause用来引用正在处于修改状态下的行中的数据,如果在WHEN子句中指定trigger_condition的话,则首先对该条件求值。触发器主体只有在该条件为真值时才运行。利用触发器与序列相结合,,可以实现在进行DML操作的时候,使表中主键值自动增加。其实现步骤可参照如下例子。
droptable book;
--创建表
createtable book(
bookId varchar2(4) primarykey,
name varchar2(20)
);
--创建序列
createsequence book_seq start with 1 increment by 1;
--创建触发器
createorreplacetrigger book_trigger
before inserton book
for each row
begin
select book_seq.nextval into :new.bookId from dual;
end ;
--添加数据
insertinto book(name) values ('cc');
insertinto book(name) values ('dd');
commit;
demo:
create table t_mid_RoleToOrg(FID VARCHAR2(20) PRIMARY KEY,FRolemappingid varchar(150),FOrgNumber varchar(150));
--创建一个序列
create sequence seq_test START WITH 1 INCREMENT BY 1 CACHE 20;
commit;
--创建触发器
create trigger seq_trigger
before insert on t_mid_RoleToOrg for each row when(new.fid is null)
begin
select seq_test.nextval into :new.fid from dual;
end;
insert into t_mid_RoleToOrg (FRolemappingid,Forgnumber) values('刘璨','刘璨');
SELECT * FROM t_mid_RoleToOrg ;
drop trigger seq_trigger;
drop sequence seq_test;
drop table t_mid_RoleToOrg;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
