在Oracle中没有“auto_increment”或“identity”列。但是,您可以使用序列和触发器轻松建模:
表定义:
CREATE TABLE departments (
ID NUMBER(10) NOT NULL,DESCRIPTION VARCHAR2(50) NOT NULL);
ALTER TABLE departments ADD (
CONSTRAINT dept_pk PRIMARY KEY (ID));
CREATE SEQUENCE dept_seq START WITH 1;
触发器定义:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
/
更新:IDENTITY列现在可用于Oracle 12c版本see this:
CREATE TABLE t1 (c1 NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,c2 VARCHAR2(10));
或指定开始和增加值,也阻止任何插入标识列(GENERATED ALWAYS)(再次,仅Oracle 12c)
CREATE TABLE t1 (
c1 NUMBER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),c2 VARCHAR2(10)
);