【问题标题】:What is the proper approach to insert into multiple tables at once?一次插入多个表的正确方法是什么?
【发布时间】:2016-05-19 03:04:07
【问题描述】:

例如,我有一个名为 product_list 的表,其中包含产品列表:

+----+-------+-----------+-------------+--+
| id | name  | weight(g) | type        |  |
+----+-------+-----------+-------------+--+
| 1  | Shirt | 157       | Clothes     |  |
+----+-------+-----------+-------------+--+
| 2  | Ring  | 53        | Accessories |  |
+----+-------+-----------+-------------+--+
| 3  | Pants | 202       | Clothes     |  |
+----+-------+-----------+-------------+--+

还有一个名为product_price的表:

+----------+----+-------+--------+--+
| price_id | id | name  | price  |  |
+----------+----+-------+--------+--+
| 1        | 1  | Shirt | 99.00  |  |
+----------+----+-------+--------+--+
| 2        | 2  | Ring  | 149.00 |  |
+----------+----+-------+--------+--+
| 3        | 3  | Pants | 119.00 |  |
+----------+----+-------+--------+--+

如果我在 product_list 中插入 1 行数据,部分数据(例如 product_idproduct name)也应该插入到另一个表中,例如 product_price,其中包含所有产品的价格(新产品的价格将具有 0 或 NULL 值)。例如:

product_list:

+----+--------+-----------+-------------+--+
| id | name   | weight(g) | type        |  |
+----+--------+-----------+-------------+--+
| 1  | Shirt  | 157       | Clothes     |  |
+----+--------+-----------+-------------+--+
| 2  | Ring   | 53        | Accessories |  |
+----+--------+-----------+-------------+--+
| 3  | Pants  | 202       | Clothes     |  |
+----+--------+-----------+-------------+--+
| 4  | Shirt2 | 175       | Clothes     |  |
+----+--------+-----------+-------------+--+

product_price:

+----------+----+-------+--------+--+
| price_id | id | name  | price  |  |
+----------+----+-------+--------+--+
| 1        | 1  | Shirt | 99.00  |  |
+----------+----+-------+--------+--+
| 2        | 2  | Ring  | 149.00 |  |
+----------+----+-------+--------+--+
| 3        | 3  | Pants | 119.00 |  |
+----------+----+-------+--------+--+
| 4        | 4  | Shirt2| 0.00   |  |
+----------+----+-------+--------+--+

我的问题是解决这个问题的方法。有经验的人处理这个问题的正确方法是什么(以专业的方式)?

这是我想到的两种方法:

1 - 每当我将产品数据插入product_list 时,使用触发器插入到product_price 等其他表中

2 - 使用类似product_add 的函数(存储过程)将新产品添加到每个表中。

哪种方法更好?或者如果有更好的建议,那么我想知道它。提前致谢。

TLDR:我应该使用Triggers 还是使用Stored Procedures,哪个更好?或者你有更好的建议?

【问题讨论】:

    标签: postgresql stored-procedures triggers sql-insert


    【解决方案1】:

    在 Postgres 中,您可以使用 CTE:

    with pl as (
          insert into product_list(name, weight, type)
              select . . .
              returning *
         )
    insert into product_price(id, price)
         select id, NULL
         from pl;
    

    注意:您不应在product_listproduct_price 表中重复name 列。它应该只在列表中。

    【讨论】:

    • 我有几个问题。 1. 如果我使用管理仪表板将新产品(客户端)输入数据库,您提到的这个 CTE,您会调用 stored procedure 还是只运行此代码或使用 triggers? 2. 为什么name列不应该重复,如果我想方便地用作名称参考?如果我创建了一个视图,我什至不必为名称连接表。或者我什至不需要选择 2 个单独的视图,我可以只选择 1 个视图,这不是更容易吗?
    猜你喜欢
    • 2022-01-10
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多