I have a table called customer which has, among others, a column called name and a column called first_name_start. I want first_name_start to be equal to SUBSTRING(name, 1, 4). How would I create a trigger that makes sure this happens?
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
SET NEW.first_name_start = SUBSTRING(NEW.name, 1, 4);
END$$
CREATE TRIGGER trigger_name BEFORE UPDATE ON your_table
FOR EACH ROW
BEGIN
SET NEW.first_name_start = SUBSTRING(NEW.name, 1, 4);
END$$
DELIMITER ;
Unfortunately, I didn't get time to test this, it might be completely wrong.
Why not just make a view which adds the first_name_start if you really need it as a column? Then you won't be storing redundant data, but you will be able to use it as if you were.
Related
I have three columns column 1, column 2, column 3(total). i want to put the sum of column 1 and column 2 into column 3 after every update query..... note i am using code igniter.. how can i do it if i update col 1 then total column automatically update itself.
Try like this:
DELIMITER $$
CREATE TRIGGER updtrigger AFTER UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.col1 <> OLD.col1 OR NEW.col2 <> OLD.col2 THEN
SET NEW.col3 = NEW.col1 + New.col2;
END IF;
END $$
Here is a very good tutorial.
An AFTER UPDATE Trigger means that MySQL will fire this trigger after
the UPDATE operation is executed.
why dont you just update the db instead of creating a trigger. plus youve created the trigger after the insert, it should go before. but really you shouldn't be doing it that way
try this code:
DELIMITER $$
CREATE
TRIGGER `db_name`.`trigger_name` AFTER UPDATE
ON `db_name`.`table_name`
FOR EACH ROW BEGIN
UPDATE
`table_name`
SET
`column3` = `column1` + `column2`;
END$$
DELIMITER ;
I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.
I have a table with 2 columns named "Table1":
(Column 1 named "Col1" with the values: A,B,C,D,E,F)
(Column 2 named "Col2" with the values: 12,15,2,5,200,1).
I would like get all the values from column 2 to change to the value 1 if their value is lower than 100, so that column 2 will eventually look like this:
(Column 2 named "Col2" with the values: 1,1,1,1,200,1).
I tried to create a trigger:
delimiter //
CREATE TRIGGER Table1upd BEFORE UPDATE ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;
When creating the trigger in MySQL workbench it says that the trigger was added but that 0 row(s) affected.
I assume my problem is with the BEFORE UPDATE choice, because the trigger does work when I update a value in the table, but I don't know what to change it to so that the trigger will also initially execute automatically when I create it.
Thank you in advance for any help,
D
The answer is simple: a BEFORE UPDATE trigger only triggers on UPDATE, so you need a second trigger BEFORE INSERT to cover both use cases: update and insert:
delimiter //
CREATE TRIGGER Table1insert BEFORE INSERT ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;
I'm trying to create a trigger, however I keep getting back a syntax error.
Here's the statement:
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END
DELIMITER ;
I have a registration form on my site. When a person registers it populates the Candidates table with their profile information.
In the Candidates table, there are various fields, two of them being 'email' and 'UserID'.
UserID is also the PK in 'useradmin', so I'm linking the two up.
So when a user registers, I need to insert a record into 'useradmin' with the email address that's just been used to register, and then update the 'Candidates' table, with UserID that's just been created in 'useradmin'.
I hope this makes sense?
NB. I am changing the delimiter before running the statement.
Besides properly using DELIMITER when creating a trigger you have at least two fundamental issues with your current code:
In MySQL you can't use issue a DML statement (in your case UPDATE) against a table (candidates) on which you defined a trigger (also candidates). Your only option is to use BEFORE trigger and set a value of userid column of a row being inserted to a proper value.
You can't arbitrarily reference a column (useradmin.userid) of a table out of the context like you did in your UPDATE. You didn't joined useradmin table or used it in a subquery.
That being said and assuming that userid in useradmin table is an auto_increment column your trigger might look like this
DELIMITER $$
CREATE TRIGGER after_candidate_insert
BEFORE INSERT ON candidates
FOR EACH ROW
BEGIN
INSERT INTO useradmin (`username`, `talent`) VALUES (NEW.email, 1);
SET NEW.userid = LAST_INSERT_ID();
END$$
DELIMITER ;
Here is SQLFiddle demo
You should use semicolon after end your insert query.
You can use INSERT ... ON DUPLICATE KEY UPDATE syntax for your purpose
try out this...
DELIMITER $$
CREATE TRIGGER `swtalentbank`.`after_candidate_insert`
AFTER INSERT ON `Candidates` FOR EACH ROW
BEGIN
INSERT INTO useradmin (username, talent)
VALUES (NEW.email, 1);
UPDATE `Candidates` SET UserID = useradmin.userid where useradmin.username = NEW.email;
END $$
DELIMITER ;
I am working on a project where I need my ID column to be a power of 2 (1,2,4,8,16..). I know that we cannot offset the auto_increment but for simple addition/subtraction in my.cnf.
Example:
id
----
1
2
4
8
16
32
64
128
etc
One of the ideas I had was to use the auto increment functionality as the base, and then create a trigger to apply the power of 2 and update the new ID, but unfortunately, it is not working:
DELIMITER $$
CREATE TRIGGER testbitcompatid BEFORE INSERT ON Table
FOR EACH ROW
BEGIN
SET NEW.id = pow(NEW.id, 2)
END;
$$
DELIMITER ;
Because the BEFORE INSERT has not yet generated the AUTO_INCREMENT id, the AUTO_INCREMENT will always return 0, essentially causing no change on the columns.
I also tried AFTER INSERT:
DELIMITER $$
CREATE TRIGGER testbitcompatid AFTER INSERT ON Table
FOR EACH ROW
BEGIN
SET Table.id = pow(NEW.id, 2) WHERE id = NEW.id;
END;
$$
DELIMITER ;
But this failed because you cannot change values of the table which the trigger is applied to during an AFTER INSERT.
Scratching my head, but I am sure someone else has a great way of accomplishing this.
To work around all the issues above, I was able to construct the following which works great!
DELIMITER $$
CREATE TRIGGER testbitcompatid BEFORE INSERT ON Table
FOR EACH ROW
BEGIN
SET #LAST_ROW = (SELECT MAX(id) FROM Table);
SET NEW.id = CASE WHEN #LAST_ROW IS NULL THEN 1 ELSE #LAST_ROW * 2 END;
END;
$$
DELIMITER ;
Pretty much, we take the highest id, grab the log(2) of it which gives us the corresponding AUTO_INCREMENT id. We then add 1, and power that up to 2.
I hope this helps prevent some headache down the road for others.