zer*_*oef 3 sql sql-server database-design many-to-many
我接管了一个具有SQL后端的应用程序.有多个表,但我关注的两个是这些:
QAProfile
---------
ProfileID <pk> -int
ProfileName
SecurityGroups -varchar(max)
SecurityGroups
--------------
GroupID <pk> -int
GroupName
Run Code Online (Sandbox Code Playgroud)
我的问题是SecurityGroups字段是一个逗号分隔的GroupID值列表.
所以Profile表看起来像这样:
--------------------------------------------
| ProfileID | ProfileName | SecurityGroups |
--------------------------------------------
| 1 | foo | ,1,13,34,56, |
--------------------------------------------
| 2 | bar | ,4,13,29,56, |
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)
配置文件可以具有多个安全组,安全组可以在多个配置文件上
关于如何重新设计这个的任何建议?
Dan*_*llo 10
你正在寻找重新设计它是好的,因为一般来说逗号分隔的列表不属于SQL数据库.
它看起来像一个简单的联结表可以替换表的SecurityGroups列QAProfile:
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)
);
Run Code Online (Sandbox Code Playgroud)