信息模式,如何检索链接表

发布于 2024-11-04 17:56:53 字数 132 浏览 0 评论 0原文

我正在编写一个可以基于数据库创建数据仓库的脚本。为了识别所有集线器,我需要一个查询来提供链接表的名称(多对多)。

做到这一点的最佳方法是什么:

找到代理键还是我必须分析它们来自和去向的关系?

预先感谢您

I'm wokring on a script that can create a data vault based on a database. To identify all the hubs i need a query that gives me the name of the link tables (many-to-many).

what is the best way to do this:

find surrogate keys or do I have to analyse the relations where they come from and where they go to?

Thank you in afvanced

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2024-11-11 17:56:53

在多对多中,您可以依靠外键的存在。您不能指望代理键的存在。

至少,我认为您需要识别具有

  • 复合主键或
  • 复合唯一约束

的表,以及至少两列具有对其他表的外键引用的列。

如果您的平台支持 information_schema 视图,您可能需要查看其中的一个或多个。

  • information_schema.key_column_usage
  • information_schema.referential_constraints
  • information_schema.table_constraints

我认为这将为您提供 PostgreSQL 中具有复合主键的表。可能会帮助您入门。

select t.constraint_name, t.table_name, count(*) num_key_columns
from information_schema.table_constraints t
inner join information_schema.key_column_usage k 
    on (t.constraint_name = k.constraint_name)
where t.constraint_type = 'PRIMARY KEY' or t.constraint_type = 'UNIQUE'
group by t.constraint_name, t.table_name
having count(*) >= 2;

(如果我这样做,我会从此查询创建一个视图。另一个用于外键引用。对它们的连接应该非常简单。)

In a many-to-many, you can count on the existence of foreign keys. You can't count on the existence of surrogate keys.

At the very least, I think you'll need to identify tables that have

  • either a compound primary key or
  • a compound unique constraint

together with at least two columns that have foreign key references to other tables.

If your platform supports information_schema views, you'll probably need to look at one or more of these.

  • information_schema.key_column_usage
  • information_schema.referential_constraints
  • information_schema.table_constraints

I think this will give you the tables that have a compound primary key in PostgreSQL. Might help get you started.

select t.constraint_name, t.table_name, count(*) num_key_columns
from information_schema.table_constraints t
inner join information_schema.key_column_usage k 
    on (t.constraint_name = k.constraint_name)
where t.constraint_type = 'PRIMARY KEY' or t.constraint_type = 'UNIQUE'
group by t.constraint_name, t.table_name
having count(*) >= 2;

(If I were doing this, I'd create a view from this query. Another for foreign key references. Joins on them should be dead simple.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文