ORACLE创建自动增长列实例

2007-04-16 18:58:24

技术资料 Oracle

--建表

CREATE TABLE table1
 (id NUMBER PRIMARY KEY,
  username VARCHAR2(50));

--建序列号

CREATE SEQUENCE table1_seq
MINVALUE 1
MAXVALUE 9999999999
START WITH 1
INCREMENT BY 1
CACHE 30
ORDER;

--建立触发器

CREATE OR REPLACE TRIGGER table1_tg
BEFORE INSERT ON table1
FOR EACH ROW
BEGIN
  SELECT table1_seq.NEXTVAL INTO :NEW.id FROM DUAL;
END;
/

--执行

INSERT INTO table1 (username) VALUES ('张三');
INSERT INTO table1 (username) VALUES ('李四');

--查询

SELECT * FROM table1;

        ID USERNAME
---------- ------------------------------------------------
         1 张三
         2 李四