trigger to capitalise inserted field - mysql

suppose for the table student if insert query is executed as INSERT INTO student(id,name) VALUES (1,'sumit');.Just after insertion of the row I want the recently inserted row's name field value to be capitalised using trigger.I searched for it every where but couldn't get working code,please some body help?

You can write the after insert and update trigger on table.
CREATE TRIGGER lcase_insert BEFORE INSERT ON my_table FOR EACH ROW
SET NEW.name = LOWER(NEW.name);
CREATE TRIGGER lcase_update BEFORE UPDATE ON my_table FOR EACH ROW
SET NEW.name = LOWER(NEW.name);
here NEW.name is your name inserted in table.

Related

MySQL-Update inserted auto increment primary key to custom value

When I insert a new row, an auto increment ID will be saved like 1,2,3..
I need to custom it using a Trigger so it will be saved like 20171,20172,20173..
This trigger would do this.
CREATE DEFINER=`root`#`%` TRIGGER `test_before_insert` BEFORE INSERT ON `test` FOR EACH ROW BEGIN
SET NEW.id = (
SELECT CONCAT(YEAR(CURDATE()),IFNULL(MAX(CAST(ids.id AS UNSIGNED))+1,1))
FROM (
SELECT RIGHT(t.id,LENGTH(t.id)-4) AS id
FROM test t
WHERE LEFT(t.id,4) = YEAR(CURDATE())
) ids
);
END
But there are many reason you would not want to. This will get exponentially more expensive as rows are inserted, and provides no viable sorting etc.

MySQL trigger to insert date to row with unique matching ID

I need to write an SQL trigger to insert the date into a row of another table after an insert.
The data comes in as such:
A form is submitted to _OnsiteTable which includes a JobID, this JobID matches a current entry containing a JobID value in _JobTable. Upon insert of the data into _OnsiteTable I need a trigger that will insert the current date into a column called JobClosedDate in JobTable.
It needs to insert the date into the row that matches the JobID value entered in the form on _OnsiteTable.
I have this so far but I don't know how to compare the JobID values between tables and insert into the specific row.
CREATE TRIGGER UpdateClosedDate
AFTER INSERT on _OnsiteTable
BEGIN
Declare ClosedDate DATETIME;
SET ClosedDate = CURDATE();
CASE WHEN (ClosedDate) = ?
THEN INSERT INTO `_JobTable`( `JobClosedDate`)
VALUES
( ClosedDate);
END CASE;
END
Any help is greatly appreciated!
Complete query now working, thank you! As below:
CREATE TRIGGER UpdateClosedDate
AFTER INSERT on _OnsiteTable
BEGIN
Declare ClosedDate DATETIME;
SET ClosedDate = CURDATE();
UPDATE `_JobTable` SET `JobClosedDate` = ClosedDate WHERE `JobID` = new.JobID;
END
Since the problem was solved with the given comment I will add it here to reference for others with similar issues:
Since you want to Update a column ("insert a value into a column...") You just need to fix your trigger changing your current case statement to an update command as follow:
UPDATE `_JobTable`
SET `JobClosedDate` = ClosedDate
WHERE `JobID` = NEW.JobID;

sql trigger-how to trigger an insert that update another table

I have two tables named CustomerGoods(with column quantity) and Stock(with column Availablequantity). I want a trigger to occur when I insert into the CustomerGoods so that the quantity is added into Availablequantity in the Stock table.
You have not provided proper and to the point problem, but as per my understanding from what you have written.
Please try this.
CREATE
TRIGGER `AvailablequantityTrig` AFTER INSERT ON `CustomerGoods`
FOR EACH ROW BEGIN
UPDATE Availablequantity set quantity=quantity+1 where GoodsID = NEW.id;
END;
Here NEW.id is the id of goods and GoodsID is the id of goods in Availability table..
Please change your column names as required..
Thanks.

MySQL Multiplication trigger?

I have a table called history with the fields id, g.id, date, value. I want to put a trigger that will update the table when a new row is inserted and divide the number inserted in the value field by 2.
I have been trying for hours with no luck, any help would be appreciated.
for eg, after
INSERT INTO `online_game_shop`.`history`
(`id`, `gameID`, `dateofPurchase`, `Value`)
VALUES ('1001', '101', '2014-02-22', '10');
so the trigger will automatically divide 10 by 2 and update the field with result.
CREATE TRIGGER pointstovalue
AFTER INSERT ON history
FOR EACH ROW
BEGIN
UPDATE history
SET value = new.value/2
WHERE history.id = NEW.id
END;
You want a before insert trigger:
CREATE TRIGGER pointstovalue
BEFORE INSERT ON history
FOR EACH ROW
BEGIN
set new.value = new.value/2;
END;

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