Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all fields are unique in Oracle?

Tags:

sql

unique

oracle

How to check if all fields are unique in Oracle?

like image 687
Burjua Avatar asked Oct 24 '10 20:10

Burjua


People also ask

How do you know if a column is unique?

select count(distinct column_name), count(column_name) from table_name; If the # of unique values is equal to the total # of values, then all values are unique.

How do I find unique columns in a table?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.


2 Answers

SELECT myColumn, COUNT(*)
FROM myTable
GROUP BY myColumn
HAVING COUNT(*) > 1

This will return to you all myColumn values along with the number of their occurence if their number of occurences is higher than one (i.e. they are not unique).

If the result of this query is empty, then you have unique values in this column.

like image 145
eumiro Avatar answered Sep 22 '22 19:09

eumiro


An easy way to do this is to analyze the table using DBMS_STATS. After you do, you can look at dba_tables... Look at the num_rows column. The look at dab_tab_columns. Compare the num_distinct for each column to the number of rows. This is a round about way of doing what you want without performing a full table scan if you are worried about affecting a production system on a huge table. If you want direct results, do what the the others suggest by running the query against the table with a group by.

like image 40
erbsock Avatar answered Sep 24 '22 19:09

erbsock