Query to determine if columns are combined to create a unique key

I am trying to determine if a set of three columns in a table in Oracle will constitute a unique key and can be used in a 1: 1 ratio.

If I run this query and the keys are a unique combination, I should not see count > 1, right?

 select count(*) from my_table t group by ta, tb, tc 

Is there a better / alternative way to make this determination?

+4
source share
2 answers

Use the HAVING to easily identify duplicates.

 select ta, tb, tc, count(1) from my_table t group by ta, tb, tc having count(1) > 1; 
+9
source

If the table has a decent amount of data, it might be easier to make

 SELECT ta, tb, tc, count(*) FROM my_table t GROUP BY ta, tb, tc HAVING COUNT(*) > 1 

If this query returns 0 rows, the three columns (currently) are unique. If this query returns 1 or more rows, you will know which values ​​are duplicated.

Of course, if you find that the three columns are currently unique, you need to create a unique constraint if you intend to use this fact.

0
source

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


All Articles