Update Trigger Distinct Date - mysql

I'm doing a project and I got stuck on this trigger. This is the two tables involved.
---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id_tra | int(11) | NO | PRI | NULL | auto_increment |
| nombre_tra | varchar(100) | NO | | NULL | |
| apellidos_tra | varchar(100) | NO | | NULL | |
| dni_tra | varchar(1000) | NO | | NULL | |
| telefono_tra | int(10) | NO | | NULL | |
| falta_tra | date | NO | | NULL | |
| dias_tra | int(255) | NO | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
+--------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------+------+-----+---------+----------------+
| id_rec | int(11) | NO | PRI | NULL | auto_increment |
| id_tra_rec | int(11) | NO | MUL | NULL | |
| id_var_rec | int(11) | NO | MUL | NULL | |
| fecha_rec | date | NO | | NULL | |
| cantidad_rec | int(255) | NO | | NULL | |
+--------------+----------+------+-----+---------+----------------+
On this case, id_tra and id_tra_rec are related, and I need a trigger to update the dias_tra on dias_tra + 1 for id_tra = id_tra_rec when there is an INSERT on the second table image.
Thats relatively easy but the strange thing is that the Insert could have different data but the same date (fecha_rec), so the trigger must know if there is a row with the same id and the same date(fecha_rec) to not to update the dias_tra. Something like a select distinct. Here is what i tried:
create trigger dias_tra
after insert on datos_recogida
for each row
begin
if (select fecha_rec from datos_recogida where id_tra_rec=new.id_trarec and fecha_rec=new.fecha_rec)
update datos_trabajadores set dias_tra = dias_tra +1 where id_tra=new.id_tra_rec
end if;
end;
Sorry for my English, first time here, hope you understanding. If you need more information I'm around here :)

You don't say when datos_trabajadores is created so here's a trigger which checks and creates if necessary. I have used a simple count to check if check if id_tra_rec and fecha_rec already exist - this is an after insert trigger so a count of 1 means its the first one. note the debug_table is there to debug you should remove when you are happy.
drop table if exists datos_recogida,datos_trabajadores;
create table datos_trabajadores
( id_tra int(11) auto_increment primary key,
nombre_tra varchar(100) ,
apellidos_tra varchar(100) ,
dni_tra varchar(1000) ,
telefono_tra int(10) ,
falta_tra date ,
dias_tra int(255) )
;
create table datos_recogida
( id_rec int(11) auto_increment primary key,
id_tra_rec int(11) ,
id_var_rec int(11) ,
fecha_rec date ,
cantidad_rec int(255) );
drop trigger if exists t;
delimiter $$
create trigger t after insert on datos_recogida
for each row
begin
if (select count(*) from datos_recogida where id_tra_rec = new.id_tra_rec and fecha_rec = new.fecha_rec) = 1 then
insert into debug_table(msg) values (concat('not found:',new.id_tra_rec,':',new.fecha_rec));
if not exists(select 1 from datos_trabajadores where dias_tra = new.id_tra_rec) then
insert into debug_table(msg) values ('inserting');
insert into datos_trabajadores(dias_tra,nombre_tra) values (new.id_tra_rec,1);
else
insert into debug_table(msg) values ('Updating');
update datos_trabajadores
set nombre_tra = nombre_tra + 1
where dias_tra = new.id_tra_rec;
end if;
end if;
end $$
delimiter ;
truncate table debug_table;
truncate table datos_recogida;
truncate table datos_trabajadores;
insert into datos_recogida (id_tra_rec,fecha_rec)
values
(1,'2019-01-01'),
(1,'2019-01-01'),
(1,'2019-01-02');
select * from debug_table;
select * from datos_trabajadores;
MariaDB [sandbox]> select * from debug_table;
+----+------------------------+------+
| id | msg | MSG2 |
+----+------------------------+------+
| 1 | not found:1:2019-01-01 | NULL |
| 2 | inserting | NULL |
| 3 | not found:1:2019-01-02 | NULL |
| 4 | Updating | NULL |
+----+------------------------+------+
4 rows in set (0.00 sec)
MariaDB [sandbox]> select * from datos_trabajadores;
+--------+------------+---------------+---------+--------------+-----------+----------+
| id_tra | nombre_tra | apellidos_tra | dni_tra | telefono_tra | falta_tra | dias_tra |
+--------+------------+---------------+---------+--------------+-----------+----------+
| 1 | 2 | NULL | NULL | NULL | NULL | 1 |
+--------+------------+---------------+---------+--------------+-----------+----------+
1 row in set (0.00 sec)

