示例:
--id自增语法
CREATE SEQUENCE sequence //创建序列名称
[INCREMENT BY n] //递增的序列值是n 如果n是正数就递增,如果是负数就递减 默认是1
[START WITH n] //开始的值,递增默认是minvalue 递减是maxvalue
[{MAXVALUE n | NOMAXVALUE}] //最大值
[{MINVALUE n | NOMINVALUE}] //最小值
[{CYCLE | NOCYCLE}] //循环/不循环
[{CACHE n | NOCACHE}];//分配并存入到内存中
--创建id 自增
create sequence seq_on_test
increment by 1
start with 1
nomaxvalue
nocycle
nocache;
--seq的两个方法 :
select seq_on_test.currval from dual;
select seq_on_test.nextval from dual;
--建个表
create table testuser(
sno integer,
sname varchar2(9) not null,
ssex varchar2(3) not null,
sbirthday date,
sclass varchar2(5),
constraint pk_testuser primary key(sno)
);
comment on column testuser.sno is '学号(主键)';
comment on column testuser.sname is '学生姓名';
comment on column testuser.ssex is '学生性别';
comment on column testuser.sbirthday is '学生出生年月日';
comment on column testuser.sclass is '学生所在班级';
--触发器添加
create or replace trigger testuser_insert_trigger
before insert on testuser
for each row
declare
-- local variable here
begin
select seq_on_test.nextval into :new.sno from dual;
end testuser_insert_trigger;
insert into testuser(sname,ssex,sbirthday,sclass) values('小乔','女',to_date('1977-09-01','yyyy-mm-dd'),95033);
--添加数据
insert into testuser(sno,sname,ssex,sbirthday,sclass) values(seq_on_test.nextval,'貂蝉','女',to_date('1977-09-01','yyyy-mm-dd'),95033);
insert into testuser(sno,sname,ssex,sbirthday,sclass) values(seq_on_test.nextval,'小乔','女',to_date('1977-09-01','yyyy-mm-dd'),95033);
update testuser set sno = '101' where sname = '貂蝉';
select * from testuser;
delete from testuser where sno = '2';