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 |
Related
I want to wrote a trigger for table users. when new user added, if his Title is IT related, then create a record in IT contact list table.
So I wrote below trigger.
CREATE TRIGGER `test1` INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (username,Title);
END IF;
END;
It has error 'unknow column Title in the field list' But it did exist in table Users and IT_contact_list.So what's the issue?Thx.
Fixed and tested in workbench. Try this:
delimiter //
drop trigger if exists test1 //
CREATE TRIGGER `test1` AFTER INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(new.Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (new.username,new.Title);
END IF;
END//
insert into Users values('john','HardwareIT');
insert into Users values('bill','Hardware engineer');
select * From IT_contact_list;
--result set:
john HardwareIT
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
I have 2 tables member_details and archives. I want a trigger that will insert deleted data from member_details into archives as soon as as a particular record is deleted from the member_details table.
DELIMITER $$
CREATE TRIGGER member_details_ADEL AFTER DELETE ON member_details
FOR EACH ROW
insert into archives values
This is how you can do it, lets say you have a table called test with the following data
mysql> select * from test ;
+----------------------+
| id |
+----------------------+
| 10 |
| 20 |
| 30 |
+----------------------+
You have another table called test1 as no data currently
mysql> select * from test1 ;
Empty set (0.00 sec)
Now lets write a trigger so that when there is a delete on the test table the deleted record gets added to the test1 table. You need to use after delete trigger for this and the using old.col you can get the data
In this example I have only one column you can use old.col name to access any column on the table where the trigger is executing
delimiter //
create trigger log_delete after delete on test
for each row
begin
insert into test1 (id) values (old.id);
end ; //
delimiter ;
Now lets delete a record in test table as
mysql> delete from test where id = 10 ;
Query OK, 1 row affected, (0.00 sec)
mysql> select * from test ;
+----------------------+
| id |
+----------------------+
| 20 |
| 30 |
+----------------------+
2 rows in set (0.00 sec)
mysql> select * from test1 ;
+----------------------+
| id |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
Now as you can see the deleted row has been added to the table test1.
All you need to do is to set proper table names and the column names in the above trigger along with the trigger name you want to have
USE church;
DELIMITER $$
CREATE TRIGGER member_details_ADEL AFTER DELETE ON member_details
FOR EACH ROW
begin
insert into archives
(TITLE, NAME, `NAME OF SPOUSE`, `LEVEL OF EDUCATION`, ADDRESS, `PHONE NUMBER`, OCCUPATION, `DATE JOINED`, `EMERGENCY CONTACT`)
values
(old.TITLE, old.NAME, old.`NAME OF SPOUSE`, old.`LEVEL OF EDUCATION`, old.`ADDRESS`, old.`PHONE NUMBER`,old.`OCCUPATION`, old.`DATE JOINED`, old.`EMERGENCY CONTACT`);
END ; $$
delimiter ;
Make sure to backtick the column names when there is a space in between or in case using a reserve keyword.
You can use OLD keyword to fetch values from the deleted record.
But you have to specify column names with the insert statement.
Example :
delimiter //
drop trigger if exists member_details_adel //
create trigger member_details_adel
after delete on member_details
for each row
insert
into archives( col1, col2, colx )
values( old.col1, old.col2, old.colx );
//
delimiter ;
Change and extend column names as per your table needs.
Update 1:
There was syntax error in your tried trigger body.
Corrected one is as follows:
delimiter $$
create trigger member_details_adel
after delete on member_details
for each row
insert
into archives (title, name, name of spouse,
level of education, address,
phone number, occupation,
date joined, emergency contact)
values (old.title, old.name, old.`name of spouse`,
old.`level of education`, old.`address`,
old.`phone number`, old.`occupation`,
old.`date joined`, old.`emergency contact`);
$$
delimiter ;
Hello Im trying to find a way the UPDATE or INSERT data with one query.
I have a table with like this:
+------+-------------+
| User | action_type |
+------+-------------+
| Jon | 1 |
| Kate | 2 |
| Jon | 4 |
+------+-------------+
I want to insert new value for Jon only if there is no values of Jon.
If I have values of Jon I want to update all the rows with Jon.
Ive read about INSERT ... ON DUPLICATE KEY UPDATE but I dont have unique values.
Thanks for helping.
you can count the entries for jon. If exists update else insert.
if you want to implement only using sql you can use an stored procedure
something like this:
CREATE PROCEDURE insertorupdate (IN name VARCHAR(20))
BEGIN
DECLARE numJon INT;
SELECT COUNT(*) INTO numJon FROM table WHERE User=name;
IF numJon > 0 THEN
// UPDATE ;
ELSE
// INSERT
END IF;
END
Then you can call you Store Procedure:
CALL insertorupdate('John');
If you can do it from your app you can call the same thing but separatelly. Do a select count, test if count if grater than 0 and then do the insert or the update on DB
Don't count like in user468891's answer. It might become a performance issue. Instead just check if an entry exists. As soon as an entry is found, the query returns true, count continues to find all records.
DELIMITER $$
CREATE PROCEDURE insertorupdate (IN name VARCHAR(20))
BEGIN
IF (EXISTS(SELECT 1 FROM table WHERE User = name)) THEN
// UPDATE...
ELSE
// INSERT...
END IF;
END $$
DELIMITER ;
I have two tables with the following column names.
users
ui | email | pass
user_profiles
_email | qty | lvl
I am attempting to make sure that everytime a new user is added to the users table that the user_profiles table creates a new row for the new user. However it fails. After reading through many sample trigger statements I can't seem to find the error.
CREATE TRIGGER updatUserProfilesTBL AFTER INSERT ON users
BEGIN
INSERT INTO user_profiles (_email)
VALUES (new.email)
END;
Looks like you are missing the FOR EACH ROW. Additionally, you will need to alter the DELIMITER if executing this through the MySQL command line and add a ; after the insert statement.
DELIMITER $$
CREATE TRIGGER updatUserProfilesTBL AFTER INSERT ON users
FOR EACH ROW BEGIN
INSERT INTO user_profiles (_email)
VALUES (new.email);
END$$
DELIMITER ;
According to the CREATE TRIGGER spec, it is not optional.