I have a database db1 which contains tables tbl1, tbl2, and tbl3.
I also have an empty database db2.
Can I create a MERGE table mrg1 which merges the contents of tbl1, tbl2, and tbl3 from database db1, but is stored in database db2?
Yes, if your database is set up to allow you to run queries against multiple databases at once. I think it would look something like this,
INSERT INTO db2.mrg1 (colA,colB,ColC)
(
SELECT colA,colB,ColC
FROM db1.tbl1,db1.tbl2,db1.tbl3
WHERE [whatever joins your tables together]
)
Related
I have multiple databases all having the same table structure on one server.
I need to change three tables in all databases which name is
xx_databasename
where xx is an isoAlpha2Code of the country the database os connected to.
So how can I perform a query like
DROP TABLE IF EXISTS tablename;
CREATE TABLE tablename (....);
on each database with just one mySQL command?
Is there a more-efficent, less laborious way of copying all records from one table to another that doing this:
INSERT INTO product_backup SELECT * FROM product
Typically, the product table will hold around 50,000 records. Both tables are identical in structure and have 31 columns in them. I'd like to point out this is not my database design, I have inherited a legacy system.
There's just one thing you're missing. Especially, if you're using InnoDB, is you want to explicitly add an ORDER BY clause in your SELECT statement to ensure you're inserting rows in primary key (clustered index) order:
INSERT INTO product_backup SELECT * FROM product ORDER BY product_id
Consider removing secondary indexes on the backup table if they're not needed. This will also save some load on the server.
Finally, if you are using InnoDB, reduce the number of row locks that are required and just explicitly lock both tables:
LOCK TABLES product_backup WRITE;
LOCK TABLES product READ;
INSERT INTO product_backup SELECT * FROM product ORDER BY product_id;
UNLOCK TABLES;
The locking stuff probably won't make a huge difference, as row locking is very fast (though not as fast as table locks), but since you asked.
mysqldump -R --add-drop-table db_name table_name > filepath/file_name.sql
This will take a dump of specified tables with a drop option to delete the exisiting table when you import it. then do,
mysql db_name < filepath/file_name.sql
DROP the destination table:
DROP TABLE DESTINATION_TABLE;
CREATE TABLE DESTINATION_TABLE AS (SELECT * FROM SOURCE_TABLE);
I don't think this will be worthy for a 50k table but:
If you have the database dump you can reload a table from it. As you want to load a table in another one you could change the table name in the dump with a sed command:
Here you have some hints:
http://blog.tsheets.com/2008/tips-tricks/mysql-restoring-a-single-table-from-a-huge-mysqldump-file.html
An alternative (depending on your design) would be to use triggers on the original table inserts so that the duplicated table gets the data as well.
And a better alternative would be to create another MySQL instance and either run it in a master-slave configuration or in a daily dump master/load slave fashion.
I have databases db1 and db2 and I want to copy all tables from db1 to db2. I have tried many times. For one table:
create table backup.(table_name) select from db1.(table_name);
create table backup.* select from db1.*;
You can try:
select * from db1.database into db2.backup
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 have such a situation, in my system there are many databases and for some queries I need to use the same table "hours", which basically has one field 'h' and stores hours like '00',...,'23'.
My question is about efficiency, is it better to create separate database and store this table there or have this table in each database. My queries will look like:
SELECT ... FROM hours CROSS JOIN some_table ...
Thank you!
Either way, you'd be having to modify all the "non-local" queries to use that table
SELECT ...
FROM sometable
CROSS JOIN dbname.hours
If it's stuck into its own database, then you have to modify ALL queries. If it's in one of the 'real' databases, you only have to modify n-1 queries. Plus having to grant appropriate permissions on that table if you're using multiple different mysql accounts for the different databases.