SQL:如何将两个表中的主键值插入主表

发布于 2024-08-27 06:41:48 字数 2880 浏览 2 评论 0原文

我希望能得到一些关于 SQL 语句的帮助,我真的无法理解。

我想要做的非常简单,当新行插入两个表之一时,我需要从两个不同的表中获取值并将它们复制到主表中。

这个问题也许最好这样解释:

我有三个表,产品类别、区域类别和主表。

         ---------------------------
TABLE:   PRODUCTCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  BOOKS      | Books
         ---------------------------

         ---------------------------
TABLE:   REGIONCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  EU         | European Union
         ---------------------------

         ------------------------------------------
TABLE:   MASTERTABLE
         ------------------------------------------
COLUMNS: REGION     | PRODUCT       | ACCOUNT
         ------------------------------------------
VALUES:  EU         | BOOKS         | NULL
         ------------------------------------------

我希望在产品类别或区域类别中创建新行时像这样插入值。

新行已创建。

         ---------------------------
TABLE:   PRODUCTCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  BOOKS      | Books
         ---------------------------
VALUES:  DVD        | DVDs
         ---------------------------

SQL 语句将新值复制到主表中。

         ------------------------------------------
TABLE:   MASTERTABLE
         ------------------------------------------
COLUMNS: REGION     | PRODUCT      | ACCOUNT
         ------------------------------------------
VALUES:  EU         | BOOKS        | NULL
         ------------------------------------------
VALUES:  EU         | DVD          | NULL
         ------------------------------------------

如果在区域类别中创建行,情况也是如此。

新行。

         ---------------------------
TABLE:   REGIONCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  EU         | European Union
         ---------------------------
VALUES:  US         | United States
         ---------------------------

复制到主表。

         ------------------------------------------
TABLE:   MASTERTABLE
         ------------------------------------------
COLUMNS: REGION     | PRODUCT       | ACCOUNT
         ------------------------------------------
VALUES:  EU         | BOOKS         | NULL
         ------------------------------------------
VALUES:  EU         | DVD           | NULL
         ------------------------------------------
VALUES:  US         | BOOKS         | NULL
         ------------------------------------------
VALUES:  US         | DVD           | NULL
         ------------------------------------------

我希望这是有道理的。

谢谢,

斯特凡

I would appreciate some help with an SQL statement I really can't get my head around.

What I want to do is fairly simple, I need to take the values from two different tables and copy them into an master table when a new row is inserted into one of the two tables.

The problem is perhaps best explained like this:

I have three tables, productcategories, regioncategories and mastertable.

         ---------------------------
TABLE:   PRODUCTCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  BOOKS      | Books
         ---------------------------

         ---------------------------
TABLE:   REGIONCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  EU         | European Union
         ---------------------------

         ------------------------------------------
TABLE:   MASTERTABLE
         ------------------------------------------
COLUMNS: REGION     | PRODUCT       | ACCOUNT
         ------------------------------------------
VALUES:  EU         | BOOKS         | NULL
         ------------------------------------------

I want the values to be inserted like this when a new row is created in either productcategories or regioncategories.

New row is created.

         ---------------------------
TABLE:   PRODUCTCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  BOOKS      | Books
         ---------------------------
VALUES:  DVD        | DVDs
         ---------------------------

And a SQL statement copies the new values into the mastertable.

         ------------------------------------------
TABLE:   MASTERTABLE
         ------------------------------------------
COLUMNS: REGION     | PRODUCT      | ACCOUNT
         ------------------------------------------
VALUES:  EU         | BOOKS        | NULL
         ------------------------------------------
VALUES:  EU         | DVD          | NULL
         ------------------------------------------

The same goes if a row is created in the regioncategories.

New row.

         ---------------------------
TABLE:   REGIONCATEGORIES
         ---------------------------
COLUMNS: CODE       | DESCRIPTION
         ---------------------------
VALUES:  EU         | European Union
         ---------------------------
VALUES:  US         | United States
         ---------------------------

Copied to the mastertable.

         ------------------------------------------
TABLE:   MASTERTABLE
         ------------------------------------------
COLUMNS: REGION     | PRODUCT       | ACCOUNT
         ------------------------------------------
