SQL table creation help

I took an application that has an SQL server. There are a few tables, but two of which I am worried about are the following:

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


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

My problem is that the SecurityGroups field is a list of GroupID values, separated by commas.

So, the Profile table is as follows:

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

A profile can have multiple security groups, and a security group can be in multiple profiles

Any suggestions on how to do this?

+3
source share
6 answers

It’s good that you want to rebuild this, because in general, comma-delimited lists do not apply to SQL databases.

, 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)
);
+10

, :

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

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

QASecurityGroups
----------------
ProfileID<pk>
GroupID <pk>
+4
Create Table QAProfileSecurityGroup (
 ProfileID int,
 GroupID int,
 PRIMARY KEY ( ProfileID, GroupID)
)
+2
  • ProfileSecurityGroups
  • SecurityGroups Profile.

alt text

+2

, , :

ProfileSecurityGroups
---------------------
Id, <pk> -int
ProfileId <fk>,
SecurityGroupId <fk>
+1
  • ​​ ProfileToGroup:

    ProfileID GroupID

  • SQL script, "/".
  • , SecurityGroups QAProfile
+1

Source: https://habr.com/ru/post/1768108/


All Articles