Effective way to update SQL relationship table

Let's say that I have three properly normalized tables. One of the people, one of the qualifications and one person comparing people with the qualification:

People:

id | Name
----------
1  | Alice
2  | Bob

degrees:

id | Name
---------
1  | PhD
2  | MA

People to degree:

person_id | degree_id
---------------------
1         | 2         # Alice has an MA
2         | 1         # Bob has a PhD

So, I have to update this mapping through my web interface. (I made a mistake. Bob has a bachelor's degree, not a Ph.D., and Alice just got her B Eng.)

There are four possible states of these one-to-one relationships:

  • was true before, should now be false
  • was false before should now be true
  • was true before, must stay true
  • was false before, should remain false

, , , , : " BA ? , ". " ? " ..

?

, - , .

1: oneday , - , , INSERT - .

2: potatopeelings , , submit.

+3
2

, UPDATE - DELETE, INSERT (, SQL Server inserted deleted, updated). , , .. DELETE ( ) , INSERT .

, SQL MERGE, , (SQL Server 2008 ).

+2

, checkbox (1. Ismail )

           MA      PhD    
Alice      x 
Bob                 x

x . script . INSERT DELETE " " MERGE ( Ismail)

BEGIN TRAN
INSERT query
DELETE query
COMMIT

INSERT ( DELETE) , . INSERT (2,2), DELETE - (2,1).

0

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


All Articles