加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Postgresql中的“后代表”是什么?

发布时间:2020-12-13 16:03:37 所属栏目:百科 来源:网络整理
导读:Postgresql的数据库转储使用ALTER TABLE ONLY tablename而不是我熟悉的ALTER TABLE tablename.我很好奇ONLY关键字的作用,所以我在 Postgresql documentation中查找了它,它说如下: name The name (optionally schema-qualified) of an existing table to alt
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读