简体   繁体   English

将数据从一个表分发到SQL Server中的多个表

[英]distributing data from one table to multiple tables in sql server

I have 3 tables: 我有3张桌子:

Item_Detail -----------------ID, Name Item_Detail ----------------- ID,名称

ItemPurchased_Detail ---QtyPurchased , RateOfPurchase , DiscountReceived ItemPurchased_Detail --- QtyPurchased,RateOfPurchase,DiscountReceived

ItemSold_Detail -----------QtySold , RateofSale , DiscountGiven ItemSold_Detail ----------- QtySold,销售率,DiscountGiven

Also, i have a MAIN TABLE, ITEM_FULL_DETAIL, which contains all the columns from above 3 tables. 另外,我有一个主表,ITEM_FULL_DETAIL,其中包含来自上述3个表的所有列。

i have a winform application, with a single form which contains all the textboxes to insert data in the ITEM_FULL_DETAIL table. 我有一个winform应用程序,具有一个包含所有文本框的表单,用于在ITEM_FULL_DETAIL表中插入数据。 the user would input all the data, and click the SUBMIT button 用户将输入所有数据,然后单击“提交”按钮

i want to insert data first in the MAIN TABLE, and then it should distribute data individually to all the 3 tables. 我想先在主表中插入数据,然后将数据分别分配到所有3个表中。 for this what shall i use? 为此,我应该使用什么? like triggers, porcedures, or views or joins? 例如触发器,过程,视图或联接?

Also, am using the ITEM_FULL_DETAIL table, because i want to protect my actual tables from any loss of data such as in case of power outage. 另外,我正在使用ITEM_FULL_DETAIL表,因为我想保护我的实际表不受任何数据丢失的影响,例如断电的情况。

Shall I use a temporary table in place of ITEM_FULL_DETAIL table or is it fine using the current one only? 我应该使用临时表代替ITEM_FULL_DETAIL表,还是仅使用当前表就可以了?

Is there any other way also? 还有其他办法吗?

您可以使用数据库触发器或在应用程序级别插入所有记录。

You should probably re-think your design: duplicating the same data in different tables is usually a bad idea. 您可能应该重新考虑设计:在不同的表中复制相同的数据通常是一个坏主意。 In this case, you could replace ITEM_FULL_DETAIL with a view, then maintain the data in the underlying tables. 在这种情况下,您可以将ITEM_FULL_DETAIL替换为视图,然后将数据保留在基础表中。 That way you only have one copy of the data, so you don't need to worry about inconsistencies between tables. 这样,您只有一个数据副本,因此您不必担心表之间的不一致。

If you did that then you could either insert new data into the 3 underlying tables in the correct order (probably the best idea) or use an INSTEAD OF trigger on the ITEM_FULL_DETAIL view (more complicated). 如果这样做,则可以按正确的顺序将新数据插入3个基础表中(可能是最好的主意),也可以在ITEM_FULL_DETAIL视图上使用INSTEAD OF触发器(更为复杂)。 The INSERTs can be done using an ORM, ADO.NET, a stored procedure or whatever suits your application design best. 可以使用ORM,ADO.NET,存储过程或最适合您的应用程序设计的任何方式来完成INSERT。

If you do have a good reason for duplicating your data, then it would be helpful if you could share it, because someone may have a better suggestion for that scenario. 如果确实有重复数据的理由,那么可以共享它会很有帮助,因为有人可能会对这种情况有更好的建议。

Also, am using the ITEM_FULL_DETAIL table, because i want to protect my actual tables from any loss of data such as in case of power outage. 另外,我正在使用ITEM_FULL_DETAIL表,因为我想保护我的实际表不受任何数据丢失的影响,例如断电的情况。

..What? ..什么? How do you suppose you are protecting your tables? 您如何保护自己的餐桌? What are you trying to prevent? 您想防止什么? There is absolutely no need to have the ITEM_FULL_DETAIL table if what you are worried about is data integrity. 如果您担心的是数据完整性,则绝对不需要ITEM_FULL_DETAIL表。 You're probably creating a situation in which data integrity can be compromised by using this intermediate table. 您可能正在创建一种情况,其中使用此中间表可能会损害数据完整性。

Are you aware of transactions? 您知道交易吗? Use them. 使用它们。 If two out of three tables are written to, then the power on the client goes off and can't complete the 3rd write, the transaction will fail and the partial data will be rolled back. 如果写入三分之二的表,则客户端的电源将关闭并且无法完成第三次写入,事务将失败并且部分数据将被回滚。

Unless I'm totally missing the point here.. 除非我完全不了解这里的要点。

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

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