简体   繁体   English

更新数据库结构-新表或添加字段

[英]updating db structure - new table or adding a field

This might sound like a silly question but here it is; 这听起来像是一个愚蠢的问题,但事实确实如此。 I am sure it has happened to anyone around here, you build a web app with a db structure per specifications (php/mysql), but then the specs change slightly and you need to make the change in the db to reflect it, here is a short example: 我敢肯定,这里的任何人都发生了这种情况,您按照规范(php / mysql)构建了一个具有db结构的Web应用程序,但是随后规范略有变化,您需要在db中进行更改以反映出来,这是一个简短的例子:

Order table
->order id
->user id
->closed
->timestamp

but because the orders are paid in different currency than in the one, which is quoted in the db, I need to add the field exchange rate , which is only checked and known when closing the order, not upon insertion of the record. 但是由于订单的支付货币与数据库中引用的货币不同,因此我需要添加字段exchange rate ,该exchange rate仅在关闭订单时才检查并知道,而不是在插入记录时才知道。 Thus I can either add the new field to the current table, and leave it null/blank when inserting, and then update when necessary; 因此,我可以将新字段添加到当前表中,并在插入时将其保留为空/空白,然后在必要时进行更新; or I can create a new table with the following structure: 或者我可以使用以下结构创建一个新表:

Order exchange rates
->exchange id
->order id
->exchange rate

Although I believe that the letter is better because it is a less intrusive change, and won't affect the rest of the application functionality, you could end up with insane amount of joined queries to get all the information necessary. 尽管我相信这封信会更好,因为它是一种不太麻烦的更改,并且不会影响应用程序的其余功能,但最终可能会出现疯狂的连接查询,以获取所有必要的信息。 On the other hand the former approach could mess up some other queries you have in the db, but it is definitely more practical and also logical in terms of the overall db structure. 另一方面,前一种方法可能会使您在数据库中存在的其他查询混乱,但是从整体数据库结构的角度来看,这绝对是更实用且合乎逻辑的。 Also I don't think that it is a good practice to use the structure of insert null and update later, but that might be just my lonely opinion... Thus I would like to ask what do you think is the preferable approach. 另外,我认为使用insert null的结构并在以后进行更新不是一个好习惯,但这可能只是我的寂寞意见。因此,我想问一下您认为哪种方法更可取。

I'm thinking of another alternative. 我在考虑另一种选择。 Setup an exchange rate table like: 设置汇率表,例如:

create table exchange_rate(
   cur_code_from varchar2(3)  not null
  ,cur_code_to   varchar2(3)  not null
  ,valid_from    date         not null
  ,valid_to      date         not null
  ,rate          number(20,6) not null
);

alter table exchange_rate 
  add constraint exchange_rate_pk 
      primary key(cur_code_from, cur_code_to, valid_from);

The table should hold data that looks something like: 该表应包含类似于以下内容的数据:

cur_code_from   cur_code_to valid_from  valid_to   rate
=============  =========== ==========  ========    ====
    EUR           EUR      2014-01-01   9999-12-31  1
    EUR           USD      2014-01-01   9999-12-31  1,311702
    EUR           SEK      2014-01-01   2014-03-30  8,808322
    EUR           SEK      2014-04-01   9999-12-31  8,658084
    EUR           GBP      2014-01-01   9999-12-31  0,842865
    EUR           PLN      2014-01-01   9999-12-31  4,211555

Note the special case when you convert from and to the same currency. 从和转换为相同货币时,请注意特殊情况。 From a normalization perspective, you don't need valid_to since it can be computed from the next valid_from , but from a practical point of view, it's easier to work with a valid-to-date than using a sub-query every time. 从规范化的角度来看,您不需要valid_to因为可以从下一个valid_from进行计算,但是从实际的角度来看,使用有效日期比每次使用子查询都容易。

Then, to convert into the customers currency you would join with this table: 然后,要转换成客户货币,您可以使用此表:

select o.order_value * x.rate as value_in_customer_currency
  from orders o
  join exchange_rate_t x on(
       x.cur_code_from = 'EUR' -- Your- default currency here
   and x.cur_code_to   = 'SEK' -- The customers currency here
   and o.order_close_date between x.valid_from and x.valid_to
  )
 where o.order_id = 1234;

Here I have used the rates valid as of the order_close_date . 在这里,我使用了自order_close_date有效的order_close_date So if you have two orders, one with a close date of 2014-02-01, then it would pick up a different rate than an order with a close date of 2014-04-05. 因此,如果您有两个订单,一个订单的截止日期为2014-02-01,那么它将获得与截止日期为2014-04-05的订单不同的汇率。

I think you just need to add exchange_rate_id in the order table and create a look up table Exchange_Rates with columns ex_rate_id , description , deleted , created_date . 我认为您只需要在订单表中添加exchange_rate_id并使用表ex_rate_iddescriptiondeletedcreated_date创建一个表Exchange_Rates

So when an order closes you just need to update the exchange_rate_id column in order table with id and later on you can create a join with the look up table to pull records. 因此,当订单关闭时,您只需要用id更新订单表中的exchange_rate_id列,以后便可以使用查找表创建联接以提取记录。

Keep in mind that 请记住

  1. one order have only one currency upon closing. 一个订单在关闭时只有一种货币。
  2. one currency can be updated against one or many orders 可以针对一个或多个订单更新一种货币

It is a one to many relationship, so i don't think that you have to make a separate table for that. 这是一对多的关系,所以我认为您不必为此创建单独的表。 If you do so I think that will consider in extra normalization. 如果您这样做,我认为可以考虑进行额外的标准化。

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

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