飞奔的炮台 发表于 2022-1-28 11:29:04

浅析常用数据库的自增字段创建方法汇总

DB2

CREATE   TABLET1
(
   idINTEGER   NOT   NULLGENERATED ALWAYSAS   IDENTITY(STARTWITH   1INCREMENTBY   1MINVALUE1NO MAXVALUE NO CYCLE NO CACHEORDER ),
   ...
);

Oracle(需要创建一个SEQUENCE和一个TRIGGER):

CREATE TABLE T1
(
   id NUMBER(10,0) NOT NULL,
   ...
);
CREATE SEQUENCE T1_ID_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE CACHE 100 ORDER;
CREATE OR REPLACE TRIGGER INSERT_T1_ID
BEFORE INSERT ON T1
REFERENCING NEW AS new OLD AS old
FOR EACH ROW
BEGIN
    SELECT T1_ID_SEQ.NEXTVAL INTO :new.id FROM DUAL;
END;

MySQL

CREATE TABLE T1
(
   id INT NOT NULL AUTO_INCREMENT,
   ...
);

PostgreSQL

CREATE TABLE T1
(
   id SERIAL NOT NULL,
   ...
);

SQL Server

CREATE TABLE T1
(
   id INT NOT NULL IDENTITY,
   ...
);

Sybase

CREATE TABLE T1
(
   id INT NOT NULL IDENTITY,
   ...
);

https://www.uoften.com/dbs/oracle/20180415/72935.html
页: [1]
查看完整版本: 浅析常用数据库的自增字段创建方法汇总