MySQL Script Applied to Many Databases - mysql

I have one MySQL script and many databases that the script is to be applied.Simplified script is like this.
CREATE TABLE aaa1 AS SELECT * FROM dbname1.aaa ;
CREATE TABLE aaa2 AS SELECT * FROM dbname2.aaa ;
CREATE TABLE aaa3 AS SELECT * FROM dbname3.aaa ;
...
CREATE TABLE bbb1 AS SELECT * FROM dbname1.bbb ;
CREATE TABLE bbb2 AS SELECT * FROM dbname2.bbb ;
CREATE TABLE bbb3 AS SELECT * FROM dbname3.bbb ;
My Problem:
The easiest way that I can think of is applying "USE dbname" inside the stored procedure, but it doesn't work. I searched the internet and some people say dynamic SQL for this. I tried this and that but all of them are not working.
Could you help me resolve this with a working script example? Thank you in advance.

Related

Drop table and copy one table look alike in MYSQL weekly

I have a problem where I need to copy a table from MYSQL using this:
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable;
I need to drop that table weekly and create them again because I dont want to lose the original data.
Can anyone help me to find a solution where it can be done automatically. I really have no idea about that
What if you have a java program, where you're having the MySQL query and let the query be executed automatically through a cron job periodically.
Where you can maybe have two separate threads running, or you could do it within the same thread it self. ie, executing the INSERT into the newtable and then maybe you can simply drop the old ones.
OR you could go with the MySQL Event Scheduler. This SO pretty much explains the effective way of going ahead with this as per your requirement.
You can use MySQL EVENT Scheduler for this very purpose like below. You can as well consider using CREATE TABLE AS(CTAS) construct instead
delimiter |
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 WEEK
DO
BEGIN
DROP TABLE IF EXISTS newtable;
CREATE TABLE newtable AS
SELECT * FROM oldtable;
END |
delimiter ;

MySql - Create a table and select from it in the same procedure

I’m trying to execute this simple code as a procedure in MySql using the PHPMyAdmin
CREATE TEMPORARY TABLE IF NOT EXISTS jobs AS (SELECT
*
FROM
all_jobs
WHERE
job_object_type LIKE 'jobName'
OR
job_object_type LIKE 'jobStatus');
SELECT
*
FROM
jobs
But I’m getting an error (#1064) because of my second SELECT query. I’m coming from MS-SQL and there the SELECT would be executed without any problems. How can I execute the CREATE TABLE X query and direct after that the SELECT * FROM X query in the same procedure?
EDIT: forgot to mention that I’m using the MariaDB
Well that is how it's done using the PHPMyAdmin:
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS jobs AS (SELECT
*
FROM
all_jobs
WHERE
job_object_type LIKE 'jobName'
OR
job_object_type LIKE 'jobStatus');
SELECT
*
FROM
jobs;
END
Now everything is being executed as it should be.

MySQL VIEW Incorrect key file for table try to repair it

I have a FooBar view like:
CREATE VIEW `FooBar` AS
SELECT * FROM `Foo`.`Bar`
UNION ALL
SELECT * FROM `Foo1`.`Bar`
When I SELECT * FROM FooBar I get:
Incorrect key file for table '/tmp/#sql_1234_5.MYI'; try to repair it
When I run the select statements like this:
SELECT * FROM `Foo`.`Bar`
UNION ALL
SELECT * FROM `Foo1`.`Bar`
Everything goes well. The problem is clearly with my FooBar view.
When I REPAIR TABLE FooBar I get
'WhiskerDatabase.VisualDiscrimSuperimposed_Results' is not BASE TABLE
Corrupt
I tried to DROP VIEW FooBarand reCREATE VIEW FooBar... but the problems persists.
And I can't locate the sql_1234_5.MYI file in /var/lib/mysql/MyDB/.
Found the explanation here. MySQL build a temporary file. The temporary file is too big to fit in memory.
Adjusting the system memory or using a LIMIT clause could help solve the problem.
I solve my problem by creating a procedure that create a table instead of a view. Such as:
DROP TABLE `FooBar` IF EXISTS;
CREATE TABLE `FooBar` AS SELECT * FROM `Foo`.`Bar`;
INSERT INTO `FooBar` SELECT * FROM `Foo1`.`Bar`;

SELECT INTO equivalent in mysql

I have been easily using
SELECT * INTO newtable IN 'another_database'
FROM original_table_in_separate_database;
to backup/copy data from one table to another table easily in MSSQL.
Now i am moving to MYSQL and cannot accomplish this task as this feature is not available in MYSQL.
Though CREATE TABLE ... SELECT can somehow accomplish the task in same database, but not with two different database.
Please help me if there is any idea :)
Thanks in advance .
You can use INSERT INTO .. SELECT FROM construct like
INSERT INTO db1.dbo.newtable
SELECT * FROM db2.dbo.original_table_in_separate_database;
Point to note: For INSERT INTO .. SELECT to work both the target table and inserting table must exist. Otherwise, use CREATE TABLE AS ... SELECT like
CREATE TABLE newtable
AS
SELECT * FROM db2.dbo.original_table_in_separate_database;

Use If exists for select statement in mysql

I am creating a view and written the below code snippet :
CREATE OR REPLACE VIEW vclPersonData
AS
SELECT * FROM phone_data UNION
SELECT * FROM Address
I get an error if the table doesn't exists, to come overthat i used If Exists but it too doesn't works for me.
Any help is thankful.
Thanks in Advance.
You'll need two steps in your script:
CREATE TABLE IF NOT EXISTS
CREATE VIEW AS SELECT * FROM TABLE
If the table exists, step 1 will be harmless. If the table does not exist, step 1 will create it and step 2 will create an empty view.
If you only want the view to be created IF the table exist, check the existance of the table before:
BEGIN
SELECT 1 FROM TABLE;
CREATE VIEW AS SELECT * FROM TABLE
COMMIT