Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 27th, 2007, 09:45 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create trigger on update

I have a table in which has a column named "count".

How to write a SQL statement that will delete the row (automatically) in the given table when the count column value becomes 0.

Regards,
Phani
__________________
Regards,
Phani
 
Old April 27th, 2007, 09:55 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Normally you would use an Instead Of clause in your trigger but MySQL doesnt support it

So you would have to do something like

CREATE Trigger tgr_Delete After Update on [tbl]
Begin
  Delete from [tbl] where [CountColumn] = 0
End

You will have adjust this so MySQL likes it (adding ; and such) but the concept is the same.


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 27th, 2007, 10:08 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

dParsons,
  Here you are not deleting it automatically you are doing query for count = 0, in your delete sql statement. Cant it be done automatically?
 
Old April 27th, 2007, 10:24 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Well, define automatically. Automatically to me means that I don't have to issue a speicfic query against the database and, in this case, the trigger handles the delete hence automatically.

In anycase, if MySQL supported Instead Of you could check to see if the value getting updated to the count column was 0 and just discard the update, but it doesnt. You can use BEFORE INSERT/UPDATE and AFTER INSERT/UPDATE Again this will require you to write logic so I guess it isnt "automatic".

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 27th, 2007, 03:13 PM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What i meant was the row should get delete automatically without searching for it "where count =0". May be its not possible in sql. Thanks for your inputs

 
Old April 30th, 2007, 09:30 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem i am facing with the trigger is, it will get invoked whenever there is an update on the table, which is reducing the efficiency of the query. What i would like to have is it should not be triggered every time when there is an update, but it should be triggered only when the value becomes 0.

 
Old April 30th, 2007, 09:48 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Then you can't use a trigger. Triggers will fire everytime unless you explicitly disable them in your code.

I am curious though, how do you expect SQL to delete a single row, if you don't want to let it use a where clause? This seems a bit ubsurd.

In any case this IS possible in SQL using a Trigger, however, not within MySQL as I have previously stated. You will need to come up with some other logic that will eliminate or completely ignore the row from being inserted into the table.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old May 1st, 2007, 04:30 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Of course you cant delete a single row without using the where clause.

But after the updation of the refcount you cant check (immediately) whether the value has become 0, and if it is you can delete that row at that time, without searching the table for values with refcount 0.

its like

Update refcount = refcount - 1 where rowid = ?.
if(refcount == 0)
delete rowid = ?

The problem is that, here in this case you need to search for the given rowid in the table only once, whereas in sqlite you will need to search the table twice:
onc for updating the refcount for the given id.
Another for deleting the row whose refcount value is zero.

Will th query given below will do what i want?

CREATE Trigger tgr_Delete After Update on [tbl]
Begin
  Delete from [tbl] where [CountColumn] = 0 and rowid = old.rowid
End

But here also there is search involved for rowid in the given table.

Is there any sql query that will delete the row when the refcount value becomes zero, without searching the table twice?

 
Old May 1st, 2007, 06:48 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I am not sure that I understand what you are trying to do? You want to make an update to a new row and delete an old row, is that correct?

If that is the case, I might consider using a stored procedure that will insert a new row and also test the value of column in question and then do something based on that value.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old May 1st, 2007, 08:00 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Update refcount = refcount - 1 where rowid = A.

if(refcount == 0)
delete rowid = A

I want the above two lines to be executed in SQL. That is, when i update the refcount of a particular rowid (assume A) and if the refcount value becomes 0, i want to delete that row (A). Basically i dont want to search the table twice. I need a solution which does search the table only once.







Similar Threads
Thread Thread Starter Forum Replies Last Post
After Update Trigger debbiecoates SQL Server 2000 2 February 15th, 2008 04:55 AM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Problem with update trigger gbrown SQL Language 2 September 4th, 2004 12:33 PM
Help With INSERT + UPDATE Trigger HenryE SQL Server 2000 1 December 11th, 2003 06:26 PM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.