最近接触oracle,想使用sql的自增长列,但oracle不能直接设置,可以通过序列和触发器来实现
--建表
CREATE TABLE TABLE_NAME
(
T_ID VARCHAR2(10) PRIMARY KEY,
USER_ID VARCHAR2(10) NOT NULL,
USER_NAME VARCHAR2(50) NOT NULL
);
--序列
CREATE SEQUENCE SEQ_TABLE_NAME
start with 1 nomaxvalue nocycle
INCREMENT BY 1
--触发器
CREATE OR REPLACE TRIGGER TIG_TABLE_NAME
BEFORE INSERT ON TABLE_NAME
FOR EACH ROW
DECLARE
BEGIN
SELECT SEQ_TABLE_NAME.NEXTVAL INTO :NEW.COLUMNNAME FROM DUAL;
END;
--结果
INSERT INTO TABLE_NAME T (USER_ID,USER_NAME) VALUES(test,'test);
SELECT * FROM TABLE_NAME;