99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

怎么在PostgreSQL中查看表的主外鍵-創新互聯

這期內容當中小編將會給大家帶來有關怎么在PostgreSQL中查看表的主外鍵,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

成都創新互聯憑借專業的設計團隊扎實的技術支持、優質高效的服務意識和豐厚的資源優勢,提供專業的網站策劃、成都網站設計、做網站、網站優化、軟件開發、網站改版等服務,在成都十多年的網站建設設計經驗,為成都上千多家中小型企業策劃設計了網站。
SELECT
   tc.constraint_name, tc.table_name, kcu.column_name, 
   ccu.table_name AS foreign_table_name,
   ccu.column_name AS foreign_column_name,
   tc.is_deferrable,tc.initially_deferred
 FROM 
   information_schema.table_constraints AS tc 
   JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
   JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
 WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = 'your table name';

constraint_type有四種:

UNIQUE、PRIMARY KEY、CHECK、FOREIGN KEY

通過修改上邊sql語句的table_name和constraint_type來進行相應的查詢

補充:PostgreSQL查詢約束和創建刪除約束

查詢約束constraint

SELECT
   tc.constraint_name, tc.table_name, kcu.column_name, 
   ccu.table_name AS foreign_table_name,
   ccu.column_name AS foreign_column_name,
   tc.is_deferrable,tc.initially_deferred
 FROM 
   information_schema.table_constraints AS tc 
   JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
   JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
 WHERE constraint_type = 'UNIQUE' AND tc.table_name = 'table_name';

constraint_type有四種:

UNIQUE、PRIMARY KEY、CHECK、FOREIGN KEY, 通過修改上邊sql語句的table_name和constraint_type來進行相應的查詢。

添加約束

ALTER TABLE table_name ADD CONSTRAINT uk_users_name1 UNIQUE (NAME);

刪除約束

alter table table_name drop constraint if EXISTS uk_users_name1;

補充:PostgreSQL的依賴約束(系統表pg_depend和pg_constraint)詳解

pg_depend是postgres的一張系統表,用來記錄數據庫對象之間的依賴關系,除了常見的主外鍵,還有其他一些內部依賴關系,可以通過這個系統表呈現出來。

一、表結構:

postgres=# \d+ pg_depend
            Table "pg_catalog.pg_depend"
  Column  | Type  | Modifiers | Storage | Stats target | Description
-------------+---------+-----------+---------+--------------+-------------
 classid   | oid   | not null | plain  |       | 系統OID
 objid    | oid   | not null | plain  |       | 對象OID
 objsubid  | integer | not null | plain  |       |
 refclassid | oid   | not null | plain  |       | 引用系統OID
 refobjid  | oid   | not null | plain  |       | 引用對象ID
 refobjsubid | integer | not null | plain  |       |
 deptype   | "char" | not null | plain  |       | pg_depend類型
Indexes:
  "pg_depend_depender_index" btree (classid, objid, objsubid)
  "pg_depend_reference_index" btree (refclassid, refobjid, refobjsubid)
Has OIDs: no

--BTW:OID是Object Identifier的縮寫,是對象ID的意思,因為是無符號的4字節類型,不夠足夠大,所以一般不用來做主鍵使用,僅系統內部,比如系統表等應用,可以與一些整型數字進行轉換。與之相關的系統參數是default_with_oids,默認是off

postgres=# \d pg_constraint
   Table "pg_catalog.pg_constraint"
  Column   |   Type   | Modifiers 
---------------+--------------+-----------
 conname    | name     | not null    -- 約束名
 connamespace | oid     | not null    -- 約束所在命名空間的OID
 contype    | "char"    | not null    -- 約束類型
 condeferrable | boolean   | not null    -- 約束是否可以推遲
 condeferred  | boolean   | not null    -- 缺省情況下,約束是否可以推遲
 convalidated | boolean   | not null    -- 約束是否經過驗證
 conrelid   | oid     | not null    -- 約束所在的表的OID
 contypid   | oid     | not null    -- 約束所在的域的OID
 conindid   | oid     | not null    -- 如果是、主鍵、外鍵或排除約束,則為支持這個約束的索引;否則為0
 confrelid   | oid     | not null    -- 如果是外鍵,則為參考的表;否則為 0
 confupdtype  | "char"    | not null    -- 外鍵更新操作代碼
 confdeltype  | "char"    | not null    -- 外鍵刪除操作代碼
 confmatchtype | "char"    | not null    -- 外鍵匹配類型
 conislocal  | boolean   | not null    
 coninhcount  | integer   | not null    -- 約束直接繼承祖先的數量
 connoinherit | boolean   | not null    
 conkey    | smallint[]  |     -- 如果是表約束(包含外鍵,但是不包含約束觸發器),則是約束字段的列表
 confkey    | smallint[]  |     -- 如果是一個外鍵,是參考的字段的列表
 conpfeqop   | oid[]    |     -- 如果是一個外鍵,是PK = FK比較的相等操作符的列表
 conppeqop   | oid[]    |    -- 如果是一個外鍵,是PK = PK比較的相等操作符的列表
 conffeqop   | oid[]    |     -- 如果是一個外鍵,是FK = FK比較的相等操作符的列表
 conexclop   | oid[]    |     -- 如果是一個排除約束,是每個字段排除操作符的列表
 conbin    | pg_node_tree |     -- 如果是一個檢查約束,那就是其表達式的內部形式
 consrc    | text     |     -- 如果是檢查約束,則是表達式的人類可讀形式
