Unix Technical Forum

CREATE TRIGGER BEFORE ...

This is a discussion on CREATE TRIGGER BEFORE ... within the MySQL forums, part of the Database Server Software category; --> Hi, I have a table used to store frequently updated datas. Only the last data available must replace the ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:33 AM
Florent Clairambault
 
Posts: n/a
Default CREATE TRIGGER BEFORE ...

Hi,

I have a table used to store frequently updated datas. Only the last
data available must replace the other. This means that every time I add
some data, I must check if the date column is lower than the date of the
data's date i'm inserting.

Right now, I have a SELECT to get the date and a REPLACE to insert
data. But I must try to keep the lowest number of queries. So I would
like to do a CREATE TRIGGER which applies to "REPLACE INTO" queries.

I have no idea on how to do that, because creating TRIGGER seems to
apply on INSERT or DELETE but not directly REPLACE. But if there is a
INSERT query, that means that a DELETE one has already been made. But it
should have been made if the date to insert is lower than the date
already stored. Or is it managed in a transactionnal way (if we cancel
the INSERT, the DELETE is also cancelled).

But even if the knowledge of that, I have no id on how to say
"check if the date of the row that will be replaced is not newer".

Thank you for your answers

Florent
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:33 AM
ThanksButNo
 
Posts: n/a
Default Re: CREATE TRIGGER BEFORE ...

On Feb 20, 2:28 pm, Florent Clairambault
<Florent_ReMoVe_Th...@clairambault.fr> wrote:
> Hi,
>
> I have a table used to store frequently updated datas. Only the last
> data available must replace the other. This means that every time I add
> some data, I must check if the date column is lower than the date of the
> data's date i'm inserting.
>
> Right now, I have a SELECT to get the date and a REPLACE to insert
> data. But I must try to keep the lowest number of queries. So I would
> like to do a CREATE TRIGGER which applies to "REPLACE INTO" queries.
>
> I have no idea on how to do that, because creating TRIGGER seems to
> apply on INSERT or DELETE but not directly REPLACE. But if there is a
> INSERT query, that means that a DELETE one has already been made. But it
> should have been made if the date to insert is lower than the date
> already stored. Or is it managed in a transactionnal way (if we cancel
> the INSERT, the DELETE is also cancelled).
>
> But even if the knowledge of that, I have no id on how to say
> "check if the date of the row that will be replaced is not newer".
>
> Thank you for your answers
>
> Florent


I'm not at all sure of what it is you're trying to do. Perhaps an
example or two might illuminate your situation better?

However, it should be noted that if you run the queries independently,
or if the trigger runs the queries for you, you're not saving any
time. Triggers generally don't buy you performance, but rather enforce
the business rules at the database level.

E.g., if you need to do a SELECT then test for a condition then INSERT
if true, it won't matter whether you do it in the application or the
trigger does it. There's still the time used by SELECT, test, then
INSERT.

\:|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:33 AM
Florent Clairambault
 
Posts: n/a
Default Re: CREATE TRIGGER BEFORE ...

> I'm not at all sure of what it is you're trying to do. Perhaps an
> example or two might illuminate your situation better?


I have a table with :
key(uint PK), data(TEXT), date(DATETIME)

I want that every time I replace some data, by inserting (key, data)
with a REPLACE INTO it checks if the row that will be replaced has
an older date.
If it's not the case, I want him to cancel the transaction.

I'm sure it will be faster because the server which insert data isn't
really fast will have to make this check nearly every second in
production stage.

Florent

>
> However, it should be noted that if you run the queries independently,
> or if the trigger runs the queries for you, you're not saving any
> time. Triggers generally don't buy you performance, but rather enforce
> the business rules at the database level.
>
> E.g., if you need to do a SELECT then test for a condition then INSERT
> if true, it won't matter whether you do it in the application or the
> trigger does it. There's still the time used by SELECT, test, then
> INSERT.
>
> \:|

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:33 AM
Michael Austin
 
Posts: n/a
Default Re: CREATE TRIGGER BEFORE ...

Florent Clairambault wrote:
>> I'm not at all sure of what it is you're trying to do. Perhaps an
>> example or two might illuminate your situation better?

>
> I have a table with :
> key(uint PK), data(TEXT), date(DATETIME)
>
> I want that every time I replace some data, by inserting (key, data)
> with a REPLACE INTO it checks if the row that will be replaced has
> an older date.
> If it's not the case, I want him to cancel the transaction.
>
> I'm sure it will be faster because the server which insert data isn't
> really fast will have to make this check nearly every second in
> production stage.
>
> Florent
>
>>
>> However, it should be noted that if you run the queries independently,
>> or if the trigger runs the queries for you, you're not saving any
>> time. Triggers generally don't buy you performance, but rather enforce
>> the business rules at the database level.
>>
>> E.g., if you need to do a SELECT then test for a condition then INSERT
>> if true, it won't matter whether you do it in the application or the
>> trigger does it. There's still the time used by SELECT, test, then
>> INSERT.
>>
>> \:|


Example:
mysql> create table b (a1 integer,a2 integer, primary key (a1));
Query OK, 0 rows affected (0.81 sec)

mysql> insert into b values (1,2),(2,2);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0


mysql> select * from b;
+----+------+
| a1 | a2 |
+----+------+
| 1 | 2 |
| 2 | 2 |
+----+------+
2 rows in set (0.00 sec)

mysql> insert into b (a1, a2) values(1,2) on duplicate key
-> update b.a2 = IF(VALUES(a2) < b.a2,b.a2,VALUES(a2));
Query OK, 2 rows affected (0.00 sec)

IF new value is less than old value use old value else use new value


mysql> select * from b;
+----+------+
| a1 | a2 |
+----+------+
| 1 | 2 |
| 2 | 2 |
+----+------+
2 rows in set (0.00 sec)

mysql> insert into b (a1, a2) values(1,3) on duplicate key
-> update b.a2 = IF(VALUES(a2) < b.a2,b.a2,VALUES(a2));
Query OK, 2 rows affected (0.00 sec)

mysql> select * from b;
+----+------+
| a1 | a2 |
+----+------+
| 1 | 3 |
| 2 | 2 |
+----+------+
2 rows in set (0.01 sec)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com