用習慣MySQL或MS SQL之後,就會覺得這兩種資料庫讓欄位數值Auto Increment的設定真的很方便,相較於PostgreSQL就沒這樣的便利。不過麻煩歸麻煩,還是可以透過以下的語法來設定。

首先,在PostgreSQL中開啟SQL執行視窗,建立sequence,在視窗中輸入以下語法並且執行:
create sequence testIncrement
increment 1
minvalue 1
maxvalue 999999999999
start 1
cache 1;
alter table testIncrement owner to postgres;

只要輸入正確是不會有任何錯誤產生的喔!

再來建立需要自動編號的欄位以及table,也是一樣在SQL語法執行視窗輸入以下語法並且執行:
create table products
(
id integer not null default nextval('testIncrement'::regclass),
description text,
constraint products_pkey primary key(id)
)
with(oids=false);
alter table products owner to postgres;

table建立完成之後可以在description輸入文字測試,記得輸入完之後要重新整理,您會發現id欄位已經能自動產生編號了!

 

來源 : http://bluegray-javalearning.blogspot.tw/2010/10/postgresql.html

arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()