简体   繁体   English

SQL Server-一对一关系最佳实践

[英]SQL Server - 1 to 1 relationship best practice

I have a table called Station with many fields, one of the fields is - StationPrice. 我有一个名为Station的表,其中包含许多字段,其中一个字段是-StationPrice。

I want to hold more information about the payment process such as - currency, paymentStatus and etc (somewhere like 10 fields). 我想保存有关付款过程的更多信息,例如-货币,paymentStatus等(约10个字段)。 My question is if I need to expand the current table - Station with the new fields or to have a field called - StationPriceId that will be a foreign key to another table called StationPrices which will store all the information about the price related to that station. 我的问题是我是否需要扩展当前表-带有新字段的Station或具有一个名为-StationPriceId的字段,该字段将成为另一个名为StationPrices的表的外键,该表将存储与该站有关的价格的所有信息。

The answer to your question is 1 of the most popular answers in DB world - it depends . 您的问题的答案是DB世界上最流行的答案之一- 这取决于 It might look nicer if this info is split into 2 different tables, however you need to understand that if you split it then you'll need to 2 inserts instead of 1, the same with updated and deletes. 如果将此信息拆分为2个不同的表,则看起来会更好,但是,您需要了解,如果将其拆分,则将需要2次插入而不是1次插入,与update和Deletes相同。 Moreover you'll need to JOIN this table every time you'll need this data. 此外,您每次需要此数据时都需要联接该表。 I would go with single table first and then moved it to the separate column when the specific need come up. 我将首先使用单个表,然后在出现特定需求时将其移至单独的列。

From other side of view, if this data will be rarely accessed and JOIN/DELETE/INSERT overhead will be minimal then it is OK to move it. 从另一方面来看,如果很少访问该数据并且JOIN / DELETE / INSERT开销很小,那么可以移动它。

My question is if I need to expand the current table - Station with the new fields or to have a field called - StationPriceId that will be a foreign key to another table called StationPrices which will store all the information about the price related to that station.

Yes it is better to use two tables, Based on information you had provided - it is better to use a separate table. 是的,根据您提供的信息,最好使用两个表-最好使用单独的表。

  1. You might want to maintain a price change history 您可能要保留价格变动记录
  2. Hence if you maintain a separate table, you can make earlier price as Active = false and enter a new price for the particular station 因此,如果您维护一个单独的表,则可以将较早的价格设为Active = false并为该特定车站输入新价格

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

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