This is a standard update problem for any use case and there are multiple ways to solve it.
Lets take your example.
create table Employee
(
EmployeeId int primary key,
Names varchar(100),
skills varchar(200),
Salary numeric(10,2)
)
create table Skill
(
Skillid int primary key,
Names varchar(100)
)
CREATE TABLE EmployeeSkillMapping
( employeeid INT FOREIGN KEY REFERENCES Employee(EmployeeId),
skillid INT FOREIGN KEY REFERENCES skill(Skillid),
)
Now let's add some dummy data
INSERT INTO Employee(employeeid, names, salary)
SELECT 1, 'Test1', 100
insert into skill (skillid, names)
SELECT 1, 'C++'
UNION ALL
SELECT 2, 'C#'
UNION ALL
SELECT 3, 'Java'
UNION ALL
SELECT 4, 'ReactJS'
UNION ALL
SELECT 5, 'NodeJS'
insert into EmployeeSkillMapping(employeeid, skillid)
SELECT 1, 1
UNION ALL
SELECT 1, 2
UNION ALL
SELECT 1, 3
The usecase here is Employee Name "Test" have three skills (C++, C# and Java). Now we need to modify Asif by removing Java skill and add new ReactJS skill.
Usually the interface for doing above modifications is on the left side we have all available skills and at the right side we have assigned skills list.
One elegant approach is to use MERGE. Merge command can Insert/Update/Delete in one transaction. You can call an SP/SQL from code passing all assigned skills list to the SP and the SP will do the Job, may be like this.
MERGE EmployeeSkillMapping AS tgt
USING (
VALUES
(1, 1),
(1, 2),
(1, 4)
) AS src (employeeid, skillid)
ON tgt.employeeid = src.employeeid and tgt.skillid = src.skillid
WHEN NOT MATCHED THEN
INSERT (employeeid, skillid)
VALUES (src.employeeid, src.skillid)
WHEN NOT MATCHED BY SOURCE THEN
DELETE
Another approach is to break it into two parts. One is for Insert and another is for deletion. You already have the previous assigned list while you fetched employee details. Insert case is simple where you know the newly assigned skills are not present by comparing new values with session values. For the delete case, you need to know which skills have been removed by again comparing new values with session values.
Once you have both data sets you can separately execute Insert and delete statements.
This should give you a head start. Good Luck!