Is there any way of listing all tables and columns names using UNION or a JOIN?
If you want all tables and columns in a schema, no need to use UNION and BIND, just joining the data in
information_schema.columns
information_schema.tables
will do the trick. See details on both at:
http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
An example query that would achieve the minimum of what appears to be your goal would be:
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
But again, joining the two based on table name might be needed -- depends on your precise goal.
If you just want the tables with UNION/BIND in the names and the columns with UNION/BIND in the names, two simple queries to do that would be:
SELECT TABLE_NAME from INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%UNION%' or TABLE_NAME LIKE '%BIND%'
and
SELECT TABLE_NAME, COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%UNION%' or TABLE_NAME LIKE '%BIND%'
You can use INFORMATION_SCHEMA. The query below would be useful:
SELECT 'COLUMN' as Match_Type,
column_name as MATCH_NAME,
table_name,
table_schema
FROM INFORMATION_sCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%UNION%'
OR COLUMN_NAME LIKE '%BIND%'
UNION ALL
SELECT 'TABLE' as Match_Type,
table_name as MATCH_NAME,
table_name,
table_schema
FROM INFORMATION_sCHEMA.TABLES
WHERE TABLE_NAME LIKE '%UNION%'
OR TABLE_NAME LIKE '%BIND%'
Related
I would need to find all %phone% instances from all tables in a specific DB.
This is my script i'm trying with yet still getting all the DBs (I need only from DB1):
use DB1;
SELECT *
FROM
information_schema.columns
WHERE
column_name LIKE '%phone%';
I also tried writing like this:
SELECT *
FROM
DB1.information_schema.columns
WHERE
column_name LIKE '%phone%';
but I got a SQL Error [1064] [42000] for that syntax.
What would be the correct way to query this?
There's only one INFORMATION_SCHEMA database for all the databases on the server. The table database is in the table_schema column of the columns table.
SELECT *
FROM information_schema.columns
WHERE table_schema = 'DB1' AND column_name LIKE '%phone%';
I used the following for '%email%'
and it worked.
So i guess it would for for you too:
SELECT
DISTINCT TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME LIKE ('%phone%') AND TABLE_SCHEMA='DB1';
use the parameter TABLE_SCHEMA, to select your scheam/database
SELECT *
FROM
information_schema.columns
WHERE
column_name LIKE '%phone%'
AND TABLe_SCHEMA = 'DB1';
I'm astounded by the number of tables in Magento Enterprise 1.13 (over 200). I'm trying to get a handle on the way things are organized and I think it would be helpful to know the number of columns in each of the tables. The following query will get me a breakdown of columns and their data_types for each table:
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = `<database_name>`
ORDER BY TABLE_NAME;
But I would also like to know the number of columns in each table.
SELECT COUNT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME IN (
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<database_name>'
);
Unfortunately, the above query returns a count of the total number of columns in the database. I realize that my approach is too simplistic and a LOOP or a FOREACH statement is closer to the solution I'm looking for but I don't know how to make the leap to that point.
SELECT TABLE_NAME, COUNT(COLUMN_NAME) AS NoCOLUMNS
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_NAME IN (SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<database_name>')
GROUP BY TABLE_NAME;
Basic GROUP BY DEMO
I have a table with 300+ column. Looking for a specific column is like nightmare. Is there any query where If I would like to search for columns stats with 'grand' can be listed...
You can use show columns from table with where. Try the following query,
SHOW COLUMNS FROM tablename WHERE field like 'grand%';
Just put in your tablename after FROM and it would work.
select * from myTable where mycolumn like 'grand%'
Try this.
SELECT
table_name,
column_name,
data_type,
ordinal_position
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'DatabaseName' --- the database you want to search
AND table_name = 'yourTableName'
AND column_name LIKE '%Grand' ;
This one worked for me. Its shows all columns and table in entire database
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name OR table_name LIKE '%sale%';
I have a database with well over 90 tables and I am trying to figure which, if any, of them have the same two specific columns. The code I am looking for would be something like this:
SHOW TABLES IN `database`
WHERE column = 'columnA'
AND column = 'columnB';`
Is this even possible?
This will give you all tables having either of the two columns, which you can browse through to find what you need.
select *
from information_schema.columns
where column_name in ('columnA', 'columnB')
order by table_name, column_Name
SELECT TABLE_SCHEMA AS `schema`, TABLE_NAME AS `table`, COLUMN_NAME AS `column`
FROM `information_schema`.`COLUMNS`
WHERE COLUMN_NAME IN('column1','column2')
I have 2-3 different column names that I want to look up in the entire database and list out all tables which have those columns. Is there any easy script?
To get all tables with columns columnA or ColumnB in the database YourDatabase:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('columnA','ColumnB')
AND TABLE_SCHEMA='YourDatabase';
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';
More simply done in one line of SQL:
SELECT * FROM information_schema.columns WHERE column_name = 'column_name';
SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE 'employee%'
AND TABLE_SCHEMA='YourDatabase'
In older MySQL versions or some MySQL NDB Cluster versions that do not have information_schema, you can dump the table structure and search the column manually.
mysqldump -h$host -u$user -p$pass --compact --no-data --all-databases > some_file.sql
Now search the column name in some_file.sql using your preferred text editor, or use some nifty AWK scripts.
And a simple sed script to find the column. Just replace COLUMN_NAME with yours:
sed -n '/^USE/{h};/^CREATE/{H;x;s/\nCREATE.*\n/\n/;x};/COLUMN_NAME/{x;p};' <some_file.sql
USE `DATABASE_NAME`;
CREATE TABLE `TABLE_NAME` (
`COLUMN_NAME` varchar(10) NOT NULL,
You can pipe the dump directly in sed, but that's trivial.
For those searching for the inverse of this, i.e. looking for tables that do not contain a certain column name, here is the query...
SELECT DISTINCT TABLE_NAME FROM information_schema.columns WHERE
TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME NOT IN (SELECT DISTINCT
TABLE_NAME FROM information_schema.columns WHERE column_name =
'column_name' AND TABLE_SCHEMA = 'your_db_name');
This came in really handy when we began to slowly implement use of InnoDB's special ai_col column and needed to figure out which of our 200 tables had yet to be upgraded.
Use this one line query. Replace desired_column_name by your column name.
SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';
If you want to "get all tables only", then use this query:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%'
and TABLE_SCHEMA = 'tresbu_lk'
If you want "to get all tables with columns", then use this query:
SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%'
AND TABLE_SCHEMA='tresbu_lk'
select distinct table_name
from information_schema.columns
where column_name in ('ColumnA')
and table_schema='YourDatabase';
and table_name in
(
select distinct table_name
from information_schema.columns
where column_name in ('ColumnB')
and table_schema='YourDatabase';
);
That ^^ will get the tables with ColumnA and ColumnB instead of ColumnA or ColumnB like the accepted answer
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
The problem with information_schema is that it can be terribly slow. It is faster to use the SHOW commands.
After you select the database you first send the query SHOW TABLES. And then you do SHOW COLUMNS for each of the tables.
In PHP that would look something like
$res = mysqli_query("SHOW TABLES");
while($row = mysqli_fetch_array($res))
{ $rs2 = mysqli_query("SHOW COLUMNS FROM ".$row[0]);
while($rw2 = mysqli_fetch_array($rs2))
{ if($rw2[0] == $target)
....
}
}