I need Stored-Procedure That AUTOMATICALLY Deletes everytime row, where in table paym both columns table1 and table2 are not empty.
Example in tables below:
table: paym
ID username table1 Table2
+-------+-------------+-------------+-----------+
| 1 | John | Value | Value |
+-------+-------------+-------------+-----------+
| 2 | Alex | Null | Null |
+-------+-------------+-------------+-----------+
Condition is True: After Deleted row:
ID username table1 Table2
+-------+-------------+-------------+-----------+
| 2 | Alex | Null | Null |
+-------+-------------+-------------+-----------+
My attemp is: (Not working)
CREATE PROCEDURE DeleteRow
BEGIN
DELETE
FROM
paym WHERE table1 and table2 IS NOT NULL ;
END;
CREATE PROCEDURE DeleteRow
BEGIN
DELETE
FROM
paym WHERE table1 IS NOT NULL AND table2 IS NOT NULL ;
END;
Your logic is right here, but the syntax is wrong.
After every field on WHERE clause you must specify a condition, just like this:
CREATE PROCEDURE DeleteRow
BEGIN
DELETE
FROM
paym WHERE table1 IS NOT NULL AND table2 IS NOT NULL ;
END;
You can also use COALESCE
CREATE PROCEDURE DeleteRow
BEGIN
DELETE FROM paym WHERE COALESCE(table1,table2) IS NOT NULL;
END;
the code should read table is not null and table 2 is not null, as you need to have a condition after an and statement.
Related
I have 2 tables which are below.
I would like to create a trigger for table_one. when changed the username in table_one, update user_id value from the table_two user value.
table_one:
+--------------+----------+
| user_id | username |
+--------------+----------+
| 15 | robin |
| 46 | albert |
+--------------+----------+
table_two:
+--------------+----------+
| id | user |
+--------------+----------+
| 1 | john |
| 2 | jack |
| 3 | robin |
| 4 | kylie |
| 5 | robert |
| 6 | albert |
| 7 | jay |
+--------------+----------+
thanks in advance
Do a BEFORE UPDATE trigger, not an AFTER UPDATE trigger.
(I don't think it's possible in an AFTER UPDATE trigger to modify the row that was updated, that fired the trigger. I could be wrong about that, but I just can't wrap my brain around how that would work.)
This is a demonstration of a BEFORE UPDATE trigger on table_one that assigns a value to the user_id column (of the row being updated) based on the result from a query:
DELIMITER $$
CREATE TRIGGER ...
BEFORE UPDATE ON table_one
BEGIN
# local variable we will temporarily store a value fetched from a query
DECLARE li_new_id BIGINT DEFAULT NULL; # match datatype of table_two.id
# lookup `id` value from `table_two` with a SQL query
SELECT s.id
INTO li_new_id
FROM table_two s
WHERE s.user = NEW.username
ORDER BY s.id
LIMIT 1 ;
# assign the value we fetched to the `user_id` column of the row being updated
SET NEW.user_id := li_new_id ;
END$$
DELIMITER ;
Note that if the query doesn't find a matching row, the local variable will have a NULL value. So the SET statement will assign NULL to to the user_id column. There's no check in the trigger that the value we assign won't violate a constraint (NOT NULL, UNIQUE, FOREIGN KEY).
I need trigger That Deletes row where in table paym both columns table1 and table2 are not empty.
Example in tables below:
table: paym
ID username table1 Table2
+-------+-------------+-------------+-----------+
| 1 | John | Value | Value |
+-------+-------------+-------------+-----------+
| 2 | Alex | Null | Null |
+-------+-------------+-------------+-----------+
Condition is True: After Deleted row:
ID username table1 Table2
+-------+-------------+-------------+-----------+
| 2 | Alex | Null | Null |
+-------+-------------+-------------+-----------+
My attemp is: (Not working)
CREATE trigger DeleteROW
AFTER UPDATE ON paym
FOR EACH ROW
BEGIN
IF (NEW.table1 IS NOT NULL AND NEW.table2 IS NOT NULL) THEN
DELETE
FROM
paym WHERE table1 and table2 IS NOT NULL ;
END IF;
END
A trigger cannot modify the table it is running on.
You should create a stored procedure to handle this, and call that instead of the DELETE command...
I have a table with this data
mysql> SELECT * FROM prueba;
+----+------+------+------+
| id | U1 | U2 | cant |
+----+------+------+------+
| 2 | U123 | U124 | 2 |
+----+------+------+------+
I neeed to increment 'cant' in 1 everytime i execute the statement.
But if the row doesn't exists it has to be created.
The columns U1 and U2 are pairs of data, this means a U1,U2 pair is unique, and thats the condition to search if the record exists.
This is what i have made so far.
select U1,U2,cant,
(
case when exists (select U1,U2 from prueba where U1='U123'
and U2='U124')
then 1
else 0
end
) as tmp
from prueba
See the accepted answer in insert-to-table-or-update-if-exists-mysql. The idea is to use MySQLs
INSERT INTO ... ON DUPLICATE KEY UPDATE ...
Assume the following structure:
Table1:
ID | name
Table2:
ID | name
Table3:
ID | table1_id | table2_id | value
I want to build a trigger, after insert to Table1 if id not exist, to create new rows for each row in Table2 inside Table3 with the corresponding IDs.
What I did so far is creating this logic in php, I have never created triggers this complex before so I don't really know how to approach this.
Example:
Customers Table after insert:
+----+------+
| ID | Name |
+----+------+
| 1 | Dan |
+----+------+
Currency Table:
+----+------+
| ID | Name |
+----+------+
| 1 | USD |
| 2 | EUR |
+----+------+
Customers Currency Table after trigger
+----+---------------+-------------+-------+
| ID | customer_id | currency_id | Value |
+----+---------------+-------------+-------+
| 1 | 1 | 1 | NULL |
| 2 | 1 | 2 | NULL |
+----+---------------+-------------+-------+
Another option that you can use is:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `trg_bi`$$
CREATE TRIGGER `trg_bi` BEFORE INSERT ON `table1`
FOR EACH ROW
BEGIN
INSERT INTO `table3` (`table1_id`, `table2_id`)
SELECT NEW.`id`, `t2`.`id`
FROM `table2` `t2`
WHERE NOT EXISTS (SELECT NULL FROM `table1` `t1` WHERE `t1`.`id` = NEW.`id`);
END$$
DELIMITER ;
Here is validated by the id column table1, but you can use the column you want to validate, however, depends as validate that there is no 'customer' in table1.
SQL Fiddle example
To deal with this you need to use cursor in trigger, here is a nice tutorial on this http://www.mysqltutorial.org/mysql-cursor/
Now in your case I would suggest that the customer table id should be primary key auto incremented so that you always have unique value
So here how it should be
create table customer (id int primary key auto_increment , name varchar (100));
create table currency (id int primary key auto_increment, name varchar(100));
insert into currency (name) values ('USD'),('EUR') ;
create table customer_currency (id int primary key auto_increment, customer_id int , currency_id int , val varchar(100));
The trigger will be something as
delimiter //
create trigger customer_add after insert on customer
for each row
begin
DECLARE done INT DEFAULT FALSE;
DECLARE currency_id int;
DECLARE currency_val varchar(100);
DECLARE cur CURSOR FOR SELECT id,name FROM currency;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
ins_loop: LOOP
FETCH cur INTO currency_id,currency_val;
IF done THEN
LEAVE ins_loop;
END IF;
INSERT INTO customer_currency (customer_id,currency_id,val) VALUES (NEW.id,currency_id,currency_val);
END LOOP;
CLOSE cur;
end ; //
delimiter ;
Now in mysql lets add a record on customer table
mysql> insert into customer (name) values ('Abhik') ;
Query OK, 1 row affected (0.02 sec)
Now lets see what is there in the customer_currency symbol
mysql> select * from customer_currency ;
+----+-------------+-------------+------+
| id | customer_id | currency_id | val |
+----+-------------+-------------+------+
| 1 | 1 | 1 | USD |
| 2 | 1 | 2 | EUR |
+----+-------------+-------------+------+
In the trigger I am adding the currency value as well in the 3rd table if you do not want then can ignore that and it will become null.
You can write an after delete trigger on customer and delete the data from customer_currency where customer_id is the id of the deleted row in customer table.
I've written a trigger to insert a row into table b after an operation on table a.
For some reason, it has no effect if I add this trigger 'after insert' and then insert a row. However, it does work if I add the trigger as 'after update', and update the row.
Here's the trigger code. When I replace 'AFTER UPDATE' with 'AFTER INSERT', and do an insert, then nothing happens when i insert a new row. I get no errors when creating the trigger, and I am not trying to update the same table the trigger is being set upon.
Any help is appreciated!
thanks,
Jen
drop trigger if exists insertUndecided;
DELIMITER //
CREATE TRIGGER insertUndecided
AFTER UPDATE ON jiraissue
FOR EACH ROW
BEGIN
insert into nodeassociation (SOURCE_NODE_ID, SOURCE_NODE_ENTITY, SINK_NODE_ID, SINK_NODE_ENTITY, ASSOCIATION_TYPE, SEQUENCE)
select
NEW.id as SOURCE_NODE_ID,
'Issue' as SOURCE_NODE_ENTITY,
(select pv.id from projectversion pv
where pv.vname='undecided'
and pv.project=NEW.project ) as SINK_NODE_ID,
'Version' as SINK_NODE_ENTITY,
'IssueFixVersion' as ASSOCIATION_TYPE,
NULL as SEQUENCE
from dual where exists
(select pkey from jiraissue
where id=NEW.id and id not in
(select distinct source_node_id from nodeassociation
where source_node_entity='Issue' and SINK_NODE_ENTITY='Version'
and ASSOCIATION_TYPE='IssueFixVersion') );
END;//
DELIMITER ;
On MySQL 5.5.20 with InnoDB tables i've reproduced your situation in a simplified test.
both, inserting using a result from the trigger table which is referring to the new row alreay and directly inserting using the NEW values worked fine
CREATE TABLE test1(a1 INT NOT NULL auto_increment, b1 INT, PRIMARY KEY (a1) );
CREATE TABLE test2(a1 INT, b1 INT);
CREATE TABLE test3(a1 INT, b1 INT);
DELIMITER ;;
CREATE TRIGGER testAI AFTER INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a1 = NEW.a1, b1 = NEW.b1;
INSERT INTO test3 ( a1, b1 ) SELECT a1, b1 FROM test1 WHERE a1 = NEW.a1;
END;
;;
DELIMITER ;
Fire the trigger
INSERT INTO test1 (b1) VALUES (1),(2),(3);
Directly inserted from NEW values
mysql> SELECT * FROM test2;
+------+------+
| a1 | b1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
Inserted from SELECT result
mysql> SELECT * FROM test3;
+------+------+
| a1 | b1 |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.20 |
+-----------+
1 row in set (0.00 sec)
So this works for both auto_increment and explicitly inserted values.
Your problem must be somewhere else