I have a bunch of tables in my "stats" database.
tcl20151w1d1
tcl20151w1d2
tcl20151w2d1
tcl20151w2d2
tcl20151w3d1
tcl20151w3d2
tcl20151w4d1
eu20151w1d1
eu20151w1d2
eu20151w2d1
eu20151w2d2
eu20151w3d1
eu20151w3d2
eu20151w4d1
..
How can i select all tables that starts with "tcl" in "stats" database. Is it possible? Do I have to union them manually?
You can query information_schema.tables table to get a list of tables where the table name start with tcl.
You can use the list to dynamically create a union query in a stored procedure using string concatenation and prepared statements.
If those tables are all myisam tables with the same structure, you may consider creating a merge table on them:
The MERGE storage engine, also known as the MRG_MyISAM engine, is a
collection of identical MyISAM tables that can be used as one.
“Identical” means that all tables have identical column and index
information.
Related
So we have 23 databases with the same schema and table names. I need to fetch all the data from a table called inventory which is present in all the dbs.
The possible solution I can think of is doing a UNION ALL, but that means I'll have to do a union 22 times.
Is there a shorter, easier faster way this can be done?
Is posible set a table to multiples schemas on mysql? For example:
i have two schemas with identical tables:
schema1.user
schema2.user
it is possible that when querying schema1.user the information returns the records of schema1.user and schema2.user, without triggers, stored procedures or views?
Short answer to your question: No. You can not do that without triggers, stored procedures or views
A better way to avoid duplicating your data in every database is to query the user table in schema-qualified format.
In other words, even if your default database is schema2 during a given query, you can query the table from schema1:
SELECT ... FROM sometable JOIN schema1.user ON ...
You can mix qualified and non-qualified syntax in the same query. Any table that doesn't have a schema qualifier is assumed to be in the default schema.
See https://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html
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 have a couple of MRG_MyISAM tables that merge a bunch of other tables in a MySQL database. I would like to figure out programmatically which tables are included in each merge table.
I know I could run SHOW CREATE TABLE and then parse the UNION=(tbl1, tbl2) part of the statement, but that seems a little hacky. Is there a better way?
In an ideal world, I'm looking for something like this:
SELECT * FROM ?? WHERE merge_table = 'merge_table_1'
That would return rows that each contain the name of a table that's included in "merge_table_1":
--------------
| table_name |
--------------
| tbl1 |
--------------
| tbl2 |
--------------
I don't think there is any data in INFORMATION_SCHEMA to list the members of a MERGE table.
If your application has direct access to the data directory on your database server, you can simply read the .MRG file for the merge table. It is a human-readable file that simply lists the tables in the merge, and any other merge table options.
You really shouldn't be using MERGE tables anymore. You should use MySQL's PARTITIONING engine, which is much more flexible. With partitioned tables, you can query the INFORMATION_SCHEMA.PARTITIONS table to find information on each partition.
In fact, you shouldn't be using MyISAM tables either. InnoDB is more scalable, and MyISAM doesn't support any of the properties of ACID.
SHOW CREATE TABLE table_name; -- see if this gives you the information
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'
)