问题描述:
如题:我用的是plsqldev客户端工具,见表语言如下
create table CEMP
(
EMPID NUMBER(2, 0) not null ,
EMPNAME VARCHAR2(10) null ,
DEPID VARCHAR2(20) null ,
constraint PK_CEMP primary key (EMPID)
);
创建的表有三个字段,第一个字段EMPID为主键,现在我想将这个字段变主键,并默认加1的,就是在insert inot 的时候 insert into 后两个字段就行了。
问题解答:
oracle中没有自增字段,可通过序列+触发器间接实现,cmd中sqlplus登录,直接运行即可。
CREATE SEQUENCE EMPID_sequence
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE
order;
create or replace trigger tri_insert_CEMP
before insert on CEMP
for each row
begin
select EMPID_sequence.nextval into :new.EMPID from dual;
end;
/
commit;
oracle有rownum
oracle 不是sql,就算建了序列,也要手动写的。