新聞中心
PostgreSQL 數(shù)據(jù)類型介紹(五)OID的理解
那oid在哪兒?到底為什么會出現(xiàn)這種情況 ?
黃驊網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)。
來看看postgres官網(wǎng)對 oid的介紹:
根據(jù)stackoverflow的高票用戶的回答:
*OIDs basically give you a built-in, globally unique id for every row, contained in a system column (as opposed to a user-space column). That's handy for tables where you don't have a primary key, have duplicate rows, etc. For example, if you have a table with two identical rows, and you want to delete the oldest of the two, you could do that using the oid column.
In my experience, the feature is generally unused in most postgres-backed applications (probably in part because they're non-standard), and their use is essentially deprecated :
In PostgreSQL 8.1 default_with_oids is off by default; in prior versions of PostgreSQL, it was on by default.
The use of OIDs in user tables is considered deprecated, so most installations should leave this variable disabled. Applications that require OIDs for a particular table should specify WITH OIDS when creating the table. This variable can be enabled for compatibility with old applications that do not follow this behavior.
大意是你要是有個表沒有用主鍵,這時候可以把oid充當為主鍵使用,當然這是沒辦法的辦法。
總結(jié): oid是給內(nèi)部表做標識用的,不推薦使用。 建議將 default_with_oids 設置為off。 建表的時候,如果想使用主鍵,請自行建立。oid本身大小固定的,萬一 行數(shù)超過了oid 的最大限制數(shù)(4 byte int),那就無法插入新行了。
PostgreSQL設置主鍵id自增長
PostgreSQL中讓主鍵自增長可先建立一個對應表的sequence
CREATE SEQUENCE test_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
其中START是從數(shù)字幾開始,INCREMENT BY是一次增長幾個數(shù)字,NO MINVALUE是沒有最小值,NO MAXVALUE是沒有最大值;
然后修改表id字段
alter table test alter column id set default nextval('test_id_seq');
也可以在建表的時候使用:
"id" int4 DEFAULT nextval('t_ordercenter_info_history_id_seq'::regclass) NOT NULL
Postgresql如何引用具有多個唯一主鍵的表
主鍵是表中的一個或多個字段,它的值用于惟一地標識表中的某一條記錄.;使用索引可快速訪問數(shù)據(jù)庫表中的特定信息。
索引是對數(shù)據(jù)庫表中一列或多列的值進行排序的一種結(jié)構(gòu),只有當經(jīng)常查詢索引列中的數(shù)據(jù)時,才需要在表上創(chuàng)建索引。索引占用磁盤空間,并且降低添加、刪除和更新行的速度。當然索引也有好處就是查詢速度快,它利還是大于弊的所以請慎重使用索引。
比如:一個學生表(t_stu )有1000條數(shù)據(jù),給它id列建個主鍵和索引,想查詢id=1000;的這條信息,如果沒有索引,它就一條一條的比對查找,系統(tǒng)運行1000次才找到,要是創(chuàng)建了索引,查詢id=1000的這條信息,系統(tǒng)只運行一次就找到了。
postgresql如何讓主鍵自增
PostgreSQL不像SQL Server一樣有identity關(guān)鍵字可以用來指定字段自增。但是它有序列值產(chǎn)生器可以使用,并且在數(shù)據(jù)字段定義時可以指定默認值為一個表達式,這樣我們就可以使用序列值來作實現(xiàn)字段值自增。
步驟:
創(chuàng)建一個序列值
create sequence id_sequence increment?1 start 1;
id_sequence為序列值名稱,increment為步長默認為1,start為初始值,默認為1
在字段定義時,指定默認值為獲取序列值(用nextval函數(shù), 參數(shù)為序列值名稱的字符串)
create?table?mytest?(id?integer?not?null?default?nextval('id_sequence'),?txt?varchar(100));
PostgreSQL中使用UUID
UUID(Universal Unique Identifier)或者 GUID(Globally Unique Identifier)是一個 128 比特的數(shù)字,可以用于唯一標識每個網(wǎng)絡對象或資源。由于它的生成機制,一個 UUID 可以保證幾乎不會與其他 UUID 重復,因此常常用于生成數(shù)據(jù)庫中的主鍵值。
1.pgcrypto 模塊提供的 uuid
PostgreSQL 提供了一個用于加/解密的擴展模塊 pgcrypto,其中的 gen_random_uuid() 函數(shù)可以用于返回一個 version 4 的隨機 UUID。
2.uuid-ossp 模塊提供的 uuid
uuid-ossp模塊提供函數(shù)使用幾種標準算法之一產(chǎn)生通用唯一標識符(UUID)。還提供產(chǎn)生某些特殊 UUID 常量的函數(shù)。
1.將當前目錄轉(zhuǎn)移到 PostgreSQL 源代碼目錄下的 contrib;如:
2.執(zhí)行如下命令來安裝擴展模塊
如果要安裝 uuid-ossp 模塊,需要在執(zhí)行安裝擴展模塊之前,執(zhí)行 configure 并添加 --with-uuid=xxx,xxx取值為:
然后再執(zhí)行安裝擴展模塊的命令。
3.檢查是否安裝,在 PostgreSQL 的安裝目錄下的 /share/extension 目錄下,查看是否有模塊相關(guān)的文件。如:
注: gen_random_uuid() 從 PostgreSQL 13 開始成為了一個內(nèi)置函數(shù)
如果您所使用的PostgreSQL版本在13以上,則不需要執(zhí)行如下語句:
生成uuid:
如果想要生成沒有中劃線(-)的 UUID 字符串,可以使用 REPLACE 函數(shù):
查看包含的函數(shù):
執(zhí)行如下命令生成 uuid:
網(wǎng)頁題目:postgresql主鍵的簡單介紹
本文地址:http://www.ef60e0e.cn/article/dsdhjpe.html