1.建立测试数据表
CREATE TABLE TEST
(
ID NUMBER,
NAME VARCHAR2(20),
PRIMARY KEY(ID)
);
2.创建序列
CREATE SEQUENCE SEQ_TEST;
3.创建触发器
CREATE OR REPLACE TRIGGER AUTOINCREMENT
BEFORE INSERT ON TEST
FOR EACH ROW
WHEN (NEW.ID IS NULL)
BEGIN
SELECT SEQ_TEST.NEXTVAL INTO :NEW.ID FROM DUAL;
END;
/
4.插入数据
INSERT INTO TEST(NAME) VALUES('NAME1');
5.查看插入结果
SELECT * FROM TEST;
不需要以后在数据库中在加入nextval,自增的设置,这样可以忽略自增,直接插入别的值,