postgresql:提取包含所有 fk 引用的单个记录

发布于 2024-10-29 18:25:44 字数 685 浏览 0 评论 0原文

我使用大型 postgresql 生产数据库。有时需要使用实际生产数据进行调试。由于数据库的大小,将整个转储传输到我们的开发工作站是不切实际的。

有没有办法,也许使用 information_schema 来提取单个记录,以及:

  • 此记录引用的所有记录,递归级联

  • 所有记录引用此记录的所有记录(也将先前的标准应用于这些记录)


也许最好用一个例子来说明:


如果我们有以下表:

People(fk 到 Addresses)

Addresses(fk 到 Cities)

Cities

Employees(fks 到 Employee_Types, People)

Employee_Types


假设我想“提取”People 表中的特定记录。我想获得:

(1) 人员中的记录

(2) (1) 中的人员记录引用的地址中的记录 (3)

(2) 中的地址记录引用的城市中的记录

(4) 任何员工引用 (1) 中 People 记录的记录

(5) 由 (4) 中找到的 Members 记录引用的 Employee_Types 记录


有什么想法吗?

I work with a large postgresql production database. Occasionally there is a need to debug using actual production data. Because of the size of the database, it is impractical to transfer an entire dump to our development workstations.

Is there a way, perhaps using the information_schema, to extract a single record, as well as:

  • all records referenced by this record, cascaded recursively

  • all records that refer to this record (with previous criterion applied to these records as well)

Maybe best illustrated with an example:

If we have the following tables:

People (fk to Addresses)

Addresses (fk to Cities)

Cities

Employees (fks to Employee_Types, People)

Employee_Types

Say that I want to 'extract' a particular record in the People table. I would want to get:

(1) the record in People

(2) the record in Addresses referenced by the People record in (1)

(3) the record in Cities referenced by the Addresses record in (2)

(4) any Employees record that refers to the People record in (1)

(5) the Employee_Types records that are referenced by the Employees records found in (4)

Any ideas?

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

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

发布评论

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

评论(1

合久必婚 2024-11-05 18:25:44

创建一个包含与这些人相关的所有记录的转移表:

select 
    c.name as city_name, c.id as city_id,
    a.street, a.zip,
    et.type, et.type_id,
    p.id as people_id, p.name,
    e.id as employee_id, e.people_id as employee_people_id, e.type_id as employee_type_id
into transfer
from People p
inner join Addresses a on p.zip = a.zip
inner join Cities c on a.city_id = c.id
inner join Employees e on p.id = e.people_id
inner join Employee_Type et on et.id = e.type_id
where p.id in (@people_id_1, @people_id_2)

然后在最后插入到相应的表中,注意正确的顺序以避免外键错误:

insert into Cities (name, id)
select distinct city_name, city_id
from transfer

insert into Addresses (street, zip)
select distinct street, zip
from transfer

insert into People (id, name)
select distinct people_id, name
from transfer

insert into Employee_Type (type, id)
select distinct type, type_id
from transfer

insert into Employees (id, people_id, type_id)
select distinct employee_id, employee_people_id, employee_type_id
from transfer

Create a transfer table with all the records related to those persons:

select 
    c.name as city_name, c.id as city_id,
    a.street, a.zip,
    et.type, et.type_id,
    p.id as people_id, p.name,
    e.id as employee_id, e.people_id as employee_people_id, e.type_id as employee_type_id
into transfer
from People p
inner join Addresses a on p.zip = a.zip
inner join Cities c on a.city_id = c.id
inner join Employees e on p.id = e.people_id
inner join Employee_Type et on et.id = e.type_id
where p.id in (@people_id_1, @people_id_2)

Then at the destiny insert into the respective tables with attention to the correct order to avoid foreign key errors:

insert into Cities (name, id)
select distinct city_name, city_id
from transfer

insert into Addresses (street, zip)
select distinct street, zip
from transfer

insert into People (id, name)
select distinct people_id, name
from transfer

insert into Employee_Type (type, id)
select distinct type, type_id
from transfer

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