SQL Triggers
This probably is for most SQL servers, but I am using on mySQL 5.x:
To clarify, I know there are many ways to achieve this, but in my case the following is what we need to happen:
Creating a trigger that when a record is inserted, it sets another field in the same row to have it's auto-incremented id as part of the value
I've come very close, trying a AFTER INSERT trigger, however you cannot do an UPDATE on the same table whose trigger is firing.
CREATE TRIGGER ai_SetURL AFTER INSERT ON tblEvent
FOR EACH ROW UPDATE tblEvent SET EventURL=CONCAT("whatever.php?event=",EventID)
Anyone know of a way to do this?
Thanks.
-Greg
greg posted this at 17:57 — 5th March 2009.
He has: 1,581 posts
Joined: Nov 2005
Common sense, as when the table has a row inserted, the trigger would insert the new value in the same row, then trigger itself again with the triggered insert. Infinite loop.
I would have to say it's not possible then.
Some ideas you probably will have though of:
Have two tables, the main (usual) one and another only to store the new value with auto_inc id.
Run a second query using mysql_insert_id(), append to VALUE and update.
Or just insert the value without auto_inc_ID, then whenever using a select query, get the row ID and append it to the VALUE.
Greg K posted this at 18:18 — 5th March 2009.
He has: 2,145 posts
Joined: Nov 2003
I agree with the possibility of doing and insert while an insert trigger would cause a loop, but you'd think you could issue an update from an insert as long as you don't have an update trigger
Yes, thought of each of those items, but for what this is being used for the client access the row though a default "row editor", so was wanting to do this without having to write a custom editor for this.
On my home server, I'm going to try havig the second table, that gets updated with the AFTER INSERT. and then on that one, have an AFTER UPDATE which does an update back to the first... Just curious if the server will let this happen or not. For production, I don't like that idea.
For now I found a spot to throw a second SQL to preform the update, however it does an update on all rows, and since records auto delete after a set period of time, will never be more than 100 rows.
I'm still looking for another way though...
-Greg
greg posted this at 18:34 — 5th March 2009.
He has: 1,581 posts
Joined: Nov 2005
My quote is from MYSQL.com, and it doesn't specify alternative query types are acceptable, but I know what you're saying ...
pr0gr4mm3r posted this at 18:33 — 5th March 2009.
He has: 1,502 posts
Joined: Sep 2006
Why not concat it on the selecting?
SELECT CONCAT("whatever.php?event=",EventID) AS EventURL FROM tblEvent
Or, depending on how you are displaying the data, concat it in the HTML template/PHP string/whatever.
greg posted this at 18:45 — 5th March 2009.
He has: 1,581 posts
Joined: Nov 2005
That defeats the client being able to edit the field with the VALUE and ID already merged
pr0gr4mm3r posted this at 18:47 — 5th March 2009.
He has: 1,502 posts
Joined: Sep 2006
Yes - insert the data normally, and then concat that extra bit of data when you select that row from the DB later.
greg posted this at 18:49 — 5th March 2009.
He has: 1,581 posts
Joined: Nov 2005
Who said that?
I changed my post (same time as you where writing) to give the reason why it's not what he wants.
But dammit you must have been pressing refresh until someone replied !
Shaggy posted this at 19:00 — 5th March 2009.
They have: 121 posts
Joined: Dec 2008
I'm still a little... not sure of the emotion.. that MySQL doesn't support your trigger. I'm not buying the 'infinite loop' argument, no matter who's selling.
You're trigger is fired 'on insert'
you're trigger is performing an update.
You know, in a sane db that has sequences, you could just:
insert into test
(id,
val)
values
(nextval('sequence_name')
, 'url?id=' || currval('sequence_name'));
I know, you're all jealous of my MySQL free environment now, aren't you! -- this post provided no help to anyone... anywhere. It's been a long week.
Cheers,
Shaggy
greg posted this at 20:51 — 5th March 2009.
He has: 1,581 posts
Joined: Nov 2005
you're trigger is performing an update.
Logically yes, but seemingly you cannot update the same table that invoked the trigger, regardless of difference between query and trigger event type (possibly something to do with the MYSQL table lock- dunno).
It returns an error though, and there are bugs reported (and people venting their frustration) about this around the net, including on dev.mysql.com.
There are ways around certain requirements by using a BEFORE trigger.
But as trying to get next value of a MYSQL auto inc is a bad idea, Greg needs to do the actual insert first to obtain the auto increment ID to append to the 'VALUE' on UPDATE, using BEFORE wont work for him.
(also, obviously there would be no row to update to before the insert)
Link to an example
shivendra posted this at 05:22 — 23rd May 2013.
They have: 9 posts
Joined: May 2013
CREATE TRIGGER ai_SetURL AFTER INSERT ON tblEvent
FOR EACH ROW UPDATE tblEvent SET EventURL=CONCAT("whatever.php?event=",EventID)
teammatt3 posted this at 05:15 — 6th March 2009.
He has: 2,102 posts
Joined: Sep 2003
Building on pr0g's idea, you could create a view using that concat logic. Your client's editor probably wouldn't know the difference between a view and a table.
Another thing you might want to try is putting your update logic in a stored proc, and calling the stored proc in your trigger.
Tanmay posted this at 04:11 — 23rd May 2013.
He has: 8 posts
Joined: May 2013
Triggers are used to perform particular action whenever some insert, update, delete commands executed, they are used to control, monitor and manage group of tables.
make your password secure
easy to hack
semaphore.v posted this at 06:23 — 20th June 2013.
They have: 3 posts
Joined: Feb 2013
An SQL trigger cannot be directly called from an application. An SQL trigger is invoked by the database management system on the execution of a triggering insert, update, or delete operation.
Visite our site Semaphore Softwares for magento customization
Email : [email protected] Phone : +1 201 299 3529
497 Route 27, Iselin, NJ 08830 United States
tejayasarapu posted this at 10:55 — 26th November 2013.
They have: 30 posts
Joined: Nov 2013
thanks for info
rosyhyden posted this at 11:32 — 3rd December 2013.
They have: 2 posts
Joined: Nov 2013
Hello Gusy......
Look keyword so some write content and click linking keyword so take more than more knowledge.....................
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.