Drop table and copy one table look alike in MYSQL weekly - mysql

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 ;

Related

Phpmyadmin Database Trigger Creation Query

I have two tables inventory table(columns 'inventory_id','asset','item','delivered_by', 'received_by', 'quantity ''date') and a summary table ('stock_id', 'asset', 'item', 'qty'). I want to create a database trigger that sums up the quantity of assets and items from the inventory table into the summary table after insertion of a new row in the inventory table. .
An Instance:
Expectation From trigger
Here is a query i tried to create the trigger but it failed
CREATE TRIGGER updateqty
AFTER INSERT ON inventory
FOR EACH ROW
BEGIN
UPDATE INTO stock SET qty = (old.qty + stock.qty)
WHERE inventory.item = summary.item_name OR summary.asset_name
Any ideas as to how to create the trigger for my scenario would be very much appreciated. Thanks in advance.
There is no such thing as update into, there is no such thing as old. in an insert trigger,the where clause is invalid but I cannot guess what it should be, every begin must have an end, every statement must have a terminator - usually ;. Are you attempting this in phpmyadmin, mysql workbench or something else?

Create a trigger that removes a name from a table if it is present in another

To futher clarify i was trying to create a trigger that checks a table for a number in sql.
If it finds said number then it erases that entire row.
It uses a separate table of names to check.
I thought it be could done using a join but have had no luck.
So it would look like this I suppose if(tb1.name = tb2.name) then DELETE row.
I'm sorry if the formatting is off.
EDIT; I am using phpmyadmin so some of the the code may be missing but here is the code from my latest "attempt"
It uses on INSERT and time is set to AFTER
SELECT * FROM flights WHERE NOT EXISTS (SELECT * FROM no fly list WHERE PassengerId.Id = Passenger.Id)
have not added the DELETE as of now but the work is somewhat ongoing
Assuming that flight and dnf both have passenger_id column that uniquely identifies a passenger of interest then your trigger might look like this
DELIMITER $$
CREATE TRIGGER dnf_insert AFTER INSERT
ON dnf
FOR EACH ROW BEGIN
DELETE FROM flights WHERE passenger_id = NEW.passenger_id;
END$$
DELIMITER ;
If you post DDL (create table statements) for all relevant tables we can refine the query

MySQL views with arguments

I have been working on a pretty large database this last week. Basically I am taking an Access database and converting it to a MySQL database. I have seccessfully converted all the tables and views to MySQL. However I have a view that requires input from the user, the date. The other view is the view that will be call.
view 1 - compiled_fourweeks - needs date
view 2 - metrics_fourweeks - uses `compiled_fourweeks in query.
I was thinking of a precedure but I won't be able to reference the columns in the query.
I am kind of running out of ideas at this point.
If I understand correctly, you need to execute a view (metrics_fourweeks) that needs data from another view (compiled_fourweeks), and this last view requires input from the user.
I would go with the procedure approach:
create procedure fourWeeksData(d date)
create or replace view compiled_fourweeks
select ...
from ...
where recordDate = f -- Just an example; use whichever where clause you need
...;
select * from metrics_fourweeks;
end
If your database will be used just by a single user, your problem is solved. But if your database is meant to be used by more than one user... well, you can use temporary tables:
create procedure fourWeeksData2(d date)
drop table if exists temp_compiled_fourweeks;
create temporary table temp_compiled_fourweeks
select ...
from ...
where recordDate = f -- Just an example; use whichever where clause you need
...;
-- You will need to create the required indexes for this new temp table
-- Now replicate the SQL statement, using your new temp table
select ...
from temp_compiled_fourweeks
...;
end
Hope this helps you.

How to call a stored procedure and alter the database table in 1 go

I'm really struggeling with this for some time now.
I have a MySQL database and a lot of data. It is a formula1 website i have to create for college.
Right now the j_tracks_rounds_results table is filled with data but one column is not filled out. It's the rank column.
I created a stored procedure as the following:
DROP PROCEDURE `sp_rank`//
delimiter ##
PROCEDURE `sp_rank`(sid INT)
begin
set #i = 0;
SELECT `performance`, subround_id, #i:=#i+1 as rank
FROM `j_tracks_rounds_results`
where subround_id = sid
order by `subround_id`,`performance`;
end
delimiter ;
The output is like the following:
rec.ID PERFORMANCE SUBROUND_ID RANK
87766 100,349114516829 1 1
93040 101,075635087628 1 2
88851 101,664302543497 1 3
It gets the results and ads a rank to it, sorted on performance so the lowest performance gets rank1 etc...
What i am trying to achieve is to put the rank back into the table. Like an ALTER command for the column "rank".
How would i be able to accomplish this?
Basically don't...
Create table to hold the key (rec.id ?) and the rank. Truncate it to get rid of the previous results then use insert into ... with your query and then join to it.
You really don't want to be altering tables in your normal running, guaranteed some one will use the column when it isn't there, and then when you look at the fault it will be...
People just don't look for table structures changing through the application lifetime, it's a screw up waiting to happen.
You are misapplying your SQL statements. You want the UPDATE command, not ALTER.
eg.
UPDATE table SET rank=#i WHERE subround_id=#id

SQL Server 2008: is there a way to create a trigger like this?

What I would like to do is to create a trigger for my table where anytime someone makes and update, I log the username/nym, GetDate(), and the name of the table being updated into another table called something like "Updates"
You would have to create a trigger for each table - you cannot have a "ueber-trigger" that handles all tables at once.
Inside that trigger, you can have something like:
CREATE TRIGGER trgAuditing
ON dbo.YourTableName
FOR INSERT, UPDATE
AS BEGIN
INSERT INTO dbo.Updates(TableName, UserName, Date)
SELECT "YourTableName", SUSER_SNAME(), GETDATE()
END
Again: do this for each table of interest, and you should be fine!