I have a need to know which column names are used on more than one table of my database.
I’ve got this query as a start, which gives me a frequency table of column names across the database:
SELECT COLUMN_NAME, count(COLUMN_NAME) count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='dbName'
GROUP BY COLUMN_NAME
ORDER BY count DESC;
However, I’d like the query to also do the following:
-
Limit to only those records where
count>1
(I tried addingAND count>1
to theWHERE
clause, but I got the errorError Code: 1054. Unknown column 'count' in 'where clause'
) -
Show the names of all the tables on which each column appears (this could entail multiple rows per column name, one for each table name). If it’s easier, I can leave off the
count
column, as long as condition 1 is met.
Note: This is similar to this question, but I don’t want to search for a specific column; I want to find ALL column names that are repeated.
2
Answers
Based on Ergest Basha's answer I came up with the following query, which meets both conditions (lists the non-unique columns AND shows which tables they appear on):
This will give which tables are used more than once
If you want to know how many the columns are repeated and which one is repeated the most use:
Edit. I missed the second requirements , so you could use below query to find the table names as well: