In MySQL, I know I can list the tables in a database with:
SHOW TABLES
However, I want to insert these table names into another table, for instance:
INSERT INTO metadata(table_name) SHOW TABLES /* does not work */
Is there a way to get the table names using a standard SELECT statement, something like:
INSERT INTO metadata(table_name) SELECT name FROM table_names /* what should table_names be? */
To get the name of all tables use:
SELECT table_name FROM information_schema.tables;
To get the name of the tables from a specific database use:
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';
Now, to answer the original question, use this query:
INSERT INTO table_name
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_database_name';
For more details see: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
Try:
select * from information_schema.tables
See: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
if we have multiple databases and we need to select all tables for a particular database we can use TABLE_SCHEMA to define database name as:
select table_name from information_schema.tables where TABLE_SCHEMA='dbname';
Besides using the INFORMATION_SCHEMA table, to use SHOW TABLES to insert into a table you would use the following
<?php
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
$arrayCount = 0
while ($row = mysql_fetch_row($result)) {
$tableNames[$arrayCount] = $row[0];
$arrayCount++; //only do this to make sure it starts at index 0
}
foreach ($tableNames as &$name {
$query = "INSERT INTO metadata (table_name) VALUES ('".$name."')";
mysql_query($query);
}
?>
Take a look at the table TABLES in the database information_schema. It contains information about the tables in your other databases. But if you're on shared hosting, you probably don't have access to it.
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'DATABASE'
MySQL INFORMATION_SCHEMA.TABLES table contains data about both tables (not temporary but permanent ones) and views. The column TABLE_TYPE defines whether this is record for table or view (for tables TABLE_TYPE='BASE TABLE' and for views TABLE_TYPE='VIEW'). So if you want to see from your schema (database) tables only there's the following query :
SELECT *
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='myschema'
I think you can get the data you want from INFORMATION_SCHEMA TABLES.
You can find more info here: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
For fetching the name of all tables:
SELECT table_name
FROM information_schema.tables;
If you need to fetch it for a specific database:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_db_name';
Output:
+--------------------+
| table_name |
+--------------------+
| myapp |
| demodb |
| cliquein |
+--------------------+
3 rows in set (0.00 sec)
There is yet another simpler way to get table names
SHOW TABLES FROM <database_name>
I think it may be helpful to point out that if you want to select tables that contain specific words you can easily do it using the SELECT (instead of SHOW). Below query easily narrows down the search to tables that contain "keyword"
SELECT *
FROM information_schema.tables
WHERE table_name like "%keyword%"
This below query worked for me. This can able to show the databases,tables,column names,data types and columns count.
**select table_schema Schema_Name ,table_name TableName,column_name ColumnName,ordinal_position "Position",column_type DataType,COUNT(1) ColumnCount
FROM information_schema.columns
GROUP by table_schema,table_name,column_name,ordinal_position, column_type;**
Yes, using information_schema.TABLES.
This works even on a cloud solution like Skyvia shown below:
With this you can simply use a SELECT statement like the above to add it up for your INSERT statement. But change the table_schema value to match the database name for your actual setup.
To insert, update and delete do the following:
$teste = array('LOW_PRIORITY', 'DELAYED', 'HIGH_PRIORITY', 'IGNORE', 'INTO', 'INSERT', 'UPDATE', 'DELETE', 'QUICK', 'FROM');
$teste1 = array("\t", "\n", "\r", "\0", "\x0B");
$strsql = trim(str_ireplace($teste1, ' ', str_ireplace($teste, '', $strsql)));
$nomeTabela = substr($strsql, 0, strpos($strsql, ' '));
print($nomeTabela);
exit;
Related
I'm searching a way to show only the filled tables' list on a MySQL database schema, in other words by filtering out all the empty tables.
I know that I can perform something like that for show all the tables within a database schema:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'myd_db_schema_name'
What if I want to add an additional WHERE condition for show only NOT NULL tables's list?
I don't know if it helps but you can use two WHERE Clause with AND
And to filter out the empty Tables you can check if the Table has a row like this:
WHERE table_rows >= 1
So the full Query would be
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'myd_db_schema_name' AND table_rows >= 1
I haven't tried it out but hopefully it works.
Okay, this is the solution:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'my_db_schema_name' AND table_rows >= 1
Instead of using COLUMNS I used TABLES.
So, we managed to do interesting things to our database that created invalid views. We just want to drop these views from the database and move on.
What I could not find is an easy way to find all invalid views in the database so that I can work from there. Is there an easy way to do this?
Recipe to create an invalid view
create table some_table (some_column varchar(20));
insert into some_table(some_column) values('some_data');
create view some_view as (select some_column from some_table);
select * from some_view;
# Now drop the table and test the view
drop table some_table;
select * from some_view;
The solution from Ralph works fine. If you want a query without subselect, try this:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'view'
AND table_rows is null
AND table_comment like '%invalid%'
The condition table_rows is null is important as it will force an evaluation of the view and the error message in the table_comment column.
If afterwards you want to fix your view, you can see the original definition with
SELECT view_definition
FROM information_schema.views
WHERE table_schema = 'your_database'
AND table_name = 'your_view'
A better way of finding broken views within your MySQL database:
SELECT vws.table_schema,vws.table_name
FROM (
SELECT *
FROM information_schema.tables
WHERE table_type='VIEW'
AND table_comment LIKE '%invalid%'
) vws;
Original source of query
SELECT TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_NAME NOT IN (
SELECT TABLE_NAME
FROM information_schema.TABLES
)
Based on an answer that was somehow deleted.
SELECT CONCAT('CHECK TABLE ', table_name, ';') AS my_view_check_statements
FROM information_schema.views
WHERE table_schema = 'your_database_name'
INTO OUTFILE '/tmp/chkstmts.sql';
source '/tmp/chkstmts.sql';
Is it possible to get the custom result when executing below query:
show table status from dbname
I customized "show processlist" query in this way:
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST where time > 4 order by TIME desc;
In the same way, I want to get the custom result from above query.
Many thanks for your suggestions...
After going through information_schema, I got my answer which I want to share here.
SELECT * FROM information_schema.tables WHERE table_schema = 'dbname';
If want to list down only some specific columns, we can mention the column names separated by comma just after SELECT key. Also, we can add filter records by adding conditions in WHERE clause. For example:
SELECT table_name,table_type,Engine,version,table_rows FROM information_schema.tables WHERE table_schema = 'jprod';
There are only two differences between below queries:
(a)show table status from dbname;
(b)SELECT * FROM information_schema.tables WHERE table_schema = 'dbname';
Query (b) provides 4 extra columns - (i) Table_catalog (ii) Table_schema (iii) Table_type (iv)Checksum
Some column names in query (a) is brief likewise table_name as name, Table_rows as rows, table_comment as comment.
I'd like to show DataDictionary for entire tables in database.
SHOW COLUMNS
FROM `MyDataBase`.`MyTables`
WHERE IN ( SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'MyDataBase'
);
Can i use query something like this?
I want to see entire column data using a single query
Here is what I use to generate a data dictionary when I have to:
SELECT t.table_schema AS db_name,
t.table_name,
(CASE WHEN t.table_type = 'BASE TABLE' THEN 'table'
WHEN t.table_type = 'VIEW' THEN 'view'
ELSE t.table_type
END) AS table_type,
c.column_name,
c.column_type,
c.column_default,
c.column_key,
c.is_nullable,
c.extra,
c.column_comment
FROM information_schema.tables AS t
INNER JOIN information_schema.columns AS c
ON t.table_name = c.table_name
AND t.table_schema = c.table_schema
WHERE t.table_type IN ('base table', 'view')
AND t.table_schema LIKE '%'
ORDER BY t.table_schema,
t.table_name,
c.ordinal_position
This will list all of the databases on the server that the logged in user has access to. You may want to change the where clause to only look at the specific table schema you want.
is this what you want:
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='yourtablename';
From MySQL 5.7 Manual
Many sections indicate what SHOW statement is equivalent to a SELECT that retrieves information from INFORMATION_SCHEMA. For SHOW statements that display information for the default database if you omit a FROM db_name clause, you can often select information for the default database by adding an AND TABLE_SCHEMA = SCHEMA() condition to the WHERE clause of a query that retrieves information from an INFORMATION_SCHEMA table.
Usually I prefer to take this with multiple DESC. I feel SHOW COLUMNS is bit slower than DESC table_name.
So if want to get all the columns in some databases
Loop thru SHOW TABLES FROM DB_NAME
Loop thru all tables as DESC table_name
In the same way, SHOW INDEXES is slower when compared to SHOW CREATE TABLE if you just want to see the indexes on a table
describe table_name;
Retrieving information from INFORMATION_SCHEMA, but need DBA privilege.
The shortest syntax is this:
SHOW COLUMNS
FROM `MyDataBase`.`MyTables`;
Full SHOW Syntax for columns:
SHOW [FULL] COLUMNS
FROM tbl_name [FROM db_name]
[like_or_where]
I like this one,
it's simple with elemental info.
SELECT
table_name,
column_name,
column_type,
is_nullable,
column_comment
FROM
information_schema.COLUMNS
WHERE
table_schema = 'YOUR_SCHEMA_NAME'
ORDER BY
table_name,
ordinal_position ASC;
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)
....
}
}