You can use exists to check if there is a record with the same data.
BEGIN
IF(EXISTS(select fecha_rec from datos_recogida where id_tra_rec=new.id_trarec and fecha_rec=new.fecha_rec)) THEN
//If the record exists do what you need.
ELSE
update datos_trabajadores set dias_tra = dias_tra +1 where id_tra=new.id_tra_rec
END IF;
END;

The solution of P.Salmon helped me to get a simplified way to do it. Thanks every body and hope helps somebody.
drop trigger if exists dias_tra;
delimiter $$
CREATE TRIGGER dias_tra AFTER INSERT ON datos_recogida FOR EACH ROW
BEGIN
DECLARE dias INT;
SET dias = (SELECT COUNT(DISTINCT fecha_rec) AS diasmes FROM datos_recogida WHERE id_tra_rec=NEW.id_tra_rec);
UPDATE datos_trabajadores SET dias_tra = dias where id_tra=NEW.id_tra_rec;
END; $$

Related

I can't use input in Mysql procedure

I how defined a procedure in MySql which has an input and input will be a name of column that will be added to table 'test1' but sql names that column input instead of using the value of input .how can i do this in the right way?
DELIMITER
CREATE PROCEDURE p1
(IN input CHAR(20))
BEGIN
ALTER TABLE test1
ADD COLUMN input char(20);
END
DELIMITER ;
You should google dynamic sql and read the manual https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-prepared-statements.html
In the meantime here's an example.
drop procedure if exists p;
alter table users
drop column abc;
DELIMITER $$
CREATE PROCEDURE p
(IN input CHAR(20))
BEGIN
#ALTER TABLE test1
#ADD COLUMN input char(20);
set #sql = concat('alter table users add column ',input,' char(20);');
select #sql;
prepare sqlstmt from #sql;
execute sqlstmt;
deallocate prepare sqlstmt;
END $$
DELIMITER ;
call p('abc');
describe users;
MariaDB [sandbox]> call p('abc');
+--------------------------------------------+
| #sql |
+--------------------------------------------+
| alter table users add column abc char(20); |
+--------------------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.42 sec)
MariaDB [sandbox]> describe users;
+---------------------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+-------------------+-------+
| id | int(11) | NO | PRI | NULL | |
| userName | varchar(60) | NO | | NULL | |
| photo | varchar(50) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| ts | datetime | YES | | CURRENT_TIMESTAMP | |
| events_participated | int(11) | YES | | NULL | |
| fb_uid | int(11) | YES | | NULL | |
| Column_name | varchar(10) | YES | | NULL | |
| post_type | varchar(10) | YES | | NULL | |
| password | varchar(8) | YES | | NULL | |
| abc | char(20) | YES | | NULL | |
+---------------------+-------------+------+-----+-------------------+-------+
11 rows in set (0.03 sec)

Trigger with Ids other than OLD.id

