2010-03-27 47 views
2

嗨,大家好我正在我的基於Web的訂購系統上工作,我們希望爲我們的每個訂單保持一種任務歷史記錄。從某種意義上說,我們希望記錄誰在某個訂單上做了什麼,例如可以說訂單已經輸入 - 我們想知道訂單是否被示例承認。或者可以說,有人跟進的順序 - 等表設計問題 - 我應該創建單獨的字段或存儲爲blob

認爲有很多情況是這樣每個訂單這將是明智的創建上的線路架構:

Orders 
ID - title - description - date - is_ack - is_follow - ack_by ..... 

,加起來到很多領域 - 另一方面,我可以有一個名爲「歷史」的LongText字段,並填充一個包含所有信息的序列化對象。

但是在後一種情況下,我無法運行查詢來讓所有未被確認的訂單以及類似的東西。隨着時間的需求會改變,我將被要求修改它以允許更詳細的跟蹤,這就是爲什麼我需要設置一種可以擴展的方式,但我不想在SQL方面受到限制太多了。

編輯===================

所以斑想法有問題,然後:(但什麼是我在這方面的選擇。其實我都希望。管理是什麼與訂單那張歷史一樣,如果有人有:

  • 已確認附一封電子郵件給
  • 爲了完成一個任務的順序
  • 跟進
  • 的訂單的訂貨
  • 打了電話etc

回答

0

boolean字段,你在這個例子中顯示通常是不夠的。我建議你創建其他表:

Status (ID, Value) - essentially enumeration of possible values: Received, Acknowledged, Dispatched, ... 
OrderStatus (ID, StatusID (FK to Status), AuditBy, AuditAt, Comment) 

您可以完全避免其Status表,只是在OrderStatus表中的列Status。但在這種情況下,至少應將可能的值限制在您擁有的列表中。

通過這種方式,您可以更好地審覈發生了什麼,何時以及由誰發出的審計追蹤。

1

將邏輯上截然不同的信息混合在一起幾乎總是一場災難。減少實地計數本身並不是一個目標。

0

很少需要將信息存儲爲blob或xml。

當你這樣做時,你開始失去你的數據庫引擎提供的能力來有效地進行查詢。

然後,您的查詢將不得不與應用程序或專門的db代碼一起處理,這似乎會使事情變得複雜。

而只是想着失去的能力,以索引列有我在結束X-毛)

1

沒有真正想過這個通過,但你可以做這樣的事情http://pastie.org/889605

從pastie一些片段:

drop table if exists order_events; 
create table order_events(
event_id int unsigned not null auto_increment primary key, 
order_id int unsigned not null, 
event_type_id tinyint unsigned not null, 
status_id tinyint not null, 
emp_id smallint unsigned not null, 
event_date datetime not null, 
key order_events_order_idx(order_id), 
key order_events_emp_idx(emp_id) 
)engine=innodb; 


drop table if exists event_type; 
create table event_type(
event_type_id tinyint unsigned not null auto_increment primary key, 
name varchar(255) 
)engine=innodb; 

insert into event_type (name) values ('new order event'),('order status event'); 

create trigger orders_after_upd_trig after update on orders 
for each row 
begin 
    -- change of status 
    if new.status_id <> old.status_id then 
    insert into order_events (order_id,event_type_id,status_id,event_date,emp_id) 
    values (old.order_id, 2, new.status_id, now(), new.updated_emp_id); 
    end if; 
end# 
相關問題