1、创建表
--创建表
create table USERS
(
id NUMBER not null constraint PK_USERS_ID primary key,
username VARCHAR2(40),
userpass VARCHAR2(40),
userlike VARCHAR2(1000)
);
2、创建序列
create sequence SEQ_USERS
--最小值为1
minvalue 1
--不设置最大值
nomaxvalue
--从1开始计数
start with 1
--每次加1个
increment by 1
--直累加,不循环
nocycle
--不建缓冲区
nocache;
3、创建触发器
--创建触发器
--只有在id为空时,启动该触发器生成id号
CREATE OR REPLACE TRIGGER tri_users
BEFORE INSERT
ON users
FOR EACH ROW
WHEN (new.id is null)
BEGIN
select seq_users.nextval into :new.id from dual;
END tri_users;
或:
CREATE OR REPLACE TRIGGER tri_users
BEFORE INSERT
ON users
FOR EACH ROW
BEGIN
IF :new.id is null THEN
select seq_users.nextval into :new.id from dual;
END IF;
END tri_users;
4、插入数据
insert into users(username,userpass,userlike) values('test11','test','test');
select * from users;