I have a table like this:
user_oauth:
+----------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------------+------+-----+---------+----------------+
| id | int(8) unsigned | NO | PRI | NULL | auto_increment |
| user_id | int(8) unsigned | NO | MUL | NULL | |
| google_id | varchar(30) | YES | UNI | NULL | |
| facebook_id | varchar(30) | YES | UNI | NULL | |
| windowslive_id | varchar(30) | YES | UNI | NULL | |
+----------------+-----------------+------+-----+---------+----------------+
which contains id of 3 the tables user_facebook, user_google, user_windowslive,
Example for user_facebook:
+-----------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------------+------+-----+---------+-------+
| id | varchar(30) | NO | PRI | NULL | |
| email | varchar(60) | NO | UNI | NULL | |
| firstname | varchar(30) | YES | | NULL | |
| lastname | varchar(30) | YES | | NULL | |
| link | varchar(100) | YES | | NULL | |
| locale | varchar(5) | YES | | NULL | |
| picture | varchar(200) | YES | | NULL | |
| verified | int(1) unsigned | NO | | NULL | |
+-----------+-----------------+------+-----+---------+-------+
I would like to make a TRIGGER ON DELETE on user_oauth, which will delete user_facebook row if facebook_id of user_oauth is filled.
So I tried:
DELIMITER $$
CREATE TRIGGER `user_oauth_delete` BEFORE DELETE ON `user_oauth`
FOR EACH ROW BEGIN
DELETE FROM user_facebook
WHERE user_facebook.id = user_oauth.facebook_id;
END
$$
DELIMITER ;
But I have the error message:
Unknown column 'user_oauth.facebook_id' in 'where clause'
How to do it?
Thanks.
Solution
I had misunderstanding the OLD statement, so I had to:
DELIMITER $$
CREATE TRIGGER `user_oauth_delete` BEFORE DELETE ON `user_oauth`
FOR EACH ROW BEGIN
DELETE FROM user_facebook
WHERE user_facebook.id = OLD.facebook_id;
END
$$
DELIMITER ;
Don't do this with triggers. Do this with cascading delete constraints.
alter table oath
add constraint fk_oath_facebook
foreign key (facebookid) references user_facebook(id)
on delete cascade;
The documentation does a pretty good job of explaining constraints and the cascading capabilities.
Other than what have been suggested, you can as well consider wrapping both the DML statement inside a transaction block in a stored procedure and pass the facebook_id as parameter to the procedure. Such that both operation will happen in same order. Something like
DELIMITER $$
CREATE PROCEDURE `transaction_sp` (#facebook_id varchar(50))
LANGUAGE SQL
DETERMINISTIC
SQL SECURITY DEFINER
BEGIN
DECLARE exit handler for sqlexception
BEGIN
-- ERROR
ROLLBACK;
END;
DECLARE exit handler for sqlwarning
BEGIN
-- WARNING
ROLLBACK;
END;
START TRANSACTION;
DELETE FROM user_facebook WHERE id = #facebook_id;
DELETE FROM `user_oauth` WHERE facebook_id = #facebook_id;
COMMIT;
END
$$

Trigger update the same table with data from another table after insert

I have two unrelated tables tbl_A & tbl_B
tbl_A
+----+---------------------+------+
| id | url | slug |
+----+---------------------+------+
| 1 | http://example.com/ | 3qqd |
| 2 | http://example.com/ | t8af |
| 3 | http://example.com/ | sjim |
| 4 | http://example.com/ | awfo |
| 5 | http://example.com/ | 6myy |
+----+---------------------+------+
tbl_A description:
+---------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------------+------+-----+---------------------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| url | text | YES | | NULL | |
| slug | varchar(255) | YES | MUL | NULL | |
+---------------------+---------------------+------+-----+---------------------+----------------+
and another table :
tbl_B
+----+---------------------+---------------------+------+
| ID | user_name | url | slug |
+----+---------------------+---------------------+------+
| 1 | john.reese | NULL | NULL |
+----+---------------------+---------------------+------+
tbl_B description :
+---------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------------+------+-----+---------------------+----------------+
| ID | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_name | varchar(60) | NO | MUL | | |
| url | text | YES | | NULL | |
| slug | varchar(255) | YES | MUL | NULL | |
+---------------------+---------------------+------+-----+---------------------+----------------+
tbl_A.id is unrelated to tbl_B.ID.
tbl_B.ID is userID field and gets filled up dnamically when a new user registers. So that tbl_B.ID gets a row inserted automatically as a user register, tbl_B.ID value gets auto incremented.
tbl_A on the other hand already exists with all the details.
What I want to achieve: whenever a new user registers and userID is
INSERT into tbl_B.ID, at the same time it should trigger an update of
tbl_B.user and tbl_B.slug with the values taken from tbl_A.user and
tbl_A.slug.
Outcome: After ID 1 is added
+----+---------------------+---------------------+------+
| ID | user_name | url | slug |
+----+---------------------+---------------------+------+
| 1 | john.reese | http://example.com/ | 3qqd |
+----+---------------------+---------------------+------+
Hope I am able to explain. I was trying to use triggers but got lost, am a newbie with mysql, please bear with me.
drop trigger if exists bi_tbl_B $$
delimiter $$
create trigger bi_tbl_B before insert on tbl_B
for each row begin
UPDATE tbl_B
SET url = url +
(SELECT url
FROM tbl_A
WHERE id = NEW.id)
WHERE ID = NEW.ID;
end;
$$
delimiter ;
I don't know whether this is even possible or should I try the other way round.
Add a field user_id in tbl_A and AFTER INSERT on tbl_B update tabl_A.user_id column with the userID from tbl_B.ID
I am open to suggestions, if not trigger then procedures.
It is possible to do it, just not the way you are trying. In the before insert trigger you can change the values being inserted by changing the NEW.field_name variables.
drop trigger if exists bi_tbl_B $$
delimiter $$
create trigger bi_tbl_B before insert on tbl_B
for each row begin
DECLARE v_slug as varchar(255);
DECLARE v_url as text;
SELECT url, slug INTO v_url, v_slug FROM tbl_A WHERE id = NEW.id;
NEW.url=v_url;
NEW.slug=v_slug;
end;
$$
delimiter ;

mysql after insert trigger which updates another table's column

i'm trying to write a trigger, I have following tables:
BookingRequest:
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| idRequest | int(11) | NO | PRI | NULL | auto_increment |
| roomClass | int(11) | NO | | NULL | |
| inDate | date | NO | | NULL | |
| outDate | date | NO | | NULL | |
| numOfBeds | int(11) | NO | | NULL | |
| status | int(11) | NO | MUL | NULL | |
| idUser | int(11) | NO | MUL | NULL | |
+-----------+---------+------+-----+---------+----------------+
status table:
+------------+--------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------------------------------------------+------+-----+---------+-------+
| idStatus | int(11) | NO | PRI | NULL | |
| nameStatus | enum('underConsideration','approved','rejected') | YES | | NULL | |
+------------+--------------------------------------------------+------+-----+---------+-------+
OccupiedRoom:
+--------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+----------------+
| idOccupation | int(11) | NO | PRI | NULL | auto_increment |
| idRoom | int(11) | NO | | NULL | |
| idRequest | int(11) | NO | | NULL | |
+--------------+---------+------+-----+---------+----------------+
i need a trigger which will change status in BookingReques to 1 if request with the same id is inserted into OccupiedRoom table, so i tried something like this
create trigger occupy_trig after insert on OccupiedRoom
for each row
begin
if BookingRequest.idRequest= NEW.idRequest
then
update BookingRequest
set status = '1';
where idRequest = NEW.idRequest;
end if;
END;
and it doesn't work, so any suggestions would be very appriciated
Try this:
DELIMITER $$
CREATE TRIGGER occupy_trig
AFTER INSERT ON `OccupiedRoom` FOR EACH ROW
begin
DECLARE id_exists Boolean;
-- Check BookingRequest table
SELECT 1
INTO #id_exists
FROM BookingRequest
WHERE BookingRequest.idRequest= NEW.idRequest;
IF #id_exists = 1
THEN
UPDATE BookingRequest
SET status = '1'
WHERE idRequest = NEW.idRequest;
END IF;
END;
$$
DELIMITER ;
With your requirements you don't need BEGIN END and IF with unnecessary SELECT in your trigger. So you can simplify it to this
CREATE TRIGGER occupy_trig AFTER INSERT ON occupiedroom
FOR EACH ROW
UPDATE BookingRequest
SET status = 1
WHERE idRequest = NEW.idRequest;
Maybe remove the semi-colon after set because now the where statement doesn't belong to the update statement. Also the idRequest could be a problem, better write BookingRequest.idRequest
DELIMITER //
CREATE TRIGGER contacts_after_insert
AFTER INSERT
ON contacts FOR EACH ROW
BEGIN
DECLARE vUser varchar(50);
-- Find username of person performing the INSERT into table
SELECT USER() INTO vUser;
-- Insert record into audit table
INSERT INTO contacts_audit
( contact_id,
deleted_date,
deleted_by)
VALUES
( NEW.contact_id,
SYSDATE(),
vUser );
END; //
DELIMITER ;

MySQL cursor not fetching inside procedure

I have the following tables
albums:
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| album_id | int(11) | NO | PRI | NULL | auto_increment |
| band_id | int(11) | YES | MUL | NULL | |
| release_date | varchar(45) | YES | | NULL | |
| name | varchar(45) | YES | | NULL | |
| format | varchar(45) | YES | | NULL | |
| music_genre_id | int(11) | YES | MUL | NULL | |
| label_id | int(11) | YES | MUL | NULL | |
| avg_rating | float | YES | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
and music_ratings
+-----------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+----------------+
| music_rating_id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| album_id | int(11) | YES | MUL | NULL | |
| rating | int(11) | YES | | NULL | |
+-----------------+---------+------+-----+---------+----------------+
After every insert into the *music_rating* I want to update the average rating in the albums table. I have a trigger for this, which calls a procedure. The thing is, the procedure does not work, for some reason the cursor is not fetching data from the table. (I called the procedure separately to make sure it isn't the trigger acting up. The tables have a couple of rows already, so it's not that.)
My procedure is pretty straight forward and looks like this
DELIMITER $$
CREATE PROCEDURE avg_album_calc(IN id_album INT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE rating INT;
DECLARE cur CURSOR FOR SELECT `rating` FROM `music_ratings` WHERE `album_id`=id_album;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
SET #ct=0;
SET #sm=0;
REPEAT
FETCH cur INTO rating;
IF NOT done
THEN
SET #ct = #ct +1;
SET #sm = #sm + rating;
END IF;
UNTIL done END REPEAT;
UPDATE albums SET avg_rating = #sm/#ct WHERE album_id = id_album;
CLOSE cur;
END$$
DELIMITER ;
I echoed the result of the cursor with a SELECT rating after the FETCH cur INTO rating; command, and it shows up as null.
You do not need to calculate and store avg_rating in the albums table. You can calculate in on the fly -
SELECT a.album_id, a.name, AVG(mr.rating) FROM albums a
LEFT JOIN music_ratings mr
ON a.album_id = mr.album_id
GROUP BY a.album_id