Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the specific column (field) name from multiple tables in a database

I have large numbers of tables in my database. I am searching for a column named Country, but don't know which table contains that column. Is there a specific query that will help me to find the name of the table containing this column?

like image 792
S.p Avatar asked Dec 09 '22 21:12

S.p


2 Answers

Yes, you can use INFORMATION_SCHEMA.COLUMNS

SELECT DISTINCT 
   TABLE_NAME 
FROM 
   INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'Country'
like image 87
Paul Alan Taylor Avatar answered Jun 02 '23 09:06

Paul Alan Taylor


SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%ColumnName%'
ORDER BY schema_name, table_name;

Replace ColumnName to your actual column name

like image 24
Pankaj Agarwal Avatar answered Jun 02 '23 09:06

Pankaj Agarwal