I need to delete all tables that were created today. I do not know what are the tables., because the process is autogenerated by a framework.
This is possible by querying information_schema? any idea?
You can get all tables created today by using
SELECT TABLE_NAME, TABLE_SCHEMA AS db FROM information_schema.`TABLES` t
WHERE DATE(t.CREATE_TIME) = CURDATE()
AND t.TABLE_SCHEMA NOT IN("information_schema", "mysql", "performance_schema")
The NOT IN("information_schema", "mysql", "performance_schema") excludes (temporary) system tables created by msql itself. It's best to limit TABLE_SCHEMA to whatever database name you are using, if you don't need to delete tables among different databases.
How you delete them is now up to you ;).
Related
I am trying to find the list of MySQL's system tables (or internal tables).
The database "mysql" contains both its system tables (internal or proprietary to MySQL database) and user created tables.
For example, by executing the below query
SELECT table_name, table_type, engine, TABLE_COMMENT
FROM information_schema.tables
Where TABLE_SCHEMA = 'mysql'
order by engine desc;
we get all tables' type as BASE TABLE only. These entries include my user table also (for example, CREATE TABLE mysql.tst1 (i INT) ENGINE = MYISAM;)
Could you please let me know how can we differentiate between internal(or system) tables AND user created tables? Do we have any query or system objects like stored procedure ....etc ...?
how about the below query i guess you missed out the information_schemaand performance_schema
so check out the below query
SELECT table_name, table_type, engine, TABLE_COMMENT
FROM information_schema.tables
Where
TABLE_SCHEMA IN ('mysql','information_schema','performance_schema')
ORDER BY engine DESC
I have a database with hundreds of the same tables however not all tables have the same column order. I don't know which tables and I don't know what order they are in, only that the columns are the same for every table. Is there a way to rearrange all columns in a table? I know with the Alter statement you can rearrange one column but I would like to to all columns at once for a table.
EDIT:
I would need to do this for all tables using the table_schema and then create a query for all. So exporting the database and changing the create table isn't an option since I would need to change it for all tables.
Thanks to CBroe here is a solution:
make an export of only the data of the database and create a new database.
Then run this query in the information_schema:
SELECT CONCAT("Create Table `db_new`.`", TABLE_NAME, "` LIKE `db_old`.`default` ;") as MySQLCMD FROM TABLES where TABLE_SCHEMA = 'db_old'
Then execute all these queries which are generated for you to create all the tables with the same structure (default table).
Then import the data in the new database.
I am facing a problem in searching a particular table_name.
I have around 50 databases on the server and i wish to search a table_name say X is created in which all databases.
Is there any straight-forward to find the exact databases in which X table is found in MYSQL through phpMyAdmin.
Any help will be appreciated.
Thanks
You can query the information_schema database for this. The below query will return the names of all the databases, which contains the table your_table_name.
SELECT `TABLE_SCHEMA`
FROM `information_schema`.`TABLES`
WHERE `TABLE_NAME` = 'your_table_name'
I hope this is what you are looking for.
According to the MySQL documentation about information_schema database,
INFORMATION_SCHEMA provides access to database metadata.
Metadata is data about the data, such as the name of a database or
table, the data type of a column, or access privileges. Other terms
that sometimes are used for this information are data dictionary and
system catalog.
Please query the information_schema.TABLES with table_type='BASE TABLE' and providing the name of the table(table_name).
This query will give us all the tables with '<your_table_name>' in all the databases that are currently in our server.
Please change '<your_table_name>' as per your requirement.
SELECT TABLE_SCHEMA, TABLE_NAME
FROM `information_schema`.`TABLES`
where table_type='BASE TABLE'
and table_name = '<your_table_name>'
limit 100;
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'
)
Is there any way to know the time when last select statement has been performed on a table? I am using the InnoDB storage engine.
I have tried with the following query:
select update_time,table_name from information_schema.tables where table_schema='databasename';
..but I'm receiving NULL in the update_time column.
Unless you manually update a last_accessed-field on the table, my best bet would be to add query logging and parse the log-files.
I googled and found these relates questions:
When was the last time a mysql table was accessed?
How do you get the last access (and/or write) time of a MySQL database?
SELECT UPDATE_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'`