How to insert same data into multiple tables in mysql using triggers - mysql

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 ;

Related

how can i insert records of one table to another using trigger in mysql

i have 1 table as emp_demo and another table as emp_demos with same columns as
eg:
emp_demo_id|emp_demo_name
1 | xyz
i want to add AFTER INSERT trigger on emp_demo table so as soon as a record is inserted in emp_demo it should automatically get inserted into emp_demos table.
CREATE TRIGGER after_insert_trig
AFTER INSERT
ON emp_demo
FOR EACH ROW
BEGIN
INSERT INTO emp_demos(emp_demo_id, emp_demo_name)
VALUES(new.emp_demo_id, new.emp_demo_name);
END;
Demo.

MySQL DB Trigger Insert after cost linked to id

first Trigger I created.
I have two tables, student and cost.
If I insert a new student I want to automatically insert a cost row for this student in the table cost and insert the corresponding student id to the cost.
I donĀ“t know how I can link the student id to the cost...
CREATE TRIGGER `add_cost` AFTER INSERT ON `student` FOR EACH ROW INSERT INTO cost (amount) VALUES (2000)
Thanks everybody!!
In your insert statement please use just:
NEW.{primary_key}
from related table(in your case student). If your primary is id this should looks like:
NEW.id
NEW is created after row is inserted and contain all inserted values + PK after insert.
Finally your query should looks like below:
INSERT INTO cost (student_id, amount) VALUES (NEW.id, 2000)
You can find more info on http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

Mysql after insert trigger inserting 1st row twice

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

Mysql Trigger to insert the last date

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

MySQL - Trigger that will automatic insert on other table

How can I create a trigger that will insert data into another table.
For example: I have a department table and a course table and the course table has a foreign key named department_id.
I insert CICCT into the course table but there is no CICCT in the department table.
My goal is to make the trigger add the value in the department table. How can I do this?
I tried this, is this correct?
CREATE TRIGGER department_insert BEFORE INSERT
ON course
FOR EACH row
begin
INSERT INTO department
(department_id,
description)
VALUES (new.id_dep,
NULL);
end;
Another question in the same subject, what do I need to do in order reduce the room capacity by one when a student enrolls?
Your trigger might look like this
CREATE TRIGGER department_insert
BEFORE INSERT ON course
FOR EACH ROW
INSERT IGNORE INTO department (department_id, description)
VALUES (NEW.id_dep, NULL);
Note:
to access column values of a row being inserted you have to use the NEW keyword
NEW.id_dep
^^^
use IGNORE clause in INSERT to ignore department_id values if they already exist in department table. It allows to greatly streamline your code.
Here is SQLFiddle demo
Make a separate post for your second question and specify table schemas