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.
Related
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
I would like to copy all data from one schema (a) to another (b).
Two schemas have the same structure.
I use
Insert into a.table1 select * from b.table1;
But there are 254 tables in a and listing them is very frustrated.
Do you have any better solution?
Thanks
2 options...
1) Use dynamic SQL to loop through the results of:
select TABLE_NAME from information_schema.tables
2) Output the results of that query into excel, build your inserts from that then run them in a big old proc.
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.
I am maintaining a legacy application with MySQL database with most of the tables having 20+ columns and few have 100+. To make the it friendlier I am trying to alter all the tables to arrange all the columns sorted alphabetically.
What would be the appropriate ALTER TABLE queries ?
There is no way for changing column order in a mysql table. However, you can create a new table with the columns in your order, using for example:
CREATE TABLE newtable SELECT a,b,c,d,e,f,g FROM old_table_name
This way, your newly created table will have columns in your defined order, and you can drop the old table and rename the newtable to old name.
In order to create the above mentioned query, you just need to get column names from your old table and sort them, to do that programatically you can use something like this:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='database_name' AND TABLE_NAME = 'old_table_name'
ORDER BY column_name
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'
)