简体   繁体   English

将外键与SQL Server中的详细信息表关联的最佳方法是什么?

[英]What is the best way to associate the foreign key to the details table in SQL Server?

I'm trying to aggregate details and creating a header record for them. 我正在尝试汇总详细信息并为其创建标头记录。 What is the best way to assign the foreign key back into the details? 将外键重新分配到详细信息中的最佳方法是什么?

The details table will have an ID(identity) and HeaderID (foreign key to headers table). 详细信息表将具有ID(标识)和HeaderID(标头表的外键)。 After aggregating the details and inserting into the Header table, how do I associate the Header ID back into the individual details? 汇总详细信息并将其插入到Header表后,如何将Header ID重新关联到各个详细信息?

I thought about using triggers to update the Details table with the @@Identity from the Header ID. 我考虑过使用触发器从标头ID中使用@@ Identity更新Details表。 However, since there is no association between the two just yet (that's what I'm trying to build), I'm not sure what to have in the WHERE clause to do the update. 但是,由于两者之间还没有关联(这就是我要构建的关联),所以我不确定WHERE子句中包含哪些内容来进行更新。

The below query should give you some sample data. 以下查询应为您提供一些示例数据。 If you execute it, where the NULL values are in the #OrderLine table is where I'm having an issue associating the OrderId back. 如果执行它,那么#OrderLine表中的NULL值就是我将OrderId关联回去的地方。

Create Table #Order
(OrderId int identity primary key,
TypeId char(1),
Total decimal(5,2)
)

Create Table #OrderLine
(OrderLineId int identity,
OrderId int constraint FK_OrderId foreign key (OrderId) references [#Order](OrderId),
LineItem int,
TypeId char(1),
Item varchar(30),
Price decimal (5,2)
)

Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Tennis Racket', 120)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Red Dress', 80)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Basketball', 30)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Dress Shirt', 60)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Pingpong Balls', 10)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Soccer Ball', 25)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Shorts', 20)

Insert into #Order(TypeId, Total)
Select  #OrderLine.TypeId
        ,SUM(#OrderLine.Price)
From    #OrderLine
Group by #OrderLine.TypeId

Select * From #Order
Select * From #OrderLine

Drop Table #Order
Drop Table #OrderLine

I think I found one way to do. 我想我找到了一种方法。 For the LineItem, I used ROW_NUMBER() and PARTITION. 对于LineItem,我使用了ROW_NUMBER()和PARTITION。 For the OrderId, I just updated the details based on the aggregation criteria for where the OrderId is NULL. 对于OrderId,我只是根据OrderId为NULL的聚合条件更新了详细信息。

If anyone has a better suggestion, please let me know. 如果有人有更好的建议,请告诉我。

Create Table #Order
(OrderId int identity primary key,
TypeId char(1),
Total decimal(5,2)
)

Create Table #OrderLine
(OrderLineId int identity,
OrderId int constraint FK_OrderId foreign key (OrderId) references [#Order](OrderId),
LineItem int,
TypeId char(1),
Item varchar(30),
Price decimal (5,2)
)

Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Tennis Racket', 120)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Red Dress', 80)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Basketball', 30)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Dress Shirt', 60)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Pingpong Balls', 10)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Soccer Ball', 25)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Shorts', 20)

Insert into #Order(TypeId, Total)
Select  #OrderLine.TypeId
        ,SUM(#OrderLine.Price)
From    #OrderLine
Group by #OrderLine.TypeId

--Update OrderId back to #OrderLine
Update #OrderLine
Set OrderId = #Order.OrderId
From    #Order
Where   #Order.TypeId = #Orderline.TypeId
and     #OrderLine.OrderId is null

--Assign the LineItem per Order
Update #OrderLine
Set LineItem = TT.LineItemId
From    #OrderLine as T
join    (Select ROW_NUMBER() OVER (PARTITION BY OrderId, TypeId ORDER BY OrderLineId) as LineItemId
            ,OrderLineId
            ,OrderId
            ,TypeId
            From #OrderLine) as TT on TT.OrderLineId = T.OrderLineId
Where   T.LineItem is null

Select * From #OrderLine
Select * From #Order

Drop Table #Order
Drop Table #OrderLine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM