Home > Database > Mysql Tutorial > How Can Multi-Column RETURNING Enhance PostgreSQL Inserts Across Multiple Tables?

How Can Multi-Column RETURNING Enhance PostgreSQL Inserts Across Multiple Tables?

Linda Hamilton
Release: 2025-01-01 07:47:09
Original
638 people have browsed it

How Can Multi-Column RETURNING Enhance PostgreSQL Inserts Across Multiple Tables?

Utilizing Multi-Column RETURNING for Efficient PostgreSQL Inserts

In the world of data management, the ability to perform multiple inserts with a single RETURNING clause can streamline operations and enhance efficiency. This article delves into the intricacies of multi-insert operations with PostgreSQL 9.3, showcasing how to seamlessly insert data into multiple child tables while retrieving the newly generated ID.

The scenario involves three tables:

  • table1 (parent): user_id, username, name, surname, emp_date
  • table2 (child of table1): pass_id, user_id, password
  • table3 (child of table1): user_dt_id, user_id, adress, city, phone

The task at hand is to insert data into table1 and simultaneously populate the user_id columns in table2 and table3 with the newly generated user_id.

To accomplish this, we employ data-modifying Common Table Expressions (CTEs). CTEs allow us to combine multiple SQL statements into a single unit, streamlining the insertion process. Here's a detailed breakdown of the CTE implementation:

WITH ins1 AS (  -- Insert into 'table1'
   INSERT INTO table1 (username, name, surname)
   VALUES ('johnee', 'john', 'smith')
   RETURNING user_id
   ),
ins2 AS (  -- Insert into 'table2' with 'user_id' from 'ins1'
   INSERT INTO table2 (user_id, password)
   SELECT ins1.user_id, 'secret'
   FROM ins1  -- No need to return anything here
   )
INSERT INTO table3 (user_id, adress, city, phone)  -- Insert into 'table3' with 'user_id' from 'ins1'
SELECT ins1.user_id, ...
FROM ins1
RETURNING user_id;
Copy after login

Key implementation details:

  • A column definition list for INSERT statements is recommended to avoid potential issues stemming from changes in table structure.
  • Redundant DEFAULT values are omitted, allowing for a concise and efficient code.
  • The final RETURNING clause retrieves the user_id from the last INSERT operation, which typically aligns with the user_id from table3.

In summary, using multi-insert with RETURNING in PostgreSQL allows for a single batch insertion across multiple tables, reducing complexity and optimizing performance. Data-modifying CTEs provide a convenient means to achieve this, offering a highly reliable and efficient solution for complex data insertion workflows.

The above is the detailed content of How Can Multi-Column RETURNING Enhance PostgreSQL Inserts Across Multiple Tables?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template