trigger not working on same database on two versions of mysql
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.
Subject
Views
Written By
Posted
trigger not working on same database on two versions of mysql
3803
December 07, 2010 03:22AM
1386
December 07, 2010 10:15AM
1380
December 08, 2010 06:36AM
1422
December 08, 2010 11:27AM
1683
December 09, 2010 12:28AM
1281
December 09, 2010 09:44AM
1388
December 10, 2010 12:26AM
1196
December 10, 2010 10:35AM
1692
December 11, 2010 12:31AM
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.