Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Updating PK with same value - effect on CASCADE UPDATE

$
0
0

Hello,

I would like to understand how sql server 2008 deals with cascade updates

For example I have
Parent table: Employee with column Id as varchar(20) primary key
Child table with IdEmployee as varchar(20) foreign key
I set up Cascade Update for those two tables, meaning any change to primary key in Employee table will cause update in child table rows that match affecting Id

Scenario 1:

Update Employee
set Id = 'ABC',
Name = 'something new'
where Id = 'CCC'

Result of child table: all rows with foreign key IdEmployee and value of 'CCC' are updated. Expected behavior.

Scenario 2:

Update Employee
set Id = 'ABC',
Name = 'something new 2'
where Id = 'ABC'

This time, i am doing something different. I am beside update of column Name with new value, also update primary key but
with SAME value

Question is: what is going to happen to child rows? Are they ALL going to UPDATE due to CASCADE UPDATE

So far, what i did in order to find solution is:

1. I put an timestamp column in child table that should update each time row gets updated
2. I put a trigger for update event on child table that will write something to some log table

*After I set up those two I ran example like above just to be sure timestamp gets changed as well trigger is being fired

Results of updating PK with same value:
1. Timestamp didnt change
2. Trigger didnt fire

Is this enough to make conclusion that updating primary key with same value ALONG with updating some other columns won't
affect child tables with UPDATE CASCADE ON

Update:
Database is CI AS collation
If i do following

Update Employee
set Id = 'abc',
Name = 'something new'
where Id = 'ABC'

1. Timestamp will change
2. Trigger will fire

Conclusion: Case sensitive is important here!

Thank you very much in advance

Milos

 



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>