ORACLE设置主键是不会自动增加的,这个和SqlServer是不一样的,在oracle中所以必须用 序列 和 触发器 来完成主键的递增。
1建立数据表
create table t1
(
userid number(10) primary key, /*建立主键*/
username varchar2(20)
);
create table Test_Increase(
userid number(10) primary key, /*建立主键*/
username varchar2(20)
);
create table Test_Increase(
userid number(10) primary key, /*建立主键*/
username varchar2(20)
);
2创建自动增长序列
create sequence t1_sequence
increment by 1 -- 每次加几个
start with 1 -- 从1开始计数
nomaxvalue -- 不设置最大值
nocycle -- 一直累加,不循环
cache 10;
CREATE SEQUENCE TestIncrease_Sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10;
CREATE SEQUENCE TestIncrease_Sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10; 3创建触发器
create trigger t1 before
insert on t1 for each row
begin
select t1_sequence.nextval into:new.userid from dual;
end;
CREATE TRIGGER Test_Increase BEFORE
insert ON Test_Increase FOR EACH ROW
begin
select TestIncrease_Sequence.nextval into:New.userid from dual;
end;
使用方法
insert into t1 (username) values ('卡雷苟斯'); 正确
insert into t1 values ('杰拉德就‘); 错误,没有足够的值
insert into t1 values ('进口量的’); 错误,没有足够的值
1468

被折叠的 条评论
为什么被折叠?



