Home > Database > Mysql Tutorial > Oracle数据库自增字段的设置

Oracle数据库自增字段的设置

WBOY
Release: 2016-06-07 17:21:22
Original
1263 people have browsed it

新建一个sequence,定义好起始值,增值大小,最大值即可。一般自增用到从1开始自增为1的居多。假定新建的sequence名字为: MY_SE

首先:

新建一个sequence,定义好起始值,,增值大小,最大值即可。一般自增用到从1开始自增为1的居多。

假定新建的sequence名字为: MY_SEQ

其次:

建立触发器

CREATE OR REPLACE TRIGGER MY_TRIG
BEFORE INSERT
ON MY_TABLE
FOR EACH ROW
DECLARE
  NEXTVAL INTEGER;
begin
    select MY_SEQ.NEXTVAL into NEXTVAL from dual;
    :NEW.ID := NEXTVAL;
  end;

说明:直接执行上述sql语句,即可建立触发器

要注意的是,在第二行before insert是插入触发。如果改为before insert or update则当修改数据的时候也会触发自增,这就要看具体的需求了。

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template