2013-02-12 84 views
1

我有表發票 - PK是年份+ document_type_id +數字(這是當前編號,我無法更改它)。因此,該數據是這樣的:兩種類型的發票編號(兩個表,0..1關係)

year document_type_id number 
2013 351    1 
2013 351    2 
2013 352    1 

現在,我需要開發第二類編號的 - 表invoices_2 - PK是一年+ market_id + cash_register_id +數字(這是編號爲一些發票是由禁止法律),FK是invoices_year + invoices_document_type_id + invoices_number。

發票 - invoice_2必須是1 - > 0..1的關係。

的問題是,在invoices_2表我可以有這個(我想消除 - 使用一些PK + FK組合):

year market_id cash_register_id number invoices_year inovices_document_type invoices_number 
2013  1    1   1   2013   351     1 
2013  1    1   2   2013   351     1 

正如你所看到的,發票2013-351- 1使用可以在invoices_2表中增加1次以上,這是必須禁止的。

http://www.sqlfiddle.com/#!3/6b42c/1

+3

在'invoices_2(invoices_year inovices_document_type invoices_number)'上創建唯一約束。 – 2013-02-12 13:45:04

回答

0

這將是更好地把第二編號系統在其自己的表。

Invoice 
------- 
year 
document_type_id 
number 
invoice_2_fk 
... 


Invoice_2 
--------- 
invoice_2 _id 
year 
market_id 
cash_register_id 
number 

發票表中的發票2外鍵可爲空。當它爲空時,沒有發票2.當它是一個有效的id時,那麼就有一張發票2.這是一對一/零關係。