有誰知道如何在Postgres 9.1中找到表的OID?我正在編寫一個更新腳本,它在嘗試創建列之前需要測試表中列是否存在。這是爲了防止第一個錯誤後運行腳本。確定Postgres 9.1中的表的OID?
23
A
回答
18
postgres目錄表pg_class
是你應該看看。每個表應該有一行,表名在relname
列中,並且隱藏列中的oid爲oid
。
該目錄表位於postgres
數據庫中,因此請確保連接到該數據庫,而不是您的應用程序數據庫。
您可能也有興趣pg_attribute
目錄表,其中包括每表列一行。
見:http://www.postgresql.org/docs/current/static/catalog-pg-class.html和http://www.postgresql.org/docs/current/static/catalog-pg-attribute.html
31
有一個簡單的方法來獲得一個表OID。投射到object-identifier classregclass
(同時連接到各個DB):
SELECT 'mytbl'::regclass::oid;
缺省爲所述第一模式在search_path
或如果沒有找到引發一個例外。
或者明確地架構資格:
SELECT 'myschema.mytbl'::regclass::oid;
然後你只需要檢查目錄表pg_attribute
該列的存在:
SELECT TRUE AS col_exists
FROM pg_attribute
WHERE attrelid = 'mytbl'::regclass
AND attname = 'mycol'
AND NOT attisdropped -- no dropped (dead) columns
-- AND attnum > 0 -- no system columns (you may or may not want this)
在Postgres裏9.4或更高版本,你也可以使用to_regclass('mytbl')
,如果未找到表格,則不會引發異常:
2
剛剛完成的可能性我想補充一點,存在對以刪除列沒有錯誤了語法:
ALTER TABLE MYTBL DROP COLUMN IF EXISTS mycol
請參閱http://www.postgresql.org/docs/9.0/static/sql-altertable.html
然後您可以安全地添加您的列。
0
相關問題
- 1. row_to_json on postgres 9.1
- 2. 如何在postgres中的表中找到行的OID?
- 3. 創建OID到Postgres表創建後
- 4. 的Postgres 9.1 - 獲取下一個值
- 5. PostgreSQL 9.1中的SET TABLESPACE表鎖定?
- 6. 長的COMMIT持續時間,postgres中的高I/O等待9.1
- 7. Postgresql 9.1中的正確整理
- 8. 如何重新實現在Postgres 9.1
- 9. 在Postgres 9.1上更新查詢太慢
- 10. 什麼是Postgres中'money'和'OID'的sqlalchemy等效列類型?
- 11. postgres中觸發器的日誌觸發9.1
- 12. postgres中的圖像大小不匹配9.1
- 13. 在postgres中顯示輸出所用的時間9.1
- 14. Hibernate的註解爲Serial ID在Postgres的9.1
- 15. Postgresql 9.1中array_agg的限制
- 16. JmDNS不正確的Andr oid 4.1
- 17. python pysnmp使用getbulk中的oid列表
- 18. Postgres 9.1 - 無法創建adminpack與pgadmin一起使用的擴展
- 19. 在Ubuntu 9.1配置Postgres用於黃瓜測試的Rails dev-box
- 20. Postgres 9.1的Docker容器不公開端口5432到主機
- 21. 無法檢索連接MVC3 EF元數據的Postgres 9.1
- 22. 的Postgres 9.1全文搜索返回任何結果
- 23. Postgresql:角色oid的外鍵
- 24. ApacheDS OID指定數據類型列表
- 25. Postgres:確定表中沒有掃描整個表的變化的最佳方法
- 26. PostgreSQL 9.1:在標題中查找確切表達式
- 27. Postgres確定所有blob的大小
- 28. Solr導入鎖定表中的postgres
- 29. RFC-1213 mib中ipInDelivers oid(.1.3.6.1.2.1.4.9.0)和ipOutRequests oid(.1.3.6.1.2.1.4.10.0)之間的區別
- 30. For循環與Postgresql 9.1中的動態表名稱?
我曾經看過'pg_class'和'pg_attribute'表中的文檔,但我不知道'pg_class'表中有一個名爲'oid'的隱藏列。我無法弄清楚文檔的來源。謝謝! –
這不太準確。每個數據庫都有一個名爲'pg_catalog'的模式,其中包含特定於數據庫的目錄表。 –
我第二@TonyVitabile。這是我正在尋找的隱藏專欄「oid」。 –