I am using mysql work bench. I have two tables "new_machine"(parent table) and "machine_record" (child table). I am using after insert trigger to insert m_id and m_name from new_machine table to mac_id and mac_name from machine_record table.
m_id is primary key for new_machine.
m_id and m_name from new_machine table is foreign key to mac_id and mac_name from machine_record table.
when i insert data in new_machine it populates in machine_record but the first record entered is entered twice.
CREATE DEFINER=`root`#`localhost` TRIGGER `system_data`.`new_machine_AFTER_INSERT` AFTER INSERT ON `new_machine` FOR EACH ROW
BEGIN
insert into `system_data`.`machine_record` (mac_id, mac_name)
select m_id, m_name
from `system_data`.`new_machine`;
END
you can direct use the Values from your insert on new_machines. So
it is not necessary to get it with a SELECT. Also your
SELECT selects ALL Records from new_machines.
CREATE DEFINER=`root`#`localhost` TRIGGER `system_data`.`new_machine_AFTER_INSERT` AFTER INSERT ON `new_machine` FOR EACH ROW
BEGIN
insert into `system_data`.`machine_record` (mac_id, mac_name)
VALUES (NEW.m_id, NEW.m_name);
END
Related
I need to compare two tables that are linked by Foreign Key and add records from the master table that are not already in the linked table with foreign key. What's the best way to go about doing this please?
You can create a trigger on master table that will be triggered when there's a new insert to the master table.
DELIMITER //
CREATE TRIGGER master_after_insert
AFTER INSERT
ON linked_table FOR EACH ROW
BEGIN
-- Insert record into linked_table
INSERT INTO linked_table
( id,
some_value,
master_id)
VALUES
( 1,
'test value',
NEW.id ); // <-- using NEW you can get newly inserted record
END; //
this seemed to do the trick for me:
INSERT into table2 (foreignkey_id)
SELECT id FROM table1
WHERE id NOT IN (SELECT foreignkey_id FROM table2)
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 have created two tables into student database
1) Admission table (rollNO,Name,fname,address,department,addmission_date)
2) Fees Table(rollNO,Name,fname,address,department,Total_fees,Fees_Payment_date)
i want to insert same data of addmission table into fees table which contain same column names like rollNO,Name,fname,address,department using trigger's event "after"
how can i do it? if i insert data into addmission table it will also automatically insert into fees table????
Try this code, NEW is ref of admission table insert record contains
DELIMITER //
CREATE TRIGGER `fee_trigg` AFTER INSERT ON `admission`
FOR EACH ROW BEGIN
INSERT INTO fee (rollNO,Name,fname,address,department)
VALUES( NEW.rollNO, NEW.Name, NEW.fname,NEW.address,NEW.department );
END; //
DELIMITER ;
I have to make only one insertion / day on a Mysql table .
DELIMITER $$
CREATE TRIGGER trig
BEFORE INSERT ON Table
FOR EACH ROW BEGIN
/** compare the date with the latest inserted **/
IF () THEN
// if it's ok => insert
ELSE
// nothing
END IF;
END$$
DELIMITER ;
I want to compare the last inserted date and current date and then initiate my trigger to do the work.
Thanks
You can achieve this without trigger (which is a good thing right?) by creating a UNIQUE constraint on the date column and then using INSERT IGNORE syntax.
Suppose you have a table with the following schema
CREATE TABLE table1
(
date DATE,
value INT
);
Let's create a UNIQUE constraint
CREATE UNIQUE INDEX idx_date_unique ON table1 (date);
Now you can use IGNORE clause
INSERT IGNORE INTO table1 VALUES (CURDATE(), 1);
INSERT IGNORE INTO table1 VALUES (CURDATE(), 2);
INSERT IGNORE INTO table1 VALUES (CURDATE(), 3);
As a result you'll have only first row in the table for each day.
Here is SQLFiddle demo
Based on my previous post found here im able to insert the values to the 2nd table when the status on first table changes, but it keeps adding indiscriminately, i need to check if the submit_id has already been inserted into the 2nd table and then update the fields not insert it gain, how would i do that check before the trigger is executed?
Because the new.status and old.status refer to the row being edited not the row on table it's being inserted into, how can i compare that and insert or update if it already exists,
Thanks
You can use INSERT INTO ... ON DUPLICATE KEY UPDATE syntax for that
If order for it to work properly you have to have a UNIQUE constraint on submitId column in your second table (let's call it students).
ALTER TABLE students ADD UNIQUE (submitId);
Now an improved version of a trigger
DELIMITER $$
CREATE TRIGGER tg_au_submissions
AFTER UPDATE ON submissions
FOR EACH ROW
BEGIN
IF NEW.status = 1 THEN
INSERT INTO students (submitId, studentName, submitDate, contacts, email)
VALUES (NEW.submitId, NEW.studentName, NEW.submitDate, NEW.contacts, NEW.email)
ON DUPLICATE KEY UPDATE
studentName = VALUES(studentName),
submitDate = VALUES(submitDate),
contacts = VALUES(contacts),
email = VALUES(email);
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo