[color=red]1.建立测试数据表[/color]
CREATE TABLE TEST
(
ID NUMBER,
NAME VARCHAR2(20),
PRIMARY KEY(ID)
);
[color=red]2.创建序列[/color]
CREATE SEQUENCE SEQ_TEST;
[color=red]3.创建触发器[/color]
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;
/
[color=red]4.插入数据[/color]
INSERT INTO TEST(NAME) VALUES('NAME1');
[color=red]5.查看插入结果[/color]
SELECT * FROM TEST;
CREATE TABLE TEST
(
ID NUMBER,
NAME VARCHAR2(20),
PRIMARY KEY(ID)
);
[color=red]2.创建序列[/color]
CREATE SEQUENCE SEQ_TEST;
[color=red]3.创建触发器[/color]
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;
/
[color=red]4.插入数据[/color]
INSERT INTO TEST(NAME) VALUES('NAME1');
[color=red]5.查看插入结果[/color]
SELECT * FROM TEST;