本篇文章是對常用數據庫的自增字段創建方法進行了全面的匯總介紹,需要的朋友參考下
DB2
復制代碼 代碼如下:
CREATE TABLE T1
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE NO CYCLE NO CACHE ORDER ),
...
);
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,
...
);