Mysql Trigger to insert the last date - mysql

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

Related

Mysql column count doesn't Match value count (but it does!)

I am running the following procedure in mysql:
create procedure addSavingAccount(id int(10), account_number varchar(10))
begin
insert into saving values(id,'savings', account_number, 0);
end //
However, when I try to call it, it gives me this error:
mysql> call addSavingAccount(103, 'B505');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
I checked on anything that could be linked to it, including triggers. But everything seems like it should be working. Here is my list of triggers:
create trigger balance_change_saving after update on saving for each row
begin
if old.balance != new.balance
then
insert into balance_update_history values(null, 'saving', new.account_number, old.balance, new.balance,
(SELECT NOW()), (select USER()));
end if;
end//
create trigger balance_insert_saving after insert on saving for each row
begin
insert into balance_update_history values(null, 'saving', new.account_number, 0, new.balance, (select now()),
(select user()));
end //
create trigger balance_delete_saving after delete on saving for each row
begin
insert into balance_update_history values(null, null, null, old.balance, null,
(SELECT NOW()), (select USER()));
end //
And here is where I define the table:
create table if not exists saving(account_number varchar(10) , customer_id int(10), balance decimal(8,2), primary key(account_number));
I'd just really like to to figure this out.
There are three columns based on your table create statement, not four. (what is the last 0 in that insert?)
Also, in the procedure, it appears that your insert values are out-of-order relative to the table creation order? So you can either rearrange the insert values to match the table, OR specify the columns with the insert.

mysql: extract Timestamp in separate column as date and time with trigger

I want to set a trigger that execute to extract date and time in separate column in a same table when new row insert the table as shown in below:
What I have tried:
CREATE TRIGGER date_time_insert`
BEFORE INSERT ON tbl_raw_attendance
FOR EACH ROW
INSERT INTO tbl_attn_temp (TraineeID,
ScannerID,
attnDate,
attnTime )
VALUES( NEW.TraineeID,
NEW.ScannerID,
CURRENT_DATE,
CURRENT_TIME );`
thanks for any help.

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

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 ;

In a trigger how to insert values into another table but check if values already exists

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