How to identify a column with name exist in all tables? - mysql

i want to query a database which has huge tables for the columns with specific name
ex: fetch all the tables which has column name like 'name'
Thanks.

Try
SELECT table_name -- you might want to add DISTINCT if you use pattern matching with LIKE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = SCHEMA()
AND column_name = 'name' -- or column_name LIKE '%name%'
Here is SQLFiddle demo

Related

Get Table Name, Column Name, Data Type & Character Maximum Lenght mySQL

I have a database in MySQL called "Test Database" and I want to create a query to get all database's columns with the following characteristics:
First column - Table name
Second column - Column name
Third column - Type
Forth column - Maximum length
I used the following queries separately to get what i want:
Get Table Name:
Select Table_Name from INFORMATION_SCHEMA.Tables
Get Columns Name:
Select Column_Name from INFORMATION_SCHEMA.Columns
Get Column Type:
Select Data_Type from INFORMATION_SCHEMA.Columns
Get Columns Size:
Select Character_Maximum_Length from INFORMATION_SCHEMA.Columns
But I did not manage to merge this into one query to get the below result:
Any help will be greatly appreciated!
You can put more than one column in a SELECT statement..
Select Table_Name, Column_Name, Data_Type, Character_Maximum_Length
from INFORMATION_SCHEMA.Columns
Use AS if you want to rename the columns you see (SELECT table_name AS "Table Name" .... I'd recommend not putting spaces in them though
If that is the entire table, then you could simplify your query easily as:
SELECT * FROM INFORMATION_SCHEMA.Columns
this will get all columns that you need.
If you want to be more specific you could try this variant:
SELECT Table_Name AS TableName, Column_Name as ColumnName, Data_Type as
DataType, Character_Maximum_Length as Character_Maximum_Length FROM
INFORMATION_SCHEMA.Columns
I hope this will help you!

mysql: check if table(s) AND column(s) exist in ONE query?

Working with multiple tables, the task is to check if the table exists and its certain columns exist, too.
The query
SELECT
(TABLE_NAME) AS table_name,
(GROUP_CONCAT(COLUMN_NAME)) AS column_name
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'dbms'
AND
TABLE_NAME IN ('user', 'status')
AND
COLUMN_NAME IN ('id', 'name')
GROUP BY table_name
works fine to detect existing columns, but returns nothing if either table does not exist or none of the columns exist.
The latter should be differentiated.
How to?
How to know whether it's a table or column names that do not exist?
To clarify, the query above will return 2 rows by 2 columns:
user | id, name
status | id, name
means, each table has both of the columns.
Changing one column name to, say, name1, will result in:
user | id
status | id
means, only id exists.
Changing column names to name1, id1 will return nothing.
This is the problem. It is impossible to say whether tables do not exist or columns.
It would be good if the return would look something like:
user | ''
status | ''
and if, say status does not exist, then like:
user | ''
what can be decoded as only user table exists, and none of the columns named in it.
You cannot use WHERE for column_name is you still want rows in the result set even if they don't match. It looks like the only real WHERE clause you want is based on the table name, and the column name is simply a display condition.
SELECT
table_name
GROUP_CONCAT(IF(column_name IN ('id','name'), column_name, NULL))
FROM information_schema.columns
WHERE table_schema = 'dbms'
AND table_name IN ('user','status')
GROUP BY table_name
you just need to change your logic in and condition as mentioned in your query TABLE_SCHEMA = 'dbms'
AND
TABLE_NAME IN ('user', 'status')
AND
COLUMN_NAME IN ('id', 'name')
it will fetch only the resultset if there are tables users or status with column name id or name in dbms,
what you can do is separately check for tables and if table exist check for columns. I hope it would help

Mysql statement to list tables of certain columns?

I'm trying to figure out the foreign keys relationship within my MySQL databases.
I have many tables that has the "mydomain_id" column located in other tables.
Is there a command line I can use to list all tables that has that particular column?
SELECT
DISTINCT `table_name`
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME IN ('name') AND
TABLE_SCHEMA='symfony_backend';
Gives: (all the tables listed below exist in my database symfony_backend)
table_name
'child'
'country'
'league'
'parent'
'player'
'schools'
'student'
'team'
'user'
You can feed more column names with: COLUMN_NAME IN ('name', 'another_column', 'and_another_one')
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'db_name'
AND column_name = 'mydomain_id'

SQL - How can I get the name of a specific column

I want to get the name of a specific column. I only found the function to get ALL column names:
select Column_name
from Information_schema.columns
where Table_name like 'table name'
Is it possible to select a specific column?
For example if I have a table with 10 columns, I want to get the column "firstname".
Yes it is:
select Column_name
from Information_schema.columns
where Table_name = 'table name'
and COLUMN_NAME = '<your column name>'

find columns in all database

If I have a database some_database with multiples tables like:
table_one
table_two
table_three
where
table_one has CPV_one
table_two has CPV_two
table_three has CPV_three
I need to find all tables that have a column like '%CPV%'
This can be done by sql query? I want to avoid check all tables one by one.
You can query information_schema to get a list of each table in your database with a matching column name:
SELECT
TABLE_NAME, COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
EDIT (selecting only the TABLE_NAME column)
As pointed out in a comment, if you want to select only the name of the table (without the list of columns that match), you should also use the DISTINCT keyword - otherwise a duplicate row will be returned for the same table for each column that matches:
SELECT
DISTINCT TABLE_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'SOME_DATABASE'
AND COLUMN_NAME LIKE '%CPV%'
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%CPV%'
AND TABLE_SCHEMA='YourDatabase';
How about querying the MySQL information_schema?
SELECT TABLE_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_NAME LIKE '%CPV%'
Here you have the code to get the table names with the needed condition:
SELECT DISTINCT TABLE_NAME
FROM information_schema.Columns
WHERE COLUMN_NAME LIKE '%CPV%';
For more details on the INFORMATION SCHEMA take a look here
And for COLUMNS table here