VALUES:  EU         | BOOKS         | NULL
         ------------------------------------------
VALUES:  EU         | DVD           | NULL
         ------------------------------------------
VALUES:  US         | BOOKS         | NULL
         ------------------------------------------
VALUES:  US         | DVD           | NULL
         ------------------------------------------

I hope it makes sense.

Thanks,

Stefan

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

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

发布评论

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

评论(4

风吹过旳痕迹 2024-09-03 06:41:49

有两种方法可以将额外信息插入到主表中。

  1. 使用触发器 - 当 PRODUCTCATEGORIES 或 REGIONCATEGORIES 中发生插入时,将触发插入触发器并检查该行是否存在于 MASTERTABLE 中。如果没有,则添加。

  2. 创建一个存储过程以将数据插入到 PRODUCTCATEGORIES 和 REGIONCATEGORIES 表中。然后,存储过程负责检查 MASTERTABLE 并在必要时进行插入。

第二种方法的优点是,对于维护代码的其他人来说,正在发生的事情是显而易见的。触发器可以隐藏重要的功能。出于性能原因,存储过程通常是事务 SQL 的首选。

There are two ways of inserting the extra information into the MASTERTABLE.

  1. Use triggers - when an insert occurs in PRODUCTCATEGORIES or REGIONCATEGORIES, an insert trigger fires and checks if the row exists in the MASTERTABLE. If not it is added.

  2. Create a stored procedure to insert data into the PRODUCTCATEGORIES and REGIONCATEGORIES table. The stored procedure is then responsible for checking the MASTERTABLE and inserting if necessary.

The 2nd approach has the advantage that it's obvious to someone else maintaining your code what's going on. Triggers can hide important functionality. Stored procedure are usually preferred for transact SQL for performance reasons.

白馒头 2024-09-03 06:41:49

您的主表似乎是所有潜在组合的交叉联接。因此,当您添加新区域时,您必须添加该区域中的所有潜在产品。对于可以简单地从所有潜在区域和所有潜在类别推断出的数据来说,这是一项巨大的工作。

我知道您提到还有其他列。这些附加列包含什么?

我通常不会在规范化数据库上保留这样的表 - 除非您提到的附加列需要在创建时以某种特殊方式分配,然后在某种维护中进行更改 - 即使在这种情况下,稀疏表(即只有偏离默认值)和适当的默认值才能很好地工作。

我的一个系统中确实有一个类似的交叉联接,它包含大约 2500 万行,允许我们在二维 2500 个总账科目 x 10000 个成本中心空间(其中有“行”和“列”)上替换非常复杂的逻辑具有相同的逻辑,但却是逻辑的“孤岛”。

Your master table appears to be a cross join of all potential combinations. Thus when you add a new region, you are having to add all potential products in that region. This is a lot of effort for data which can simply be inferred from all potential regions and all potential categories.

I know you mention that there are additional columns. What do these additional columns contain?

I would typically not keep such a table materialized on a normalized database - unless the additional columns you are alluding to need to be assigned in some special way at creation and then altered in some kind of maintenance - even in that case, a sparse table (i.e. only departures from the defaults) with appropriate defaults can work well.

I do have a similar cross join in one of my systems and it contains about 25m rows and allows us to replace very complex logic over a 2-Dimensional 2500 general ledger accounts x 10000 cost centers space where there are "rows" and "columns" of identical logic, yet "islands" of logic.

池木 2024-09-03 06:41:49

好的,谢谢您的回答和建议。

我正在处理一个连接到 MsSQL 数据库的访问项目。我尝试通过使用表触发器来解决这个问题,但没有一个建议对我有用。因此我决定在客户端使用 VBA 代码来解决这个问题。

这可能不是解决该问题的正确方法,但了解它可能对任何阅读的人都有用。

表结构是一样的,但是我为产品表和地区表都制作了相应的表格。在表单 AfterInsert 事件上,我有以下代码:

Region table:

Private Sub Form_AfterInsert()

Dim varRegion As String
Dim strSQL As String

varRegion = Me![code]
strSQL = "INSERT INTO master([region], [product]) SELECT '" & varRegion & "', & _
          [code] FROM product;"

    DoCmd.RunSQL strSQL

End Sub

Product table:

Private Sub Form_AfterInsert()

Dim varProduct As String
Dim strSQL As String

varProduct = Me![code]
strSQL = "INSERT INTO master([region], [product]) SELECT [code], & _ 
          '" & varProduct & "' FROM region;"

    DoCmd.RunSQL strSQL

End Sub

EDIT: 对此事进行了更多研究,我发现这是您需要用于触发器的代码如果您使用 SQL Server,如果您不想使用客户端设置。

显然,在 SQL Server 中,当您想要获取插入行的值时,您需要引用一个名为“inserted”的隐藏表。查看此链接以获取更多信息:DML 触发器的多行注意事项

太棒了!

产品表:

-- Trigger statement
CREATE TRIGGER "name-of-trigger"
ON producttable
FOR INSERT AS

    -- Insert statement
INSERT INTO mastertable ([region],[product])
SELECT regiontable.[code], inserted.[code]
FROM regiontable, inserted;

地区表:

-- Trigger statement
CREATE TRIGGER "name-of-trigger"
ON regiontable
FOR INSERT AS

    -- Insert statement
INSERT INTO mastertable ([product],[region])
SELECT producttable.[code], inserted.[code]
FROM producttable, inserted;

Alright, thank you for your answers and suggestions.

I am working with an access project connected to MsSQL database. I tried to solve this by using a table trigger, but none of the suggestions has done the trick for me. Therefore I decided to solve this on the client side with VBA code instead.

It is probably not the proper way to solve it, but it might be useful to know for anyone reading.

The table structure is the same, but I have made a corresponding form for both the product and region table. On the forms AfterInsert event I have the following code:

Region table:

Private Sub Form_AfterInsert()

Dim varRegion As String
Dim strSQL As String

varRegion = Me![code]
strSQL = "INSERT INTO master([region], [product]) SELECT '" & varRegion & "', & _
          [code] FROM product;"

    DoCmd.RunSQL strSQL

End Sub

Product table:

Private Sub Form_AfterInsert()

Dim varProduct As String
Dim strSQL As String

varProduct = Me![code]
strSQL = "INSERT INTO master([region], [product]) SELECT [code], & _ 
          '" & varProduct & "' FROM region;"

    DoCmd.RunSQL strSQL

End Sub

EDIT: Having research the matter a little bit more, I found that this is the code you need to be using for the trigger if you are using SQL Server, if you don't want to use the client side setup.

Apparently, in SQL Server you need to reference a hidden table called "inserted" when you want to get the values of the inserted row. View this link for more info: Multirow Considerations for DML Triggers

Great!

Product table:

-- Trigger statement
CREATE TRIGGER "name-of-trigger"
ON producttable
FOR INSERT AS

    -- Insert statement
INSERT INTO mastertable ([region],[product])
SELECT regiontable.[code], inserted.[code]
FROM regiontable, inserted;

Region table:

-- Trigger statement
CREATE TRIGGER "name-of-trigger"
ON regiontable
FOR INSERT AS

    -- Insert statement
INSERT INTO mastertable ([product],[region])
SELECT producttable.[code], inserted.[code]
FROM producttable, inserted;
梦明 2024-09-03 06:41:48

您可以在运行时轻松构建“主表”:

SELECT  *
FROM    regiontable
CROSS JOIN
        producttable

它将更加高效,因为与物化主表不同,两个表都可能适合缓存。

如果由于某种原因您需要将其具体化,那么只需在两个表上编写触发器即可:

INSERT
INTO    mastertable
SELECT  r.code, NEW.code
FROM    regiontable t

mastertable

INSERT
INTO    mastertable
SELECT  NEW.code, p.code
FROM    producttable p

producttable 上。

You can easily build your "master table" in runtime:

SELECT  *
FROM    regiontable
CROSS JOIN
        producttable

It will be more efficient, since, unlike the materialized master table, both tables will probably fit into the cache.

If for some reason you'll need to have it materialized, then just write the triggers on both tables:

INSERT
INTO    mastertable
SELECT  r.code, NEW.code
FROM    regiontable t

on mastertable, and

INSERT
INTO    mastertable
SELECT  NEW.code, p.code
FROM    producttable p

on producttable.

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