Indexes:
  "pg_constraint_oid_index" UNIQUE, btree (oid)
  "pg_constraint_conname_nsp_index" btree (conname, connamespace)
  "pg_constraint_conrelid_index" btree (conrelid)
  "pg_constraint_contypid_index" btree (contypid)

pg_depend.deptype字段類型9.1之后多了一個extension的類型,目前類型有

DEPENDENCY_NORMAL (n)   :普通的依賴對象,如表與schema的關系
DEPENDENCY_AUTO (a)    :自動的依賴對象,如主鍵約束
DEPENDENCY_INTERNAL (i)  :內部的依賴對象,通常是對象本身
DEPENDENCY_EXTENSION (e) :9.1新增的的擴展依賴
DEPENDENCY_PIN (p)    :系統內置的依賴

二、例子

wiki上有一個SQL可以列出系統和用戶對象的各種依賴關系,低版本的可以看wiki上的另一個寫法

SELECT classid::regclass AS "depender object class",
  CASE classid
    WHEN 'pg_class'::regclass THEN objid::regclass::text
    WHEN 'pg_type'::regclass THEN objid::regtype::text
    WHEN 'pg_proc'::regclass THEN objid::regprocedure::text
    ELSE objid::text
  END AS "depender object identity",
  objsubid,
  refclassid::regclass AS "referenced object class",
  CASE refclassid
    WHEN 'pg_class'::regclass THEN refobjid::regclass::text
    WHEN 'pg_type'::regclass THEN refobjid::regtype::text
    WHEN 'pg_proc'::regclass THEN refobjid::regprocedure::text
    ELSE refobjid::text
  END AS "referenced object identity",
  refobjsubid,
  CASE deptype
    WHEN 'p' THEN 'pinned'
    WHEN 'i' THEN 'internal'
    WHEN 'a' THEN 'automatic'
    WHEN 'n' THEN 'normal'
  END AS "dependency type"
FROM pg_catalog.pg_depend WHERE (objid >= 16384 OR refobjid >= 16384);

BTW:我通常喜歡在where后面加個條件 and deptype <>'i' 排除internal依賴

建一張普通的表,執行上面的SQL

postgres=# create table tbl_parent(id int);
CREATE TABLE
postgres=# 執行上面的SQL;
 depender object class | depender object identity | objsubid | referenced object class | referenced object identity | refobjsubid | dependency type
-----------------------+--------------------------+----------+-------------------------+------------- pg_class       | tbl_parent        |    0 | pg_namespace      | 2200            |      0 | normal
(1 row)

--普通用戶來看只是建了個表,但是沒有約束,其實因為這個表是建立在schema下面,表是依賴于schema上面的

加一個主鍵約束

postgres=# alter table tbl_parent add primary key(id);
ALTER TABLE
 depender object class | depender object identity | objsubid | referenced object class | referenced object identity | refobjsubid | dependency type
-----------------------+--------------------------+----------+-------------------------+------- pg_class       | tbl_parent        |    0 | pg_namespace      | 2200            |      0 | normal
 pg_constraint     | 16469          |    0 | pg_class        | tbl_parent         |      1 | automatic
(2 rows)

--多了一個約束的信息,下面的這條信息表明這個主鍵約束是依賴于表上的,并且是自動模式,詳細信息可以在系統表pg_constrant里面查詢

三、非正常刪除

正常情況下用戶刪除有依賴關系的對象時會提示需要先刪除最里層沒依賴的對象,但是如果通過刪除系統表,但又刪得不對,就會導致異常,比如上面這個例子會出現 cache lookup failed for constraint

postgres=# select oid,conname,connamespace,contype from pg_constraint where conname like 'tbl_parent%';
 oid |   conname   | connamespace | contype
-------+-----------------+--------------+---------
 16469 | tbl_parent_pkey |     2200 | p
(1 row)
 
postgres=# delete from pg_constraint where conname like 'tbl_parent%';
DELETE 1
postgres=# select oid,conname,connamespace,contype from pg_constraint where conname like 'tbl_parent%';
 oid | conname | connamespace | contype
-----+---------+--------------+---------
(0 rows)
 
postgres=# drop table tbl_parent;
ERROR: cache lookup failed for constraint 16469  --16496是約束的OID
postgres=#

--出現這個問題,是因為手工把約束對象刪除了,但是在pg_depend依賴關系里面卻仍然存在關系,所以刪除該表時發現最里層的依賴對象找不到了就報錯了,

解決:

1.手工恢復該表的約束對象,比較難也比較煩

2.刪除該表所有的系統依賴信息 上面的問題需要刪除

postgres=# delete from pg_depend where objid = 16469 or refobjid = 16469 ;
DELETE 2
postgres=# drop table tbl_parent;
DROP TABLE

3.要說一點的是不要去手工刪除一些系統表信息來達到刪除約束的目的,容易因刪不干凈而造成各種異常

上述就是小編為大家分享的怎么在PostgreSQL中查看表的主外鍵了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。

本文標題:怎么在PostgreSQL中查看表的主外鍵-創新互聯
文章路徑:http://www.yijiale78.com/article38/dpcipp.html

成都網站建設公司_創新互聯,為您提供搜索引擎優化Google軟件開發做網站網站營銷靜態網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司