When executing SHOW TABLES FROM your_db_name_here inside MySQL, the result has Tables_in_{your_db_name_here} as its indexes. I'd like to change that, like this:
SHOW TABLES as _tables FROM mydb;
or something like
SELECT Tables_in_mydb AS _tables FROM (SHOW TABLES FROM mydb);
Any ideas?
It's usually easier to interact with the INFORMATION_SCHEMA database:
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
Example:
SELECT TABLE_NAME AS _tables
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mydb'
Related
I have multiple tables in my multiple databases.
On different servers, i use MySQL / PostgreSQL / MS SQL.
I keep short table namesbut the comments given to the tables are with full explanation.
I want query that will show me tables ending with "com" and also the comment given to each table (table's comment).
In MySQL, I know:
SELECT table_name FROM information_schema.tables where table_name like "%com"
But this shows all tables from all databases.
For MySQL, check out following:
SELECT table_name FROM information_schema.tables;
will show all table names in all databases;
SELECT table_name,table_comment FROM information_schema.tables
will show all table names + comment in all databases;
interesting thing, you can fire
SELECT * FROM information_schema.tables;
to know what all info you can get of a table.
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b';
will show all table names + comment in "sifr_b" database;
SELECT table_name,table_comment FROM information_schema.tables
where
table_schema = 'sifr_b' and
table_name like "%com";
will show those table names + comment in "sifr_b" database, that have table name ending with "com";
In MySQL database I have some tables which end with _history.
Now I want to select tables that are like _history.
I have done like below.
show tables like '%_history`
Now I got the desired result.
Now in this result I got some tables which start with temp. Ex: temp_102_history.
Is there a way to exclude the tables that start with temp in the show tables like '%_history statement.
Select table_name
from information_schema.tables
Where table_name like '%_history'
and table_name not like 'temp%'
and table_schema='your database'
You can use information_schema database for this.
Try this
SHOW TABLES
WHERE tables_in_test NOT LIKE 'temp_%'
AND tables_in_test LIKE '%_history'
Replace test with your DB name.
I want to drop all databases with zero tables I was able to get the databases with tables using
SELECT table_schema, count(table_name) FROM information_schema.tables group by table_schema
But how can I delete the dbs not in this list. I can't do it manually because there are more then 500 dbs there.
About to know schemas without tables, you can try this:
SELECT * FROM information_schema.schemata S
WHERE NOT EXISTS
(SELECT 'TABLE' from information_schema.tables T
WHERE T.table_schema = S.schema_name)
Because in system table SCHEMATA you'll find all schemas of your server and in table TABLES you'll find all tables in all schemas
The upper query must be input on cursor, so you must use a prepared statement to execute your cursor, because your DROP DATABASE has a variable (your schema_table) and it can be ran only with a prepared statement
Used the method posted by #dnoeth in the comments with a slightly diffrent query to get the drop commands and then with some Notepad++ magic executed them to drop all empty databases
SELECT concat('drop database ',schema_name) FROM information_schema.schemata
WHERE schema_name NOT IN
(SELECT TABLE_SCHEMA FROM information_schema.tables)
In Sqlite, I can query for tables names and their structure like this:
SELECT name, sql
FROM sqlite_master
WHERE type = 'table' AND Name NOT LIKE '%sqlite%'
I want to query for database schematic for MySQL database. Some digging in phpMyAdmin, I end with this:
SELECT SCHEMA_NAME, TABLE_NAME
FROM SCHEMATA, TABLES
WHERE SCHEMA_NAME = TABLE_SCHEMA AND SCHEMA_NAME ='myDb'
Unfortunately, this will only output tables names. Is there any sql query do the some work of:
mysqldump -u root myDb --no-data=true --add-drop-table=false > myDb.sql
select *
from information_schema.columns
where table_schema = 'sqlite_master'
order by table_name, ordinal_position
based on the accepted answer from this SO question
How about
SHOW CREATE TABLE tablename;
I know how to use queries but I've never had to use one for this particular tasks .. I know about the SHOW TABLES; command .. How can I write a query to check if a particular table exists in a MYSQL database .. For example , a query that checks if table MEMBERS exists in database called USERS ??
You can use INFORMATION_SCHEMA.TABLES
USE USERS;
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'MEMBERS'
Searching Table:
select * from
information_schema.tables
where table_name like '%MEMBERS%'
Searching Column:
select * from
information_schema.columns
where table_name like '%COLUMN%'