COLUMNS

提供表列信息。

示例

COLUMNS 表中检索表和列信息:

=> SELECT table_schema, table_name, column_name, data_type, is_nullable
   FROM columns WHERE table_schema = 'store'
   AND data_type = 'Date';
 table_schema |    table_name     |      column_name       | data_type | is_nullable
--------------+-------------------+------------------------+-----------+-------------
 store        | store_dimension   | first_open_date        | Date      | f
 store        | store_dimension   | last_remodel_date      | Date      | f
 store        | store_orders_fact | date_ordered           | Date      | f
 store        | store_orders_fact | date_shipped           | Date      | f
 store        | store_orders_fact | expected_delivery_date | Date      | f
 store        | store_orders_fact | date_delivered         | Date      | f
6 rows)

DATETIME_PRECISION 为 NULL,因为表定义未声明精度:

=> CREATE TABLE c (c TIMESTAMP);
CREATE TABLE
=> SELECT table_name, column_name, datetime_precision FROM columns
   WHERE table_name = 'c';
 table_name | column_name | datetime_precision
------------+-------------+--------------------
 c          | c           |
(1 row)

DATETIME_PRECISION 为 4,因为表定义将精度声明为 4:

=> DROP TABLE c;
=> CREATE TABLE c (c TIMESTAMP(4));
CREATE TABLE
=> SELECT table_name, column_name, datetime_precision FROM columns
   WHERE table_name = 'c';
 table_name | column_name | datetime_precision
------------+-------------+--------------------
 c          | c           |                  4

标识列是仅适用于数字列类型的序列。要标识表中的哪一列(如果有)是标识列,请搜索 COLUMNS 表,在表 testid 中查找标识列:

=> CREATE TABLE testid (c1 IDENTITY(1, 1, 1000), c2 INT);
=> \x
Expanded display is on.
=> SELECT * FROM COLUMNS WHERE is_identity='t' AND table_name='testid';
-[ RECORD 1 ]------------+--------------------
table_id                 | 45035996273719486
table_schema             | public
table_name               | testid
is_system_table          | f
column_id                | 45035996273719486-1
column_name              | c1
data_type                | int
data_type_id             | 6
data_type_length         | 8
character_maximum_length |
numeric_precision        |
numeric_scale            |
datetime_precision       |
interval_precision       |
ordinal_position         | 1
is_nullable              | f
column_default           |
is_identity              | t

使用 SEQUENCES 表获得关于 testid 中序列的详细信息:

=> SELECT * FROM sequences WHERE identity_table_name='testid';
-[ RECORD 1 ]-------+--------------------
sequence_schema     | public
sequence_name       | testid_c1_seq
owner_name          | dbadmin
identity_table_name | testid
session_cache_count | 1000
allow_cycle         | f
output_ordered      | f
increment_by        | 1
minimum             | 1
maximum             | 9223372036854775807
current_value       | 0
sequence_schema_id  | 45035996273704976
sequence_id         | 45035996273719488
owner_id            | 45035996273704962
identity_table_id   | 45035996273719486

有关序列和标识列的更多信息,请参阅 序列