POSTGRE 查看表结构语句
时间: 2023-10-21 10:54:05 浏览: 148
可以使用以下语句来查看POSTGRE数据库中表的结构:
1. 使用INFORMATION_SCHEMA查询表的结构信息:
SELECT column_name, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.columns
WHERE table_name = 'your_table_name';
2. 使用表oid查询表字段的定义:
SELECT t.typname AS type, a.attname AS field, a.attlen AS length, a.atttypmod AS lengthvar, a.attnotnull AS notnull, b.description AS comment
FROM pg_attribute a
LEFT OUTER JOIN pg_description b ON a.attrelid = b.objoid AND a.attnum = b.objsubid
JOIN pg_type t ON a.atttypid = t.oid
WHERE a.attnum > 0 AND a.attrelid = 'your_table_oid'
ORDER BY a.attnum;
3. 使用SQL方式查看表结构:
SELECT A.attnum, A.attname AS field, T.typname AS type, A.attlen AS length, A.atttypmod AS lengthvar, A.attnotnull AS notnull, b.description AS comment
FROM pg_class C
JOIN pg_attribute A ON A.attrelid = C.oid
LEFT OUTER JOIN pg_description b ON A.attrelid = b.objoid AND A.attnum = b.objsubid
JOIN pg_type T ON A.atttypid = T.oid
WHERE C.relname = 'your_table_name' AND A.attnum > 0
ORDER BY A.attnum;
请将上述语句中的'your_table_name'替换为你要查询的表名,'your_table_oid'替换为你要查询的表的oid。
#### 引用[.reference_title]
- *1* [PostgreSQL中查看表结构的方法](https://blog.csdn.net/lukeUnique/article/details/130673634)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [postgresql用sql语句查询表结构](https://blog.csdn.net/qq_37279783/article/details/103566777)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [PostgreSQL查询表名称及表结构](https://blog.csdn.net/weixin_41287260/article/details/115347678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文