I'm learning SQL with MySQL 5.7.
And I wanna enumerate all tables include temporary tables!
But when I query SHOW TABLES; It shows only non-temporary tables.
How can I list all tables?
I've not found a direct answer to this - as far as I can see there is no way (currently MySQL 8.0.31) of listing all temporary tables on your connection.
You can test for a table's existence using:
SELECT 1 FROM my_table WHERE 0;
If you don't get an error, the table exists in some form (TEMPORARY, BASE TABLE or VIEW). To check if it's temporary you can use:
SHOW TABLES IN my_db
WHERE Tables_in_my_db = "my_table"
(returns the table name if it exists and not temporary or use 'FULL TABLES' to return the table type as well)
or
SELECT table_type
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = "my_db" AND TABLE_NAME = "my_table";
(returns the table type if it exists and not temporary)
So a table is temporary if the first test doesn't return an error but it is NOT listed using one of the other tests.
This doesn't help to enumerate the temporary tables (I believe this can be done - see other answers - if the table is INNODB but not generally for MyISAM for example), but it does enable you to identify a table as temporary or otherwise if you know the name.
In innodb you could us e
SHOW TABLES FROM INFORMATION_SCHEMA LIKE 'INNODB_TEMP%';
https://dev.mysql.com/doc/refman/5.7/en/innodb-information-schema-temp-table-info.html
https://dev.mysql.com/worklog/task/?id=648
Related
I have a MySQL database table which has been listed twice with case sensitive name.
Both table names are pointing to same table, for example Admin and admin
When I checked information_schema it is listed as below:
mysql> SELECT TABLE_CATALOG, TABLE_NAME , TABLE_TYPE, ENGINE, CREATE_TIME
FROM information_schema.tables
where table_schema='school';
How do I clean up this mess?
Usually MySQL does not allow you to create the table with case-sensitive. It will show the error as :
ERROR 1050 (42S01): Table 'admin' already exists
But MySQL allows you to create a temp table with a existing name because they don't have the same "scope". A temporary table is visible in the session only, and it is dropped at session ending. If you have the same name, MySQL "hide" the original table until you drop your temp table.
I would suggest you to take a backup of the existing data and update the MySql version to 5.7 .
I have an issue with InnoDB tables. all table status are showing in use.
I am trying to open table but it is giving error "Table 'mysqlbackup.aadhars' doesn't exist in engine"
please help me
Maybe the table belongs to different schema (and not mysqlbackup) I would recommend checking all the table names (along with schemas) and see which schema aadhars table belongs to, e.g.:
SELECT *
FROM information_schema.tables
WHERE table_schema = 'mysqlbackup';
The above query will return the names of all the tables present in mysqlbackup schema. If you want to see which schema aadhars belongs to, try the following query:
SELECT *
FROM information_schema.tables
WHERE table_name = 'aadhars';
I am having two mysql databases. One is NEW and other one is OLD.
I require the list of different tables between two databases. That is my old database is having the list of 155 tables and my new database is having the list of 165 tables.
How can I get the name of the TEN differnt tables ?
Is there any tool to do that or we can able to do via query ?
Any help will be thankful...
Thanks in advance..
Have you tried:
SHOW TABLES IN database;
or using information schema:
select table_schema,
table_name from information_schema.tables
where table_name = ???;
EDIT AS PER OP'S COMMENT:
The INFORMATION_SCHEMA database is made up of temporary tables using the MEMORY storage engine.. All tables in the INFORMATION_SCHEMA database are stored directly in memory as MEMORY storage engine tables. They are totally internal to MySQL, so the .frm mechanisms are handled in mysqld. In my answer, I first showed the table layout of INFORMATION_SCHEMA.TABLES. It is a temporary table in memory. It is manipulated using storage engine protocols. Thus, when mysqld is shutdown, all information_schema tables are dropped. When mysqld is started, all information_schema tables are created as TEMPORARY tables and repopulated with metadata for every table in the mysql instance.
For e.g. If your run following two commands you will see all the databases in your mysql metadata.
show databases;
use information_schema; show tables;
Here you are specifying a table_schema to get the table names.
SELECT table_name from
information_schema.tables WHERE table_schema = 'mydb';
With a join: assuming one database name is db1, other db2
SELECT table_name from
db1.tables x
inner join
db2.tables
on x.table_name = y.table_name
;
I think you should query on database information_schema. It's a table which contains all meta data of all database.
Query something like:
SELECT * FROM `TABLES` T1
LEFT JOIN `TABLES` T2
ON T1.`TABLE_NAME` = T2.`TABLE_NAME`
WHERE T1.`TABLE_SCHEMA`='xxx'
AND T2.`TABLE_SCHEMA`='yyy'
AND T1.TABLE_CATALOG IS NULL
You can do this by querying the INFORMATION_SCHEMA (a database which contains information of other databases in the server like table names, column names, primary key columns, primary key names, indexes, etc.) like this:
-- this gives the table names that are in the new table but not in the old table
select newTable.TABLE_NAME
from TABLES newTable
where newTable.TABLE_SCHEMA='NEW' and newTable.TABLE_NAME not in
(
select oldTable.TABLE_NAME
from TABLES oldTable
where oldTable.TABLE_SCHEMA='OLD'
)
I am using MySQL v5.1.
I would like to create index on a table by executing the following SQL statement:
CREATE INDEX index_name
ON table_name (column_name)
But, I wan to firstly check if the index on that column has already been created or not, if not, create it (otherwise do not create). What is the SQL syntax for this?
Mysql doesn't have IF NOT EXISTS for CREATE INDEX. You can work it out by querying information_schema.statistics table. Take a look here, there is an example of stored procedure that does what you are looking for (search for "CREATE INDEX IF NOT EXISTS" on the page)
You want SHOW INDEX.
To get all the indexes on a table:
SHOW INDEX FROM table_name
MySQL allows you to add a WHERE clause to limit the results as well.
Lock the table while you're checking to see if the index exists (and if it doesn't exist, creating the index) so that another process doesn't create the index right after you've checked for it but before you've created it yourself.
I have a data base where I have permanent versions of tables that I make copies of and then convert to memory engine for faster performance. Is there a way to delete all the tables in a db by engine type? Something along the lines of:
drop * from db1 where engine = memory;
Any suggestions?
You can get list of table names with the following query:
select table_name
from information_schema.tables
where table_schema = 'db1' and engine = 'memory';
And then generate a query to drop them. As far as I know you cannot use expression / subquery for a tablename in the drop statement, so you will have to use 2 queries or create a stored procedure for this.