各數據庫(MySQL、PostgreSQL、Oracle、MsSQL)有關自增字段的設置

在數據庫設計過程當中,咱們一般會把數據庫表中的ID字段設置成自增。下面以經常使用的 數據字典表爲例,說明如何在各數據庫下設置自增字段。

MySQL

MySQL數據庫只須要在目標字段上添加AUTO_INCREMENT,而且爲表設置AUTO_INCREMENT=x
x:自增開始的數字。git

參考示例:github

CREATE TABLE `dictionary` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `parent_id` int(10) unsigned NOT NULL COMMENT '父ID',
  `type` varchar(50) NOT NULL COMMENT '元數據類型',
  `item_name` varchar(100) NOT NULL COMMENT '元數據項顯示名',
  `item_value` varchar(100) DEFAULT NULL COMMENT '元數據項存儲值',
  `comment` varchar(200) DEFAULT NULL COMMENT '備註',
  `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '刪除標記',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='數據字典';

PostgreSQL

PostgreSQL數據庫有多種方法可實現自增字段的設置,經常使用的有:數據庫

  1. SERIAL,最簡單
  2. IDENTITY,是PostgreSQL 10的新增特性
  3. 建立SEQUENCE,更靈活

參考示例:SERIAL框架

create table dictionary (
   id                   SERIAL not null,
   parent_id            INT4                 not null,
   type                 VARCHAR(50)          not null,
   item_name            VARCHAR(100)         not null,
   item_value           VARCHAR(100)         null,
   comment              VARCHAR(200)         null,
   deleted              INT2                 not null default 0,
   create_time          DATE                 not null default CURRENT_TIMESTAMP,
   constraint PK_dictionary primary key (id)
);

Oracle

Oracle數據庫經常使用的設置自增字段的兩種方法:數據庫設計

  1. 建立SEQUENCE
  2. IDENTITY,要求Oracle數據庫版本是12c或以上

12c版本示例:ide

create table "dictionary" (
   "id"                 INTEGER  generated as identity ( start with 1 nocycle noorder)  not null,
   "parent_id"          INTEGER               not null,
   "type"               VARCHAR2(50)          not null,
   "item_name"          VARCHAR2(100)         not null,
   "item_value"         VARCHAR2(100),
   "comment"            VARCHAR2(200),
   "deleted"            SMALLINT             default 0  not null,
   "create_time"        TIMESTAMP            default CURRENT_TIMESTAMP  not null,
   constraint "PK_dictionary" primary key ("id")
);

11g版本示例:設計

-- 建表
create table "dictionary" (
   "id"                 INTEGER               not null,
   "parent_id"          INTEGER               not null,
   "type"               VARCHAR2(50)          not null,
   "item_name"          VARCHAR2(100)         not null,
   "item_value"         VARCHAR2(100),
   "comment"            VARCHAR2(200),
   "deleted"            SMALLINT             default 0  not null,
   "create_time"        TIMESTAMP            default CURRENT_TIMESTAMP  not null,
   constraint "PK_dictionary" primary key ("id")
);
-- 建立序列
CREATE SEQUENCE DICTIONARY_ID_SEQ
INCREMENT BY 1
START WITH 1;
-- 建立觸發器,非必須
CREATE OR REPLACE TRIGGER DICTIONARY_ID_SEQ_TRG
BEFORE INSERT ON "dictionary"
FOR EACH ROW
WHEN (NEW."id" IS NULL)
BEGIN
  SELECT DICTIONARY_ID_SEQ.NEXTVAL
  INTO :NEW."id"
  FROM DUAL;
END;

MsSQL

MsSQL即SQL Server數據庫,使用IDENTITY便可。code

參考示例:開發

create table dictionary (
   id                   int                  identity,
   parent_id            int                  not null,
   type                 varchar(50)          not null,
   item_name            varchar(100)         not null,
   item_value           varchar(100)         null,
   comment              varchar(200)         null,
   deleted              smallint             not null default 0,
   create_time          datetime             not null default CURRENT_TIMESTAMP,
   constraint PK_dictionary primary key (id)
);

diboot 簡單高效的輕代碼開發框架 (求star)get

相關文章
相關標籤/搜索