MySQL Forums
Forum List  »  Triggers

trigger not working on same database on two versions of mysql
Posted by: Liviu Vasut
Date: December 07, 2010 03:22AM

Hello,
I have a table which stores quantities of items and I needed a way to prevent clients to enter negative values. So I added a trigger to trow an error. On my development station (mysql 5.1.52 Source distribution) it works on both insert and update. On the production server (mysql 5.0.88-log Source distribution) it only works for inserts.

table definition:
CREATE TABLE `goods` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
...other fields...
`quantity` int(7) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
...other keys and constraints...
) ENGINE=InnoDB;

trigger definition:
DELIMITER |
CREATE TRIGGER `negative_insert`
BEFORE INSERT ON `goods`
FOR EACH ROW BEGIN
IF NEW.`quantity` < 0 THEN
SET NEW.`quantity` = 1 / 0;
END IF;
END;
|
CREATE TRIGGER `negative_update`
BEFORE UPDATE ON `goods`
FOR EACH ROW BEGIN
IF NEW.`quantity` < 0 THEN
SET NEW.`quantity` = 1 / 0;
END IF;
END;
|
DELIMITER ;

for testing I did:
(development station)
mysql> INSERT INTO goods (quantity) values (-1);
ERROR 1048 (23000): Column 'quantity' cannot be null
mysql> update goods set quantity =-1 where id=1;
ERROR 1048 (23000): Column 'quantity' cannot be null

(production server)
mysql> INSERT INTO goods (quantity) values (-1);
ERROR 1048 (23000): Column 'quantity' cannot be null
mysql> update goods set quantity =-1 where id=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> show warnings;
+---------+------+--------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------+
| Warning | 1048 | Column 'quantity' cannot be null |
+---------+------+--------------------------------+
1 row in set (0.00 sec)

As you can see it only generates a warning. How can I change it into an error?
Thank you for your time.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.