3.6. Inheritance

3.6. Inheritance

3.6. 继承

Inheritance is a concept from object-oriented databases. It opens up interesting new possibilities of database design.

继承是来自于面向对象数据库的一个概念。它为数据库设计带来了更多的可能性。

Let's create two tables: A table cities and a table capitals. Naturally, capitals are also cities,so you want some way to show the capitals implicitly when you list all cities. If you're really clever you might invent some scheme like this:

假设我们要创建两张表:表cities和表capitals。一般,首都也是城市,所以当我们在列出所有城市的时候可能需要一些方式可以将首都标记出来。可能你会聪明的设计出类似于以下的方案:

CREATE TABLE capitals (

name text,

population real,

altitude int, -- (in ft)

state char(2)

);3.6. Inheritance

CREATE TABLE non_capitals (

name text,

population real,

altitude int -- (in ft)

);

CREATE VIEW cities AS

SELECT name, population, altitude FROM capitals

UNION

SELECT name, population, altitude FROM non_capitals;

This works OK as far as querying goes, but it gets ugly when you need to update several rows, for one thing.

这种设计在查询的时候满足了要求,但是当进行更新操作时,则会很不方便。

A better solution is this:

更好的解决方案示例:

CREATE TABLE cities (

name text,

population real,

altitude int -- (in ft)

);

CREATE TABLE capitals (

state char(2)

) INHERITS (cities);

In this case, a row of capitals inherits all columns (name, population, and altitude) from its parent, cities. The type of the column name is text, a native PostgreSQL type for variable length character strings. State capitals have an extra column, state, that shows their state. In PostgreSQL,a table can inherit from zero or more other tables.

此场景下,capitals的行继承自其父表cities的所有列(name,population,altitude)。列name的数据类型是text,一种PostgreSQL原生支持的变长字符串。首都包含一个额外的列state以展示是哪国的首都。PostgreSQL中,表可以继承自0个或多个表。

For example, the following query finds the names of all cities, including state capitals, that are located at an altitude over 500 feet:

下例中的查询返回了所有海拔高于500英尺的城市,包含首都:

SELECT name, altitude

FROM cities

WHERE altitude > 500;

which returns:

name | altitude

-----------+----------

Las Vegas | 2174

Mariposa | 1953

Madison | 845

(3 rows)

On the other hand, the following query finds all the cities that are not state capitals and are situated at an altitude over 500 feet:

而下例,返回的为海拔高于500英尺的城市,不包含首都:

SELECT name, altitude

FROM ONLY cities

WHERE altitude > 500;

name | altitude

-----------+----------

Las Vegas | 2174

Mariposa | 1953

(2 rows)

Here the ONLY before cities indicates that the query should be run over only the cities table, and not tables below cities in the inheritance hierarchy. Many of the commands that we have already discussed — SELECT, UPDATE, and DELETE — support this ONLY notation.

cities表钱的ONLY关键字表示只查询cities表的内容,而不查询继承自cities表的其他表。前面我们讨论过的许多命令--例如select、update、delete,均支持ONLY关键字。

Note

Although inheritance is frequently useful, it has not been integrated with unique constraints or foreign keys, which limits its usefulness. See Section 5.9 for more detail.

虽然继承挺有用,但因为没有雨唯一约束及外键约束继承,所以在一定程度上限制了它的实用性。详情参见 5.9节。

发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104052507