不同資料庫的主鍵自增方式

2021-06-20 19:43:04 字數 1106 閱讀 3534

1、mysql的自增變數是比較好記的,使用auto_increment關鍵字,如果知道英文的就容易記憶了,如下建立乙個帶有自增變理的表:

create table test(id int auto_increment

primary key not null,name varchar(50));

注釋:此處的id一定要申明為主鍵,否則會報錯。

2、sql server使用identity關鍵字,可以很容易指定從什麼數開始,增幅是多少,如下:

create table test(id int identity(100,10)

primary key not null,name varchar(50));

3、oracle不能夠在建立表的時候指定自動關鍵字,它需要重新建立sequence,然後以"建立鍵。nextval"來引用:

create table test(id int primary key

not null,name varchar(50));

create sequence test_id(最好是表名+序列號標記)

increment by 1 start with 1 maxvalue 9999;

4、create table strategy

(

strategy_id decimal(17)

generated always as identity (start with 1, increment by 1 )

primary key not null,

strategy_name varchar(200),

area_code decimal(6,0)

);

generated always as identity (start with 1, increment by 1 )

從1開始自增,每次加1.

雙擊**全選

1db2 identity欄位 與 sequence:

SQLITE資料表主鍵設定Id自增方法

搞定了乙個困擾許久的問題,原來sqlite中的主鍵也是可以設定為自增的 方法就是宣告為 integer primary key 的字段可以自動增加。網上查到資料說,從 sqlite 的 2.3.4 版本開始,如果將乙個表中的乙個字段宣告為 integer primary key,那麼只需向該錶的該字...

SQLITE資料表主鍵設定Id自增方法

原文 搞定了乙個困擾許久的問題,原來sqlite中的主鍵也是可以設定為自增的 方法就是宣告為 integer primary key 的字段可以自動增加。網上查到資料說,從 sqlite 的 2.3.4 版本開始,如果將乙個表中的乙個字段宣告為 integer primary key,那麼只需向該錶...

SQLITE資料表主鍵設定Id自增方法

建立資料 設定主鍵自增 建立資料庫時,啟用主鍵自增加特性 create table testtable id integer primary key autoincrement,注意事項 設定主鍵自增時 autoincrement 主鍵型別必須是integer,不能使用int,否則會報錯。插入資料後...