Oracle表自动增长列之序列创建


  1.建立测试数据表

CREATE TABLE TEST
(
ID NUMBER,
NAME VARCHAR2(20),
PRIMARY KEY(ID)
);
2.创建序列
CREATE SEQUENCE SEQ_TEST

minvalue 1

maxvalue 99999999

start with 1

increment by 1

nocache order;

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;

你可能感兴趣的:(Oracle表自动增长列之序列创建)