简体   繁体   English

PostgreSQL中的类型表是什么? 我怎样做这样的桌子?

[英]What is a typed table in PostgreSQL? How can I make such table?

While studying the information_schema views, I noticed the following properties for a table: 在研究information_schema视图时,我注意到一个表的以下属性:

is_typed                  YES if the table is a typed table, NO if not
user_defined_type_catalog If the table is a typed table, the name of the database...
user_defined_type_schema  If the table is a typed table, the name of the schema...
user_defined_type_name    If the table is a typed table, the name of the data type...

See https://www.postgresql.org/docs/current/static/infoschema-tables.html . 参见https://www.postgresql.org/docs/current/static/infoschema-tables.html

I never heard of such concept. 我从未听说过这样的概念。 I have been looking in the documentation, but I cannot find more information. 我一直在寻找文档,但是找不到更多信息。

What is a typed table ? 什么是类型表 How can I create such table? 如何创建这样的表?

It's creating a table from a type. 它根据类型创建表。 Per the documentation : 根据文档

OF type_name Creates a typed table, which takes its structure from the specified composite type (name optionally schema-qualified). OF type_name创建一个带类型的表,该表从指定的组合类型(名称(可选,模式限定))获取其结构。 A typed table is tied to its type; 有类型的表与其类型相关联。 for example the table will be dropped if the type is dropped (with DROP TYPE ... CASCADE). 例如,如果删除了类型(使用DROP TYPE ... CASCADE),则将删除该表。

When a typed table is created, then the data types of the columns are determined by the underlying composite type and are not specified by the CREATE TABLE command. 创建类型表时,列的数据类型由基础复合类型确定,而不由CREATE TABLE命令指定。 But the CREATE TABLE command can add defaults and constraints to the table and can specify storage parameters. 但是CREATE TABLE命令可以向表添加默认值和约束,并可以指定存储参数。

Basically when you create a composite type (eg, using the create type statement) you can create a table from that type. 基本上,当您创建复合类型 (例如,使用create type语句)时,可以从该类型创建表。 When you cascade changes of the type (altering columns or dropping the type), it affects all tables built with that type, which means you can have many tables that are structured the same way. 当您级联类型的更改(更改列或删除类型)时,它会影响使用该类型构建的所有表,这意味着您可以拥有许多结构相同的表。 This is helpful for logging, replication, an ETL process, etc. 这对于日志记录,复制,ETL过程等很有帮助。


Creating the table ( SQLFiddle Example ) 创建表( SQLFiddle示例

Step 1: Create the type 步骤1:建立类型

CREATE TYPE people_type AS ( age INT, name TEXT, dob DATE );

Step 2: Create the table 步骤2:建立表格

CREATE TABLE sales_staff   OF people_type;
CREATE TABLE service_staff OF people_type;

Step 3: Alter type 步骤3:变更类型

ALTER TYPE people_type ADD ATTRIBUTE gender CHAR CASCADE;

After altering the type you will be able to see that both tables have been affected. 更改类型后,您将能够看到两个表都受到了影响。 This can be done by using \\d in psql, or querying the INFORMATION_SCHEMA.COLUMNS table in the database as in the example at the SQLFiddle link above. 可以通过在psql中使用\\d或查询数据库中的INFORMATION_SCHEMA.COLUMNS表来完成此操作,如上面SQLFiddle链接中的示例所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM