Azure SQL, Before trigger? - mysql

I have a relationship between two entities in my database. So before I create a single row I need to delete the row in the relation entity, like so:
user userHous
id 1 1 2
So when I delete a user row, it should do a before trigger and delete userHous.
in Mysql this was easily done with my old trigger:
USE `db`;
DELIMITER $$
CREATE DEFINER=`user`#`ip` TRIGGER `BDEL` BEFORE DELETE ON `user`
FOR EACH ROW
begin
DELETE FROM user WHERE userid = OLD.userid;
DELETE FROM house WHERE userid = OLD.userid;
end
I'm not sure how to go about doing this with { FOR | AFTER | INSTEAD OF }
I made this
CREATE TRIGGER [dbo].[Trigger]
ON [dbo].[user]
INSTEAD OF DELETE
AS
BEGIN
DELETE FROM userHouse WHERE userid = userid;
DELETE FROM user WHERE userid = userid;
END
But this just deletes all rows in both Tables, I guess because I put userid = userid. How do I retrieve the "parameter" which was in the query that made the trigger?

Try this
CREATE TRIGGER [dbo].[Trigger]
ON [dbo].[user]
AFTER DELETE
AS
DECLARE #user_id INT
SELECT #user_id = (SELECT userid FROM DELETED)
BEGIN
DELETE FROM userHouse WHERE userid = #user_id;
END

Related

Update New value by checking OLD Value in AFTER Update Statement using MySQL Trigger

