mysqldude

5.5 Signals

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
SIGNAL in an AFTER INSERT trigger rejects the row (InnoDB, original)
original post
✓ 1644✓ 1644✓ 1644✓ 1644✓ 1644✓ 1644✓ 1644✓ 1644✓ 1644
After the error the InnoDB table holds only 'test'
original post
✓✓✓✓✓✓✓✓✓
Same AFTER trigger on MyISAM: the error is raised but the row stays
version check
✓✓✓✓✓✓✓✓✓
Single-table rules: CHECK constraint (enforced MySQL 8.0.16+, MariaDB 10.2+)
modern rewrite
≠≠✓ 3819✓ 3819✓ 3819✓ 4025✓ 4025✓ 4025✓ 4025
SIGNAL in an AFTER INSERT trigger rejects the row (InnoDB, original) original post
MySQL 5.6✓ 16445.7✓ 16448.0✓ 16448.4✓ 16449✓ 1644
MariaDB 10.6✓ 164410.11✓ 164411.4✓ 164411.8✓ 1644
After the error the InnoDB table holds only 'test' original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Same AFTER trigger on MyISAM: the error is raised but the row stays version check
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Single-table rules: CHECK constraint (enforced MySQL 8.0.16+, MariaDB 10.2+) modern rewrite
MySQL 5.6≠5.7≠8.0✓ 38198.4✓ 38199✓ 3819
MariaDB 10.6✓ 402510.11✓ 402511.4✓ 402511.8✓ 4025
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (1)
  • Single-table rules: CHECK constraint (enforced MySQL 8.0.16+, MariaDB 10.2+) (MySQL 5.6, MySQL 5.7): expected error [3819, 4025] did not occur

Related errors: 1644

The original article

First published 2011-02-11. The text below is the original; the table above shows how it behaves on current versions.

Signals have been introduced in MySQL 5.5. which allow exceptions to be raised. This was previously unavailable and has made it difficult to write stored solid stored procedures with exception handling. This post will show a trigger implementing data consistency checks and raising an exception. This is particularly good for implementing check constraints across multiple tables.

The scenario is that we have a database that contains numerous tables video, audio and pdf and inside each of these tables we have a field called resource name. The resource name must be unique in the database irrespective of the database. I know that this is not a good practice but this is just an example of how SIGNAL’s may be used in a trigger to determine the uniqueness of the resource name.

CREATE TABLE audio (id int auto_increment, resource_name varchar(255), ...., primary key (id));
CREATE TABLE video (id int auto_increment, resource_name varchar(255), ...., primary key (id));
CREATE TABLE pdf (id int auto_increment, resource_name varchar(255), ...., primary key (id));

So far example we need to verify when something is added in audio that no similar resource_name exists in any other of the tables. We are assuming sequential input from a single source.

DELIMITER $$
CREATE TRIGGER audio01
AFTER INSERT ON audio
FOR EACH ROW BEGIN
  DECLARE count INT;
  SELECT COUNT(1) INTO count FROM video WHERE resource_name = new.resource_name;
  IF count > 0 THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'Resource name not unique';
  END IF;
  SELECT COUNT(1) INTO count FROM pdf WHERE resource_name = new.resource_name;
  IF count > 0 THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'Resource name not unique';
  END IF;
END;
$$
DELIMITER ;

The result is we can now build solid applications with more complex data integrity constraints and improved exception handling.

Here is the code in action:

mysql> insert into audio(resource_name) values ('test');
Query OK, 1 row affected (0.02 sec)

mysql> select * from audio;
+----+---------------+
| id | resource_name |
+----+---------------+
|  1 | test          |
+----+---------------+
1 row in set (0.00 sec)

mysql> insert into video(resource_name) values ('test1');
Query OK, 1 row affected (0.06 sec)

mysql> insert into audio(resource_name) values ('test1');
ERROR 1644 (45000): Resource name not unique
mysql> select * from audio;
+----+---------------+
| id | resource_name |
+----+---------------+
|  1 | test          |
+----+---------------+
1 row in set (0.00 sec)