I have a procedure like this :
create
procedure new_generation(IN id bigint unsigned)
BEGIN
create temporary table if not exists tmp_mine
(
id bigint unsigned primary key not null auto_increment,
created_at timestamp,
);
end
now I want to create an event for delete old rows.
create event per_ten_second on schedule
every '10' SECOND
starts '2021-10-26 11:49:16'
enable
do
delete from tmp_mine where created_at < now();
but it doesn't work. Can anybody give me a clue? Can we use generated temp table inside stored procedure out of it? for example inside an event.
Related
I have to create a trigger a which captures old and new value while updation & deletion taken place
Audit table :
create table if not EXISTS tbl_audits
(
TA_Auditid int AUTO_INCREMENT PRIMARY key,
TA_Name varchar(100),
TA_Oldvalue varchar(1000),
TA_Newvalue varchar(1000),
TA_Actiontaken varchar(100),
TA_createddt datetime,
TA_createdby varchar(100)
)
DELIMITER //
create TRIGGER TRfiledetails_update
BEFORE update on tbl_file_details
for EACH ROW
BEGIN
insert into tbl_audits
set TA_createddt =CURRENT_TIMESTAMP,
TA_Actiontaken ='update',
TA_Name ='File',
TA_Oldvalue =old.FL_dtstartdt ,
TA_Newvalue =new.FL_dtstartdt
end //
DELIMITER ;
In my main table i have to create trigger an event on two columns startdt and enddate.
In this case I would like to know whether I need to create two triggers separately for each column.
Is it possible to create two columns action on same trigger or need to create separate columns in audit table.
I'm trying to create a temporary table and select results from it, inside a mysql procedure.
Here's a piece of the SQL
DELIMITER $$
CREATE PROCEDURE `mydb`.`simulate_results` (currdate DATETIME, enddate DATETIME, idcam INT)
BEGIN
DECLARE MYVAR1,
MYVAR2,
MYVAR3,
IDX INT;
CREATE TEMPORARY TABLE IF NOT EXISTS tmp (
`Id` INT NOT NULL AUTO_INCREMENT,
`Field1` INT NOT NULL,
`Field2` INT NOT NULL,
`Field3` INT NOT NULL, PRIMARY KEY (`Id`)) ENGINE=MEMORY;
INSERT INTO tmp (`Field1`,`Field2`,`Field3`) VALUES (0,0,0);
INSERT INTO tmp (`Field1`,`Field2`,`Field3`) VALUES (1,0,0);
SELECT Field1,Field2,Field3,Id INTO MYVAR1,MYVAR2,MYVAR3,IDXFROM tmp ORDER BY RAND() LIMIT 1;
SELECT MYVAR1; (...)
The only variable that comes filled from the SELECT INTO statemente is IDX (primary key). The others are always NULL.
Is there something wrong with this code?
Have you looked at your temporary table to see what kind of data is lingering in it? The temporary table will still be there after the procedure completes and if you run the procedure again from the same session, you'll be writing new rows into the existing temporary table.
I would add DROP TEMPORARY TABLE IF EXISTS tmp before CREATE TEMPORARY TABLE ... to be sure you don't have old data hanging around.
I'm trying to create a procedure which adds / removes a table to the database. I'm working in SQL Server. The query works (it succeeds) but the table isn't added to the database.
I did refreshed...
ALTER procedure [dbo].[upgrade_1]
as
begin
create table awards (
ID int NOT NULL IDENTITY,
name nvarchar(256) DEFAULT 'award',
description nvarchar(256)
PRIMARY KEY (ID)
)
/*update goto_vs
set current_version = 1*/
end
The script you have in the question will only modify the PROCEDURE. The procedure needs to be executed for it to perform the required task e.g. create the table
Execute the procedure with this statement
EXEC upgrade_1
That should create the table
I need to create MySQL trigger that would log user ID on delete table row statement which must fit in one query, since I'm using PHP PDO. This is what I've come up so far:
I need the way to pass user ID in the delete query even though it is irrelevant to delete action to be performed:
Normally the query would look like this:
DELETE FROM mytable WHERE mytable.RowID = :rowID
If I could use multiple queries in my statement, I would do it like this:
SET #userID := :userID;
DELETE FROM mytable WHERE mytable.RowID = :rowID;
This way the variable #userID would be set before trigger event fires and it can use it. However since I need to squeeze my delete statement in one query, so I came up with this:
DELETE FROM mytable
WHERE CASE
WHEN #userID := :userID
THEN mytable.RowID = :rowID
ELSE mytable.RowID IS NULL
END
Just a note: RowID will never be null since it's the primary key. Now I have to create a delete trigger to log the user ID to the audit table, however I suppose that in this case trigger will be fired before the delete query itself which means that #userID variable will not be created? This was my idea of passing it as a value to the trigger.
I feel like I'm close to the solution, but this issue is a blocker. How to pass user ID value to the trigger without having multiple queries in the statement? Any thoughts, suggestions?
You can use NEW / OLD mysql trigger extensions. Reference: http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
Here is a sample code :
drop table `project`;
drop table `projectDEL`;
CREATE TABLE `project` (
`proj_id` int(11) NOT NULL AUTO_INCREMENT,
`proj_name` varchar(30) NOT NULL,
`Proj_Type` varchar(30) NOT NULL,
PRIMARY KEY (`proj_id`)
);
CREATE TABLE `projectDEL` (
`proj_id` int(11) NOT NULL AUTO_INCREMENT,
`proj_name` varchar(30) NOT NULL,
`Proj_Type` varchar(30) NOT NULL,
PRIMARY KEY (`proj_id`)
);
INSERT INTO `project` (`proj_id`, `proj_name`, `Proj_Type`) VALUES
(1, 'admin1', 'admin1'),
(2, 'admin2', 'admin2');
delimiter $
CREATE TRIGGER `uProjectDelete` BEFORE DELETE ON project
FOR EACH ROW BEGIN
INSERT INTO projectDEL SELECT * FROM project WHERE proj_id = OLD.proj_id;
END;$
delimiter ;
DELETE FROM project WHERE proj_id = 1;
SELECT * FROM project;
SELECT * FROM projectDEL;
So here is my problem, I have written a stored procedure to do the following task. In table events there are events that might potentially exist for venues that no longer exist. Not all events are tied to a venue, but the ones that are have an integer value in their venue id field otherwise it is NULL (or potentially zero but that is accounted for). Periodically, venues get deleted from our system, when that happens it is not possible to delete all of the events associated with that venue at that exact time. Instead, a task is run periodically at a later time that deletes every event that has a venue id that no longer references an existing record in the venues table. I have written a stored procedure for this and it seems to work.
This is the stored procedure:
DROP PROCEDURE IF EXISTS delete_synced_events_orphans;
DELIMITER $$
CREATE PROCEDURE delete_synced_events_orphans()
BEGIN
DECLARE event_count int(11) DEFAULT 0;
DECLARE active_event_id int(11) DEFAULT 0;
DECLARE active_venue_id int(11) DEFAULT 0;
DECLARE event_to_delete_id int(11) DEFAULT NULL;
CREATE TEMPORARY TABLE IF NOT EXISTS possible_events_to_delete (
event_id int(11) NOT NULL,
venue_id_temp int(11) NOT NULL
) engine = memory;
# create an "array" which is a table that holds the events that might need deleting
INSERT INTO possible_events_to_delete (event_id, venue_id_temp) SELECT `events`.`id`, `events`.`venue_id` FROM `events` WHERE `events`.`venue_id` IS NOT NULL AND `events`.`venue_id` <> 0;
SELECT COUNT(*) INTO `event_count` FROM `possible_events_to_delete` WHERE 1;
detector_loop: WHILE `event_count` > 0 DO
SELECT event_id INTO active_event_id FROM possible_events_to_delete WHERE 1 LIMIT 1;
SELECT venue_id_temp INTO active_venue_id FROM possible_events_to_delete WHERE 1 LIMIT 1;
# this figures out if there are events that need to be deleted
SELECT `events`.`id` INTO event_to_delete_id FROM `events`, `venues` WHERE `events`.`venue_id` <> `venues`.`id` AND `events`.`id` = active_event_id AND `events`.`venue_id` = active_venue_id;
#if no record meets that query, the active event is safe to delete
IF (event_to_delete_id <> 0 AND event_to_delete_id IS NOT NULL) THEN
DELETE FROM `events` WHERE `events`.`id` = event_to_delete_id;
#INSERT INTO test_table (event_id_test, venue_id_temp_test) SELECT `events`.`id`, `events`.`venue_id` FROM `events` WHERE `events`.`id` = event_to_delete_id;
END IF;
DELETE FROM possible_events_to_delete WHERE `event_id` = active_event_id AND `venue_id_temp` = active_venue_id;
SET `event_count` = `event_count` - 1;
END WHILE;
END $$
DELIMITER ;
Here is the table structure for the two tables in question:
CREATE TABLE IF NOT EXISTS events (
id int(11) NOT NULL,
event_time timestamp NOT NULL,
venue_id_temp int(11) NOT NULL
);
CREATE TABLE IF NOT EXISTS venues (
event_id int(11) NOT NULL,
venue_id_temp int(11) NOT NULL
);
The stored procedure works as written, but I want to know about ways that it could be made to run better. It seems like its doing a lot of extra processing to achieve its goal. Are there better ways I could query the data at hand, or are there other more useful commands and key words I could use that I just don't know about, which would allow me to complete this task better (fewer lines less computation). I am still learning how to use stored procedures, so I am using them to complete tasks as pragmatically as possible, I want to understand how this specific query could be made to better use the full range of features in MySQL to its advantage. Thank you folks.
Everithing is much simpler:
DROP PROCEDURE IF EXISTS delete_synced_events_orphans;
DELIMITER $$
CREATE PROCEDURE delete_synced_events_orphans()
BEGIN
DELETE
FROM `events`
WHERE `venue_id` IS NOT NULL AND `venue_id` <> 0
AND `venue_id` NOT IN (SELECT `id` FROM `venues`)
;
END $$
DELIMITER ;
That's it. :)
You think imperatively, trying to say MySQL how to complete your task. But SQL is a declarative language, designed for saying what to do.