检查表是否从PostgreSQL中的其他表继承 [英] Check if table inherits from other table in PostgreSQL
问题描述
在PostgreSQL中为这些表
CREATE TABLE城市(
名称文本,
人口浮动,
altitude int - in feet
);
CREATE TABLE cities_capitals(
state char(2)
)INHERITS(城市);
如何以程式方式检查其中一个表是否继承从另一个表? (think information_schema,pg_catalog,...)
应为 true
为cities_capitals和 //www.postgresql.org/docs/current/interactive/catalog-pg-inherits.htmlrel =noreferrer> pg_inherits 。
目录pg_inherits记录有关表继承
层次结构的信息。在
数据库中,每个直接子表都有一个条目。 (间接继承可以通过以下链条
确定)
这里有一个符合你的问题的查询: p>
SELECT EXISTS(
SELECT 1
FROM pg_catalog.pg_inherits
WHERE inhrelid ='public.cities_capitals ':: regclass :: oid);
TRUE
if table cities_capitals
继承自某处, FALSE
。
模式限定名称以确保。
In PostgreSQL for these tables
CREATE TABLE cities (
name text,
population float,
altitude int -- in feet
);
CREATE TABLE cities_capitals (
state char(2)
) INHERITS (cities);
How can I programmatically check whether one of these tables inherits from another table or not? (Think information_schema, pg_catalog, ...)
Should be true
for cities_capitals and false
for cities.
There is a catalog table for that pg_inherits.
The catalog pg_inherits records information about table inheritance hierarchies. There is one entry for each direct child table in the database. (Indirect inheritance can be determined by following chains of entries.)
Here's a query that fits your question:
SELECT EXISTS (
SELECT 1
FROM pg_catalog.pg_inherits
WHERE inhrelid = 'public.cities_capitals'::regclass::oid);
TRUE
if table cities_capitals
inherits from somewhere, else FALSE
.
Schema-qualify the name to be sure.
这篇关于检查表是否从PostgreSQL中的其他表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!