Home > Database > Mysql Tutorial > body text

oracle如何实现插如记录时ID自增

WBOY
Release: 2016-06-07 15:19:06
Original
1212 people have browsed it

sql server中的ID是系统设定的,当我们插入数据时他会自己增加,但oracle没有,需要我们自己添加字段ID,由于ID设置成NOT NULL,插入数据库时就会失败,这样就需要插入数据库时实现这条记录ID自己自动增加,自己搜了一下,具体方法如下 假如将表li_line_fence

sql server中的ID是系统设定的,当我们插入数据时他会自己增加,但oracle没有,需要我们自己添加字段ID,由于ID设置成NOT NULL,插入数据库时就会失败,这样就需要插入数据库时实现这条记录ID自己自动增加,自己搜了一下,具体方法如下

假如将表li_line_fence的fenceid字段设置成了ID,那么就需要插入记录时自增

1.首先建立一个序列:

create  sequence seq_atable minvalue 1 maxvalue 9999999 start with 1 increment by 1 nocache
Copy after login
2.建立一个触发器:
create or replace trigger trg_atable before insert on li_line_fence for each row
begin
   select seq_atable.nextval into :new.fenceid from dual;
end;
Copy after login
这个触发器是对inset动作的触发,into后面的new.fenceid是向新记录的fenceid字段自动按照序列中定义的插入值,这样就自动的插入值了

oracle如何实现插如记录时ID自增
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