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
- records equal
col_a
should update in table - records not equal
col_a
should inserted table a - 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
Post a Comment