How to display the column names which have only unique non-null values in MySQL table?

21 views Asked by At

I have a table in which I want to show the names of columns which contain unique values and aren't null. In other words I want to display the candidate keys from the table. Note that while creating the table, the UNIQUE or NOT NULL constraint may or may not have been provided for the required columns.

My thought process was to somehow check if `COUNT(DISTINCT columnn_name) = COUNT(*)' for every column in the table, and print the column names for which the condition is true. But I don't know how to write this in a query. Any help is appreciated, thanks!

EDIT: This is a homework question. The question just showed us a table and told us to identify potential candidate keys in the table using a query. The reason I want to do it dynamically/automatically is that if I do the count=count for every column and then manually see which ones it is true for, I might as well just see which columns are distinct and not null, which makes this approach seem wrong.

0

There are 0 answers