gpt4 book ai didi

sql - Postgresql 中的 "descendant tables"是什么?

转载 作者:行者123 更新时间:2023-11-29 11:21:18 24 4
gpt4 key购买 nike

Postgresql 的数据库转储使用 ALTER TABLE ONLY tablename 而不是我熟悉的 ALTER TABLE tablename。我很好奇 ONLY 关键字的作用,所以我在 Postgresql documentation 中查找了它,它说了以下内容:

name

The name (optionally schema-qualified) of an existing table to alter. If ONLY is specified before the table name, only that table is altered. If ONLY is not specified, the table and all its descendant tables (if any) are altered. Optionally, * can be specified after the table name to explicitly indicate that descendant tables are included.

什么是后代表?

最佳答案

PostgreSQL implements table inheritance, which can be a useful tool for database designers. (SQL:1999 and later define a type inheritance feature, which differs in many respects from the features described here.)

Let's start with an example: suppose we are trying to build a data model for cities. Each state has many cities, but only one capital. We want to be able to quickly retrieve the capital city for any particular state. This can be done by creating two tables, one for state capitals and one for cities that are not capitals. However, what happens when we want to ask for data about a city, regardless of whether it is a capital or not? The inheritance feature can help to resolve this problem. We define the capitals table so that it inherits from cities:

CREATE TABLE cities (
name text,
population float,
altitude int -- in feet
);

CREATE TABLE capitals (
state char(2)
) INHERITS (cities);

In this case, the capitals table inherits all the columns of its parent table, cities. State capitals also have an extra column, state, that shows their state.

In PostgreSQL, a table can inherit from zero or more other tables, and a query can reference either all rows of a table or all rows of a table plus all of its descendant tables. The latter behavior is the default.

来源:https://www.postgresql.org/docs/8.4/static/ddl-inherit.html

关于sql - Postgresql 中的 "descendant tables"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com