在資料表中,有些欄位是適合且通常需要被設為可自動遞增欄位值的 Ex: 用來做為Primary Key的id欄位
在SQL Server中可藉由修改該欄位的 Is Identity 及 Identity Increment 等屬性來達到此需求
那麼,在PostgreSQL中是怎麼做的呢?
答案可在其官方提供的教學文件中找到,就是
=> 將需要自動遞增的欄位,其Data Type設為serial 或者是 bigserial (視需要儲存的資料大小而定)
在文件當中提到,
「The data types serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases).」
並提供了各種資料型態的對照表,如下圖所示:
(圖片來源: http://www.postgresql.org/docs/9.1/static/datatype-numeric.html)
可以看到若將欄位設為 serial ,可儲存的資料範圍為 1 to 2147483647
若是設為bigserial,可儲存的資料範圍就會擴大到 1 to 9223372036854775807 哦哦哦哦哦!!! (興奮什麼?!)
所以需要使用哪種資料型態請視自己的需要而定囉~
參考資料來源:
1. http://www.postgresql.org/docs/9.1/static/datatype-numeric.html
