I am somewhat of a novice to SQL, but have been playing around with it using SQLFiddle to get better at it. What if you wanted to find and list all the tables that have common columns or columns that are shared across the different tables (e.g. table 1, 2, 3, & 4 have a customer field, while table 2 & 4 have a student field? How would you do that with MySQL? Lets say you have 100+ tables and not 2.
E.g.
Table 1:
ID | Customer | Pet |
Table 2:
ID | Customer | Food | Student |
Table 3:
ID | Customer | Activity |
Table 4:
ID | Customer | Cost | Student
Expected Outcome:
Table_NAME | COLUMN_NAME
Table 1, Table 2, Table 3, Table 4 | ID
Table 1, Table 2, Table 3, Table 4 | CUSTOMER
Table 2, Table 4 | Student
I tried both the below and did not give me what I was looking for:
select Table_NAME, COLUMN_NAME, Count(*)
from INFORMATION_SCHEMA.columns
GROUP BY Table_NAME
HAVING COUNT(*) > 1
SELECT Table_Name, Column_Name
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME IN
( SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.columns
GROUP BY TABLE_NAME
HAVING COUNT(*) > 1
)
ORDER BY TABLE_NAME