I have these two table called "cases" and attendance respectively which has four columns:
cases-
id empid reaction date_t
1 EMP12654 interested 2017-09-22
attendance-
id empid logintime logouttime date_t flag workinghours call_att
1 EMP12654 00:14:49 05:14:49 2017-09-18 set 6 1
What I want to do is create a trigger on cases table that updates call_att column of attendance table with number of entries in reaction column of cases table, this is what I have tried so far
CREATE DEFINER=`root`#`localhost` TRIGGER `number_call`
AFTER INSERT ON `cases` FOR EACH ROW
BEGIN UPDATE attendance set call_att=call_att +1
WHERE empid=new.empid AND date_t=new.date_t; END
But that doesn't seem to work. I am quite new to triggers.
try this
CREATE TRIGGER number_call
AFTER INSERT ON cases
FOR EACH ROW
BEGIN
UPDATE attendance set call_att=(select count(*) from cases where empid=NEW.empid )
date_t=NEW.date_t;
END
Related
I have two tables I'm working with: Records and Invoice. Invoice contains a column for the primary key of Records to be stored as a foreign key.
I'm looking to create a trigger. Immediately when a new row is generated in Records, I want a new row to also be created in Invoice, and I want the PK from Records to be inserted into the corresponding column in invoice.
For example, let's say the tables are Records(RecordsID) and invoice(invoiceID, RecordsID)
When new row created in tbl Records
Create new row in tbl Invoice and insert new Records.RecordID into new invoice.invoiceID
I'm aware this is most likely very far off, but here is the trigger I've been working on:
DELIMITER $$
create trigger new_invoice
after insert
on main for each row
begin
if new.RecordID is not null then
insert into invoice(RecordID)
values(new.RecordID(new.RecordID));
END IF;
END$$
DELIMITER ;
Any assistance will be greatly appreciated.
Thank you.
Yes you have it almost right
CREATE tABLE main(RecordID int)
CREATE tABLE invoice (RecordID int)
create trigger new_invoice
after insert
on main for each row
begin
if new.RecordID is not null then
insert into invoice(RecordID)
values(new.RecordID);
END IF;
END
INSERT INTO main VALUES (1)
SELECT * FROM invoice
| RecordID |
| -------: |
| 1 |
db<>fiddle here
I am creating a query, wherein i have created select query from table 1 and then inserting values to table 2, but how can create a command that when new data comes in table 1 then update table 2 values
and this insertion happen daily with job.
I tried creating trigger but i don t know how i can relate this to my scenario
IF OBJECT_ID('TEMPDB..#temp')IS NOT NULL
DROP TABLE #temp
Select Getdate()[Data Till],Sum[Quantity] Qty
into #temp from XYZ w
here w.Date <=Getdate()
Insert into dbo.table1
Select [Data Till],Qty from #temp >
As of now data is coming in table 2 like
1)[Data Till]-2019/05/22 Qty-100
2)[Data Till]-2019/05/23 Qty-150
3)[Data Till]-2019/05/24 Qty-120
Now what if in 2019/05/22 back dated entry of qty -20 comes in table 1 and how will table 2 has to update values in table 2
1)[Data Till]-2019/05/22 Qty-80
2)[Data Till]-2019/05/23 Qty-150
3)[Data Till]-2019/05/24 Qty-120
CREATE TRIGGER [schema_name.]trigger_name
ON Table_name
AFTER INSERT
AS
BEGIN
-- write your update statement here
END
This is how you can write after update trigger that executes your statement between 'BEGIN' and 'END' when you insert new row into 'Table_name'.
AS MY ASSUMPTION
Update means you have the data in table 2 already. If the new data comes in table 1 what kind of update you want to do ?
is it existing data update or new record insert then update.
From your question if it is updated above two kinds of things can be happened
For this why you have two write insert trigger
create trigger abc
on table1
For insert
as
begin
--update table2
end
I have two tables:
Table 1 : parts
id (primary key)
code
title
quantity
Table 2 : bill_items
id (primary key)
bill_id
parts_id: refers to primary key of table parts
qty
I would like to update parts table - qty every time I create a row in table bill_items. The qty in parts table is to be decremented by qty of bill_items. There may be N number of updates to table bill_items in one go. Would like to use a single INSERT....ON DUPLICATE or UPDATE statement.
Thank you for your time.
I think for this case better using trigger :
DELIMITER $$
CREATE
TRIGGER `bill_items_after_insert` AFTER INSERT
ON `bill_items`
FOR EACH ROW BEGIN
UPDATE parts set quantity = quantity - NEW.qty WHERE id = NEW.parts_id;
END$$
DELIMITER ;
I suggest you also make trigger for UPDATE and DELETE also for data consistency.
UPDATED
Based on your comment, it is possible to use normal insert and update using transaction for consistency data :
START TRANSACTION;
INSERT INTO bill_items (bill_id, parts_id, qty) VALUES (your_bill_id,your_parts_id,your_qty);
UPDATE parts SET quantity = quantity - your_qty WHERE id = your_parts_id;
COMMIT;
I need to create a trigger (after insert on one table) on MySQL, but the action needs to join 2 tables, for inserting into a third table. My script below returns no error, but the row is not inserted into the third table.
The first table (on which the after-insert trigger should work):
Z_TAXO
ID term_ID taxo_name
1 1 dept
2 2 staff
3 4 course
4 5 dept
The second table to be joined in the trigger:
Z_TERM
term_ID name
1 Engineering
2 Andy
4 Metallurgy
5 Business
6 Arts
The third table. If the Z_TAXO table has a new row with taxo_name = "dept", the row (joined with table Z_TERM) needs to be inserted into this table:
Z_DEPTS
ID dept_name
1 Engineering
4 Business
I created a trigger:
delimiter //
CREATE TRIGGER TRG_NEW_DEPT
AFTER INSERT ON Z_TAXO
FOR EACH ROW
BEGIN
DECLARE _dept_ID bigint(20);
DECLARE _dept_name varchar(200);
IF Z_TAXO.taxo_name = "DEPT" THEN
BEGIN
SELECT Z_TAXO.ID INTO _dept_ID FROM Z_TAXO, Z_TERM
WHERE Z_TAXO.ID = new.Z_TAXO.ID AND Z_TAXO.term_ID = Z_TERM.term_ID;
SELECT Z_TERM.name INTO _dept_name FROM Z_TERM, Z_TAXO
WHERE Z_TAXO.term_ID = Z_TERM.term_ID AND Z_TAXO.ID = new.Z_TAXO.ID;
INSERT INTO Z_DEPTS (ID, dept_name) VALUES (_dept_ID, _dept_name);
END;
END IF;
END//
delimiter ;
Then inserted a row to the Z_TAXO table:
INSERT INTO Z_TAXO (ID, term_ID, taxo_name) VALUES (5, 6, "dept");
Expecting to have this new row in table Z_DEPTS:
ID dept_name
5 Arts
But when I select * from Z_DEPTS, the result is still:
ID dept_name
1 Engineering
4 Business
What can be wrong? I can't modify the design of the tables, because they came from a wordpress Plugin. Thanks in advance!
Couple of comments about your code. 1) When using new. qualifiers you don't further qualify with the table name so new.z_taxo.id is invalid andd should simply be new.id 2) You don't need a begin..end block in a mysql if statement 3) if just doesn't make sense referring to the table z_taxo in your select stataments - a simple insert select will do.
try
drop trigger if exists trg_new_dept;
delimiter //
CREATE TRIGGER TRG_NEW_DEPT
AFTER INSERT ON Z_TAXO
FOR EACH ROW
BEGIN
INSERT INTO Z_DEPTS (ID, dept_name)
select term_id, name
from z_term
where term_id = new.term_id;
END//
delimiter ;
I am adding new user to database user_data. My trigger has to store id of semester (from insert query) on high school and then select from another database list of subjects, which are taught this semester.
At the end it has to insert into grades table rows which constains id of user, id of semester, and id of subject. I want to add that there are multiple subjects in semester.
Here is a sample trigger:
CREATE DEFINER=`root`#`localhost` TRIGGER `data_shipment_extras_BEFORE_INSERT` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
DECLARE semester_id DATETIME;
SELECT
semester_id
INTO #semester_id
FROM other_database.subjects s
WHERE s.user_id = NEW.id;
SET NEW.semester_id = #semester_id;
END
Simply modify it a bit for your needs.