I have a two different database tables
database: test1 -> table: customer
database: test2 -> table: customer_data
customer
id email
1 test#test.com
2 test#gmail.com
3 test#yahoo.com
customer_data
id params
1 test#gmail.com
2 test#yahoo.com
3 test#test.com
When user update customer email address for example,
for id =1
I want to create trigger which check
if customer_data table has
params = test#test.com
update it to new one using trigger
Code I tried
CREATE TRIGGER after_customer_email_update AFTER UPDATE ON customer_data
FOR EACH ROW
BEGIN
UPDATE customer_data
SET params = NEW.email
WHERE params = OLD.email;
END
But not updating record in another database table!!
You defined the trigger on the wrong table, and also the logic of the update is not correct. Try this version:
DELIMITER //
CREATE TRIGGER after_customer_email_update AFTER UPDATE ON customer
FOR EACH ROW
BEGIN
UPDATE customer_data
SET params = NEW.email
WHERE params = OLD.email
END; //
DELIMITER ;
CREATE TRIGGER after_customer_email_update
AFTER UPDATE
ON customer
FOR EACH ROW
UPDATE customer_data
SET params = NEW.email
WHERE params = OLD.email;
PS. Single-statement trigger (any compound statement) does not need in BEGIN-END and DELIMITER reassign (they're excess, but you may use them nevertheless).

How do I set trigger to copy user_id to another table

I need a trigger in phpmyadmin for 2 tables.
I have two tables called users and group_has_users. Table group_has_users has 2 values called group_id and user_id.
I'm trying to make a trigger that automatically inserts new data to group_has_users table with user_id when new user is created in users table( with group_id set to 1)
How to do this with phpmyadmin trigger?
insert into group_has_users (user_id) values (new.user_id);
I have no clue to make it group_id to set 1.
I think this is what you want:
delimiter $$
create trigger after_insert_users
after insert on users
for each row begin
if new.group_id = 1 then
insert into group_new_users (user_id) values (new.user_id);
end if;
end $$
delimiter ;
Note that you will have to run this in your cli or in mysql workbench
Everytime a new record is created in table users, you are looking to automatically create a new record in group_has_users, with a default group_id of 1.
Consider the following trigger:
DELIMITER //
CREATE TRIGGER users_after_insert
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO group_has_users(group_id, user_id) VALUES(1, NEW.user_id);
END;
//
Demo on DB Fiddle:
DELIMITER //
CREATE TABLE users (user_id int)//
CREATE TABLE group_has_users (group_id int, user_id int)//
CREATE TRIGGER users_after_insert
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO group_has_users(group_id, user_id) VALUES(1, NEW.user_id);
END;
//
INSERT INTO users(user_id) values(1);
SELECT * FROM group_has_users;
| group_id | user_id |
| -------- | ------- |
| 1 | 1 |

Using a variable in a MySQL stored procedure

I need to clean up the users table and several related tables. So I create a variable that contains the userids and I want to use those values instead of using a sub-query for each delete statement.
I get a syntax error with this. What am I doing wrong?
DELIMITER $$
CREATE PROCEDURE `DBNAME`.`SP_PURGE_DISABLED_USERS` ()
BEGIN
select userid into #disabled_users from USERS where disabled=1;
delete from USER_ACTIVITY where userid in SELECT userid FROM #disabled_users;
delete from USER_PREFS where userid in SELECT userid FROM #disabled_users;
-- <snip> several other related tables to be cleaned up
delete from USERS where userid in SELECT userid FROM #disabled_users;
END
For one thing, at the end of your procedure you need to put your delimiter:
END$$
Then reset you're delimiter back to ;
DELIMITER ;
In the procedure, your delete statements don't make sense. I'd stop using the parameter to store the results of the select and an inner select:
delete from USER_ACTIVITY
WHERE userid in ( select userid from users where disabled=1 );
If you only have a realatively small set of disabled users to delete, you could use group concat and find in set:
DELIMITER $$
CREATE PROCEDURE `DBNAME`.`SP_PURGE_DISABLED_USERS` ()
BEGIN
select GROUP_CONCAT(userid) into #disabled_users from USERS where disabled=1;
delete from USER_ACTIVITY where FIND_IN_SET(userid, #disabled_users);
delete from USER_PREFS where FIND_IN_SET(userid, #disabled_users);
-- <snip> several other related tables to be cleaned up
delete from USERS where FIND_IN_SET(userid, #disabled_users);
END$$
DELIMITER ;
I wouldn't do this with many thousands of userid's though... there may be a limit to string length in a variable.
I decided to go with a temporary table. It cut the stored procedure execution time in half.
CREATE PROCEDURE `DBNAME`.`SP_PURGE_DISABLED_USERS` ()
BEGIN
create temporary table disabled_users
as (select userid from USERS where disabled=1);
delete from USER_ACTIVITY where userid in SELECT userid FROM disabled_users;
delete from USER_PREFS where userid in SELECT userid FROM disabled_users;
-- <snip> several other related tables to be cleaned up
delete from USERS where userid in SELECT userid FROM disabled_users;
drop table disabled_users;
END
Thanks to all who helped on this question.

How to track who changes a record in a table

I need to track who changes to a table in mySQL database. Would you please give me an idea how I can determine which of my application users makes changes to a record in a table?
I assume you have a table with 2 columns that their name is colmun1 and column2 You should add modified_by column and also add this trigger:
DELIMITER |
CREATE TRIGGER before_update_sometable
BEFORE UPDATE ON sometable FOR EACH ROW
BEGIN
IF (NEW.column1 <> OLD.column1 or NEW.column2 <> OLD.column2) THEN
NEW.modified_by = user();
END IF;
END;
|
DELIMITER ;

How to DO MYSQL TRIGGER FOR DELETE Entry Before UPDATE

I have an Two table, main table is "Invoice" and sub table is "Invoice_split".
Before update on the "Invoice" table I want to delete the related data on the sub table "Invoice_split" for that I have written the below query But it's not working.
"DELIMITER $$
CREATE TRIGGER before_invoice_update
BEFORE UPDATE ON mac_invoice FOR EACH ROW
BEGIN
DELETE FROM mac_invoice_split WHERE OLD.invoice_id = id;
END$$
DELIMITER ;"
id => "Invoice" table primary key
invoice_id =>foreign key of "Invoice" in "Invoice_split" table
you should try this
WHERE OLD.id = invoice_id;