2010-10-05 102 views
3

我接管了一個具有SQL後端的應用程序。有多個表,但我很擔心這兩個是這些:SQL表設計幫助

QAProfile 
--------- 
ProfileID <pk> -int 
ProfileName 
SecurityGroups -varchar(max) 


SecurityGroups 
-------------- 
GroupID <pk> -int 
GroupName 

我的問題是,該SecurityGroups場羣ID值的逗號分隔的列表。

所以配置表如下:

-------------------------------------------- 
| ProfileID | ProfileName | SecurityGroups | 
-------------------------------------------- 
| 1  |  foo  | ,1,13,34,56, | 
-------------------------------------------- 
| 2  |  bar  | ,4,13,29,56, | 
-------------------------------------------- 

配置文件可以有muliple安全組和安全組可以在muliple型材

就如何重新設計這個有什麼建議?

回答

10

這是很好的,你是希望對其進行重新設計,因爲通常以逗號分隔的列表不屬於SQL數據庫。

它看起來像一個簡單的junction table可以代替QAProfile表的SecurityGroups列:

CREATE TABLE SecurityGroups_Profiles (
    GroupID  int NOT NULL, 
    ProfileID int NOT NULL, 
    PRIMARY KEY (GroupID, ProfileID), 
    FOREIGN KEY (GroupID) REFERENCES SecurityGroups (GroupID), 
    FOREIGN KEY (ProfileID) REFERENCES QAProfile (ProfileID) 
); 
+0

你的更好,你甚至添加了FK的!我想他們可以自己做。 – Sage 2010-10-05 18:39:48

+0

我不同意這種說法:「逗號分隔的列表不屬於SQL數據庫」似乎有點教條 - 當然有這種策略對於性能的原因是非常有利的。 – James 2010-10-05 18:41:20

+0

@詹姆斯:你有一點。對這句話增加「一般」更準確。 – 2010-10-05 18:42:06

1

是小菜一碟,你可以只創建一個結表是這樣的:

ProfileSecurityGroups 
--------------------- 
Id, <pk> -int 
ProfileId <fk>, 
SecurityGroupId <fk> 
4

如果是我,我會做這樣的:

QAProfile 
--------- 
ProfileID <pk> -int 
ProfileName 

SecurityGroups 
-------------- 
GroupID <pk> -int 
GroupName 

QASecurityGroups 
---------------- 
ProfileID<pk> 
GroupID <pk> 
2
Create Table QAProfileSecurityGroup (
ProfileID int, 
GroupID int, 
PRIMARY KEY (ProfileID, GroupID) 
) 
1
  • 新表ProfileToGroup應增加:

    簡檔 組ID

  • 你將需要編寫一個SQL腳本來解析出組的列表並將記錄插入到每個Profile/Group組合的新表中。
  • 最後,在QAProfile表SecurityGroups列應被刪除
2
  • 添加表ProfileSecurityGroups
  • 解析該字符串的每個配置文件,並添加對錶
  • 變化到使用該應用層新結構
  • 從Profile表中刪除SecurityGroups列

alt text

+0

頂部的方式必須給你+1掀起一個圖! – James 2010-10-05 19:03:45