Create table automatically in database backup when I create table from database source - create-table

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

Related

Alter tables on multiple databases from same server

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?

Merge 2 tables with different databases

Consider Database 1 & Database 2 having a single table each Table 1 & Table 2
How is it possible to update table 1 inserting/updating/deleting any change that happens to Table 2?
You can just do a cross database trigger.
If you're using SQL server you can read here:
Sql Server Trigger between 2 databases
If you're using Mysql you can read here:
Cross database trigger in Mysql
Perhaps create a view showing both tables?
CREATE VIEW my_view AS
SELECT * FROM db1.Table1, db2.Table2
Any change happening at any of the tables will be shown on the view, I hope it's what you wanted~

TEMPORARY table in Mysql

If two users create two TEMPORARY tables at the same time in my mysql database using a PHP script, will it create two different tables? Can those users use their own table without facing any trouble and will those table be automatically deleted?
thanks :)
CREATE TEMPORARY TABLE TempTable ( ID int, Name char(100) ) TYPE=HEAP;
INSERT INTO TempTable VALUES( 1, "Foo bar" );
SELECT * FROM TempTable;
DROP TABLE TempTable;
As stated in the manual:
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)
Temporary tables are created in a current session; so, two users can create two temp. tables at the same time in their threads. These tables will be removed on session closing.
You can use [IF NOT EXISTS] in this query...
reference

MySQL MERGE table across separate databases

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]
)

Mysql query to copy the structure of a table to create another table

Looking for help in creating a Mysql query to copy the structure of an existing table to create another table.
To create a table as in an exact replica of another table:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
If you want to also copy the contents of the table you can do:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
INSERT INTO `new_table_name` SELECT * FROM `old_table_name`;
If you want to copy the table structure including its keys, then you should use:
CREATE TABLE `new_table_name` LIKE `old_table_name`;
To copy the whole table
CREATE TABLE `new_table_name` SELECT * FROM `old_table_name`;
It will create the table and insert all the data from the old table but without bringing the keys from the old table. So you will need to set the keys for the new table.
Its old thread but if someone needed. If you want create query that will create exactly copy of existing table.
show create table <existing table name>
MySQL query to copy the structure of a table to create another table structure without data another way is...
CREATE TABLE `table_name_new` select * from `table_name_old` limit 0;
also you can use this:
CREATE TABLE 'new_table_name' SELECT * FROM 'pattern_table' where 1=0;