Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The information schema consists of a set of views that contain information about the objects defined in the current database. The information schema is defined in the SQL standard and can therefore be expected to be portable and remain stable — unlike the system catalogs, which are specific to PostgreSQL and are modeled after implementation concerns. The information schema views do not, however, contain information about PostgreSQL-specific features; to inquire about those you need to query the system catalogs or other PostgreSQL-specific views.
When querying the database for constraint information, it is possible for a standard-compliant query that expects to return one row to return several. This is because the SQL standard requires constraint names to be unique within a schema, but PostgreSQL does not enforce this restriction. PostgreSQL automatically-generated constraint names avoid duplicates in the same schema, but users can specify such duplicate names.
This problem can appear when querying information schema views such as check_constraint_routine_usage
, check_constraints
, domain_constraints
, and referential_constraints
. Some other views have similar issues but contain the table name to help distinguish duplicate rows, e.g., constraint_column_usage
, constraint_table_usage
, table_constraints
.
The information schema itself is a schema named information_schema
. This schema automatically exists in all databases. The owner of this schema is the initial database user in the cluster, and that user naturally has all the privileges on this schema, including the ability to drop it (but the space savings achieved by that are minuscule).
By default, the information schema is not in the schema search path, so you need to access all objects in it through qualified names. Since the names of some of the objects in the information schema are generic names that might occur in user applications, you should be careful if you want to put the information schema in the path.
The columns of the information schema views use special data types that are defined in the information schema. These are defined as simple domains over ordinary built-in types. You should not use these types for work outside the information schema, but your applications must be prepared for them if they select from the information schema.
These types are:cardinal_number
A nonnegative integer.character_data
A character string (without specific maximum length).sql_identifier
A character string. This type is used for SQL identifiers, the type character_data
is used for any other kind of text data.time_stamp
A domain over the type timestamp with time zoneyes_or_no
A character string domain that contains either YES
or NO
. This is used to represent Boolean (true/false) data in the information schema. (The information schema was invented before the typeboolean
was added to the SQL standard, so this convention is necessary to keep the information schema backward compatible.)
Every column in the information schema has one of these five types.
The view constraint_column_usage
identifies all columns in the current database that are used by some constraint. Only those columns are shown that are contained in a table owned by a currently enabled role. For a check constraint, this view identifies the columns that are used in the check expression. For a foreign key constraint, this view identifies the columns that the foreign key references. For a unique or primary key constraint, this view identifies the constrained columns.
constraint_column_usage
ColumnsThe view key_column_usage
identifies all columns in the current database that are restricted by some unique, primary key, or foreign key constraint. Check constraints are not included in this view. Only those columns are shown that the current user has access to, by way of being the owner or having some privilege.
Table 36.30. key_column_usage
Columns
The view columns
contains information about all table columns (or view columns) in the database. System columns (ctid
, etc.) are not included. Only those columns are shown that the current user has access to (by way of being the owner or having some privilege).
columns
ColumnsSince data types can be defined in a variety of ways in SQL, and PostgreSQL contains additional ways to define data types, their representation in the information schema can be somewhat difficult. The column data_type
is supposed to identify the underlying built-in type of the column. In PostgreSQL, this means that the type is defined in the system catalog schema pg_catalog
. This column might be useful if the application can handle the well-known built-in types specially (for example, format the numeric types differently or use the data in the precision columns). The columns udt_name
, udt_schema
, and udt_catalog
always identify the underlying data type of the column, even if the column is based on a domain. (Since PostgreSQL treats built-in types like user-defined types, built-in types appear here as well. This is an extension of the SQL standard.) These columns should be used if an application wants to process data differently according to the type, because in that case it wouldn't matter if the column is really based on a domain. If the column is based on a domain, the identity of the domain is stored in the columns domain_name
, domain_schema
, and domain_catalog
. If you want to pair up columns with their associated data types and treat domains as separate types, you could write coalesce(domain_name, udt_name)
, etc.
The view referential_constraints
contains all referential (foreign key) constraints in the current database. Only those constraints are shown for which the current user has write access to the referencing table (by way of being the owner or having some privilege other than SELECT
).
referential_constraints
ColumnsName
Data Type
Description
table_catalog
sql_identifier
Name of the database that contains the table that contains the column that is used by some constraint (always the current database)
table_schema
sql_identifier
Name of the schema that contains the table that contains the column that is used by some constraint
table_name
sql_identifier
Name of the table that contains the column that is used by some constraint
column_name
sql_identifier
Name of the column that is used by some constraint
constraint_catalog
sql_identifier
Name of the database that contains the constraint (always the current database)
constraint_schema
sql_identifier
Name of the schema that contains the constraint
constraint_name
sql_identifier
Name of the constraint
Name
Data Type
Description
constraint_catalog
sql_identifier
Name of the database that contains the constraint (always the current database)
constraint_schema
sql_identifier
Name of the schema that contains the constraint
constraint_name
sql_identifier
Name of the constraint
table_catalog
sql_identifier
Name of the database that contains the table that contains the column that is restricted by this constraint (always the current database)
table_schema
sql_identifier
Name of the schema that contains the table that contains the column that is restricted by this constraint
table_name
sql_identifier
Name of the table that contains the column that is restricted by this constraint
column_name
sql_identifier
Name of the column that is restricted by this constraint
ordinal_position
cardinal_number
Ordinal position of the column within the constraint key (count starts at 1)
position_in_unique_constraint
cardinal_number
For a foreign-key constraint, ordinal position of the referenced column within its unique constraint (count starts at 1); otherwise null
Name
Data Type
Description
table_catalog
sql_identifier
Name of the database containing the table (always the current database)
table_schema
sql_identifier
Name of the schema containing the table
table_name
sql_identifier
Name of the table
column_name
sql_identifier
Name of the column
ordinal_position
cardinal_number
Ordinal position of the column within the table (count starts at 1)
column_default
character_data
Default expression of the column
is_nullable
yes_or_no
YES
if the column is possibly nullable, NO
if it is known not nullable. A not-null constraint is one way a column can be known not nullable, but there can be others.
data_type
character_data
Data type of the column, if it is a built-in type, or ARRAY
if it is some array (in that case, see the view element_types
), else USER-DEFINED
(in that case, the type is identified in udt_name
and associated columns). If the column is based on a domain, this column refers to the type underlying the domain (and the domain is identified in domain_name
and associated columns).
character_maximum_length
cardinal_number
If data_type
identifies a character or bit string type, the declared maximum length; null for all other data types or if no maximum length was declared.
character_octet_length
cardinal_number
If data_type
identifies a character type, the maximum possible length in octets (bytes) of a datum; null for all other data types. The maximum octet length depends on the declared character maximum length (see above) and the server encoding.
numeric_precision
cardinal_number
If data_type
identifies a numeric type, this column contains the (declared or implicit) precision of the type for this column. The precision indicates the number of significant digits. It can be expressed in decimal (base 10) or binary (base 2) terms, as specified in the column numeric_precision_radix
. For all other data types, this column is null.
numeric_precision_radix
cardinal_number
If data_type
identifies a numeric type, this column indicates in which base the values in the columns numeric_precision
and numeric_scale
are expressed. The value is either 2 or 10. For all other data types, this column is null.
numeric_scale
cardinal_number
If data_type
identifies an exact numeric type, this column contains the (declared or implicit) scale of the type for this column. The scale indicates the number of significant digits to the right of the decimal point. It can be expressed in decimal (base 10) or binary (base 2) terms, as specified in the column numeric_precision_radix
. For all other data types, this column is null.
datetime_precision
cardinal_number
If data_type
identifies a date, time, timestamp, or interval type, this column contains the (declared or implicit) fractional seconds precision of the type for this column, that is, the number of decimal digits maintained following the decimal point in the seconds value. For all other data types, this column is null.
interval_type
character_data
If data_type
identifies an interval type, this column contains the specification which fields the intervals include for this column, e.g., YEAR TO MONTH
, DAY TO SECOND
, etc. If no field restrictions were specified (that is, the interval accepts all fields), and for all other data types, this field is null.
interval_precision
cardinal_number
Applies to a feature not available in PostgreSQL (see datetime_precision
for the fractional seconds precision of interval type columns)
character_set_catalog
sql_identifier
Applies to a feature not available in PostgreSQL
character_set_schema
sql_identifier
Applies to a feature not available in PostgreSQL
character_set_name
sql_identifier
Applies to a feature not available in PostgreSQL
collation_catalog
sql_identifier
Name of the database containing the collation of the column (always the current database), null if default or the data type of the column is not collatable
collation_schema
sql_identifier
Name of the schema containing the collation of the column, null if default or the data type of the column is not collatable
collation_name
sql_identifier
Name of the collation of the column, null if default or the data type of the column is not collatable
domain_catalog
sql_identifier
If the column has a domain type, the name of the database that the domain is defined in (always the current database), else null.
domain_schema
sql_identifier
If the column has a domain type, the name of the schema that the domain is defined in, else null.
domain_name
sql_identifier
If the column has a domain type, the name of the domain, else null.
udt_catalog
sql_identifier
Name of the database that the column data type (the underlying type of the domain, if applicable) is defined in (always the current database)
udt_schema
sql_identifier
Name of the schema that the column data type (the underlying type of the domain, if applicable) is defined in
udt_name
sql_identifier
Name of the column data type (the underlying type of the domain, if applicable)
scope_catalog
sql_identifier
Applies to a feature not available in PostgreSQL
scope_schema
sql_identifier
Applies to a feature not available in PostgreSQL
scope_name
sql_identifier
Applies to a feature not available in PostgreSQL
maximum_cardinality
cardinal_number
Always null, because arrays always have unlimited maximum cardinality in PostgreSQL
dtd_identifier
sql_identifier
An identifier of the data type descriptor of the column, unique among the data type descriptors pertaining to the table. This is mainly useful for joining with other instances of such identifiers. (The specific format of the identifier is not defined and not guaranteed to remain the same in future versions.)
is_self_referencing
yes_or_no
Applies to a feature not available in PostgreSQL
is_identity
yes_or_no
If the column is an identity column, then YES
, else NO
.
identity_generation
character_data
If the column is an identity column, then ALWAYS
or BY DEFAULT
, reflecting the definition of the column.
identity_start
character_data
If the column is an identity column, then the start value of the internal sequence, else null.
identity_increment
character_data
If the column is an identity column, then the increment of the internal sequence, else null.
identity_maximum
character_data
If the column is an identity column, then the maximum value of the internal sequence, else null.
identity_minimum
character_data
If the column is an identity column, then the minimum value of the internal sequence, else null.
identity_cycle
yes_or_no
If the column is an identity column, then YES
if the internal sequence cycles or NO
if it does not; otherwise null.
is_generated
character_data
If the column is a generated column, then ALWAYS
, else NEVER
.
generation_expression
character_data
If the column is a generated column, then the generation expression, else null.
is_updatable
yes_or_no
YES
if the column is updatable, NO
if not (Columns in base tables are always updatable, columns in views not necessarily)
Name | Data Type | Description |
|
| Name of the database that contains the constraint (always the current database) |
|
| Name of the schema that contains the constraint |
|
| Name of the constraint |
|
| Name of the database that contains the table (always the current database) |
|
| Name of the schema that contains the table |
|
| Name of the table |
|
| Type of the constraint: |
|
|
|
|
|
|
|
| Applies to a feature not available in PostgreSQL (currently always |
Name | Data Type | Description |
|
| Name of the database containing the constraint (always the current database) |
|
| Name of the schema containing the constraint |
|
| Name of the constraint |
|
| Name of the database that contains the unique or primary key constraint that the foreign key constraint references (always the current database) |
|
| Name of the schema that contains the unique or primary key constraint that the foreign key constraint references |
|
| Name of the unique or primary key constraint that the foreign key constraint references |
|
| Match option of the foreign key constraint: |
|
| Update rule of the foreign key constraint: |
|
| Delete rule of the foreign key constraint: |
Name | Data Type | Description |
|
| 查詢當下的資料庫名稱(只會是目前資料庫) |
|
| Name of the schema |
|
| Name of the owner of the schema |
|
| Applies to a feature not available in PostgreSQL |
|
| Applies to a feature not available in PostgreSQL |
|
| Applies to a feature not available in PostgreSQL |
|
| Applies to a feature not available in PostgreSQL |