sql - How to find the record that violate unique key constraint? -


assume have 2 tables follows,

table

    +-------+-------+-------+-------+     | col_a | col_b | col_c | col_d |     +-------+-------+-------+-------+     |       |       |       |       |     +-------+-------+-------+-------+ 

table b

    +-------+-------+-------+-------+     | col_a | col_b | col_c | col_d |     +-------+-------+-------+-------+     |       |       |       |       |     +-------+-------+-------+-------+ 

i'm going update table using table b. here conditions

  1. records equal col_a should update in table
  2. records not equal col_a should inserted table a
  3. table has unique key constraint (col_b,col_c,col_d)

problem when updating data in table a, unique key constraint fails records. question how can identify records violate unique key constraint using query. (i don't have access logs)

if don't have unique key on col_b, col_c, col_d of table_b, result in violation when copying over. can identify problematic rows query this:

select col_b, col_c, col_d   table_b   group col_b, col_c, col_d   having count(*) > 1 

a similar query can run on table_a joined table_b, specific queries run depend on columns updated in table_a. insert case, useful technique might use minus between table_a , proposed inserted row.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -