查询以确定列是否组合以创建唯一键

时间:2021-05-21 04:26:25

I'm trying to determine if a set of three columns on a table in Oracle would constitute a unique key and could be used in a 1:1 relationship.

我正在尝试确定Oracle中表上的一组三列是否构成唯一键,并且可以以1:1的关系使用。

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

如果我运行此查询,并且键是唯一的组合,我不应该看到计数> 1,对吗?

select count(*) from my_table t    
group by t.a, t.b, t.c

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

是否有更好/替代的方法来做出这个决定?

2 个解决方案

#1


10  

Use the HAVING clause to easily identify duplicates.

使用HAVING子句可以轻松识别重复项。

select t.a, t.b, t.c, count(1) 
from my_table t    
group by t.a, t.b, t.c 
having count(1) > 1;

#2


0  

If the table has a decent amount of data, it's probably easier to do

如果表具有相当数量的数据,则可能更容易

SELECT t.a, t.b, t.c, count(*)
  FROM my_table t
 GROUP BY t.a, t.b, t.c
HAVING COUNT(*) > 1

If that query returns 0 rows, the three columns are (currently) unique. If that query returns 1 or more rows, you'll know which values are duplicated.

如果该查询返回0行,则这三列(当前)是唯一的。如果该查询返回1行或更多行,您将知道哪些值是重复的。

Of course, if you find that the three columns are currently unique, you'll want to create a unique constraint if you intend to make use of that fact.

当然,如果您发现这三列当前是唯一的,那么如果您打算利用这一事实,则需要创建一个唯一约束。

#1


10  

Use the HAVING clause to easily identify duplicates.

使用HAVING子句可以轻松识别重复项。

select t.a, t.b, t.c, count(1) 
from my_table t    
group by t.a, t.b, t.c 
having count(1) > 1;

#2


0  

If the table has a decent amount of data, it's probably easier to do

如果表具有相当数量的数据,则可能更容易

SELECT t.a, t.b, t.c, count(*)
  FROM my_table t
 GROUP BY t.a, t.b, t.c
HAVING COUNT(*) > 1

If that query returns 0 rows, the three columns are (currently) unique. If that query returns 1 or more rows, you'll know which values are duplicated.

如果该查询返回0行,则这三列(当前)是唯一的。如果该查询返回1行或更多行,您将知道哪些值是重复的。

Of course, if you find that the three columns are currently unique, you'll want to create a unique constraint if you intend to make use of that fact.

当然,如果您发现这三列当前是唯一的,那么如果您打算利用这一事实,则需要创建一个唯一约束。