之前一直用mysql开发网站,最近换掉工作,碰到了oracle,发现了好多问题,oracle主键不支持自增。。
搜了一下才发现要但是通过触发器(trigger)和序列(sequence)可以实现。
一 新建一个测试表
-- Create table
create table SYTEST
(
syid NUMBER(10) not null,
syname VARCHAR2(100) not null,
sydate VARCHAR2(100)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial
64
minextents
1
maxextents
unlimited
);
-- Add comments to the table
comment on table SYTEST
is '测试';
-- Create/Recreate primary, unique and foreign key
constraints
alter table SYTEST
add constraint SYID primary key (SYID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial
64K
minextents
1
maxextents
unlimited
);
二: 创建SEQUENCE
create sequence SYID increment by 1 start with 1 minvalue 1
maxvalue 9999999999999 nocache order;
三:创建一个基于该表的before insert 触发器,在触发器中使用刚创建的SEQUENCE
create or replace trigger SYIDtrigger
before insert on SYTEST
for each row
begin
select SYID.nextval into :new.SYID from dual;
end ;
下面往表里面插入数据测试就ok了