2012-12-20 80 views
0

我有兩個表TABLE_ACCOUNTTABLE_ACTYPE在sql server 2008中給外鍵而不是列外鍵

TABLE_ACCOUNT有列

(CODE, NAME, TRADE, CATGORY, ACTYPE) 

TABLE_ACTYPE有列

(CODE, NAME, ITYPE) 

TABLE_ACTYPE的數據是:

CODE  NAME  ITYPE 
----------------------- 
1  TRADE1 1 
1  CAT1  2 
1  ACT1  3 

此表的主鍵是CODE, TYPE

數據在TABLE_ACCOUNT是:

CODE, NAME, TRADE, CATEGORY, ACTYPE 
----------------------------------- 
1  Name1 1  1   1 

我要創建3個外鍵來table_account

ALTER TABLE TABLE_ACCOUNT WITH CHECK 
ADD CONSTRAINT FK_TABLE_ACCOUNT_TABLE_ACTYPE_TRADE 
FOREIGN KEY ([TRADE,1]) REFERENCES [TABLE_ACTYPE] (CODE, ITYPE) 

ALTER TABLE TABLE_ACCOUNT WITH CHECK 
ADD CONSTRAINT FK_TABLE_ACCOUNT_TABLE_ACTYPE_CAT 
FOREIGN KEY ([TRADE,2]) REFERENCES [TABLE_ACTYPE] (CODE, ITYPE) 

ALTER TABLE TABLE_ACCOUNT WITH CHECK 
ADD CONSTRAINT FK_TABLE_ACCOUNT_TABLE_ACTYPE_ACTYPE 
FOREIGN KEY ([TRADE,3]) REFERENCES [TABLE_ACTYPE] (CODE, ITYPE) 

這可能嗎?

回答

3

號         

+0

查找到父/子類型,並作出超表,您可以FK一次。 – ErikE

+0

我已將TABLE_ACCOUNT NAME添加到TABLE(默認值爲1的CODETRADE),(默認值爲2的CODECAT),(默認值爲3的CODEITYPE)然後創建對外鍵:) –