MySQL DB Trigger Insert after cost linked to id - mysql

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

Related

Mysql insert to main table with auto increment and link data in other table to the inserted main

I am not sure if the title explains it well, But I am sure that my explanation will explain it better:
I have a table called Tracks and a tble called Flocks.
each Track has many Flocks in it.
So when I insert a new track, a new ID is created with the AUTO_INCREMENT function, and in the same query, I want to insert the track's flocks aswell, but in order to make these flocks belong to the track I just inserted, I have to set their track_id to the auto incremented value.
I can do this in 3 queries, insert the Track, fetch the incremented ID, and then insert all flocks with the ID.
But I want to do this in one query, is that possible?
You need at least two queries unless you go for triggers or stored procedures:
Insert the track
use last_insert_id() as foreign key value to insert into flocks
example:
insert into track (name) values ('Trackname');
insert info flocks (trackid) select last_insert_id();
I normally group such tasks together in a stored procedure:
create procedure createTrack ( p_trackname varchar(20) ) as
begin
insert into track (name) values (p_trackname);
insert info flocks (trackid) select last_insert_id();
end;
And then call it this way:
call createTrack("Trackname");

how to use primary key as custId number in second table

I have two tables, the first has an auto incrementing ID number, I want to use that as custId in the second table.
I am using an insert into the first table with all the basic info, name, address etc. Then in the second table only 3 things, custId, stocknum, and location. How can I write to these two tables kinda of simultaneously since stockNum may have several values, but always attached to one custId. I hope this makes sense even without putting code in here.
You can't insert into multiple tables at the same time. You have two options. You either do two inserts
INSERT INTO table1 (col1, col2) VALUES ('value1',value2);
/* Gets the id of the new row and inserts into the other table */
INSERT INTO table2 (cust_id, stocknum, location) VALUES (LAST_INSERT_ID(), 'value3', 'value4')
Or you can use a post-insert trigger
CREATE TRIGGER table2_auto AFTER INSERT ON `table1`
FOR EACH ROW
BEGIN
INSERT INTO table2 (cust_id, stocknum, location) VALUES (NEW.id, value3, 'value4')
END
Hope this helps.
After inserting in the first table, The identity field or Auto increment field generate an ID
Get this id Refer Here(LAST_INSERT_ID() MySQL)
Then use this id to store value in the other table

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

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 ;

mysql insert and select all at once

I am new to MySQL so bear with me please. I have set up a member table and a session table. When insert a new record in member table, session table will grab the assigned new record ID from member table and automatically insert into session table. Is this possible?
so the order will be something like:
insert new record(member table) -> fetch id(member table) -> insert new record(session table)
-I have already set up a foreign key in session table, but I don't know what to do
-Im using php, if this matter..
easily you can use LAST_INSERT_ID();
your query can be like this:
INSERT INTO members (id, name)
VALUES('', 'test');
INSERT INTO sessions (member_id, description)
VALUES(LAST_INSERT_ID(),'some description');
in this case id is set auto increment.
at the moment you insert your data into member table by using (LAST_INSERT_ID) the last inserted id will store in session table.

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