mysqldude

Runtime values

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Post's CREATE TABLE with int(3)/int(6) and CHARSET=utf8
original post
✓✓⚠ 1681,3719⚠ 1681,3719⚠ 1681,3719✓✓✓✓
Post's trigger (NEW.WITHDRAWL typo) then INSERT
original post
✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054✗ 1054
Trigger with the column name fixed keeps a running balance
original post
✓✓✓✓✓✓✓✓✓
Compute the balance at query time with SUM() OVER (MySQL 8.0+, MariaDB 10.2+)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Post's CREATE TABLE with int(3)/int(6) and CHARSET=utf8 original post
MySQL 5.6✓5.7✓8.0⚠ 1681,37198.4⚠ 1681,37199⚠ 1681,3719
MariaDB 10.6✓10.11✓11.4✓11.8✓
Post's trigger (NEW.WITHDRAWL typo) then INSERT original post
MySQL 5.6✗ 10545.7✗ 10548.0✗ 10548.4✗ 10549✗ 1054
MariaDB 10.6✗ 105410.11✗ 105411.4✗ 105411.8✗ 1054
Trigger with the column name fixed keeps a running balance original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Compute the balance at query time with SUM() OVER (MySQL 8.0+, MariaDB 10.2+) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓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 (3)
  • Post's CREATE TABLE with int(3)/int(6) and CHARSET=utf8 (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1681: Integer display width is deprecated and will be removed in a future release.; warning 3719: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8
  • Post's trigger (NEW.WITHDRAWL typo) then INSERT (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9, MariaDB 10.6, MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): error 1054: Unknown column 'WITHDRAWL' in 'NEW'
  • Compute the balance at query time with SUM() OVER (MySQL 8.0+, MariaDB 10.2+) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY bank_id) FROM bank ORDER BY bank_id

Related errors: 1054

The original article

First published 2012-07-19. The text below is the original; the table above shows how it behaves on current versions.

Reports sometimes need runtime values to be provided. Imagine a bank account requiring a balance field to report the current balance of the account. There are two ways in which this can be performed:

  • Using triggers to add the runtime value
  • Using session variables to store a runtime value

There are pro’s and con’s to both, primarily in the area of performance. If this information is accessed many times then we would need to recalculate each time the run time values then it may be better to store these values.

There are also conditions to be respected. We can only ever INSERT new values, we can never UPDATE or DELETE existing records. Updating or deleting would require a recalculation of the entire table.

For our example, let us imagine we want to keep a running total of a bank:

CREATE TABLE IF NOT EXISTS `bank` (
`BANK_ID` int(3) NOT NULL AUTO_INCREMENT,
`T_DATE` date NOT NULL,
`PARTICULAR` varchar(25) DEFAULT NULL,
`DEPOSIT` int(6) NOT NULL DEFAULT '0',
`WITHDRAWAL` int(6) NOT NULL DEFAULT '0',
`BALANCE` int(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`BANK_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Trigger

We will only ever add to this table. The balance is calculated based on the previous records balance with deposit added and withdrawal subtracted.

Our trigger will look something like this:

DELIMITER $$
CREATE TRIGGER bank_trig01 BEFORE INSERT ON bank
FOR EACH ROW
BEGIN
  DECLARE lBalance INT DEFAULT 0;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET lBalance = 0;
  SELECT BALANCE INTO lBalance FROM bank WHERE BANK_ID = (SELECT MAX(BANK_ID) FROM bank);
  SET NEW.BALANCE = lBalance + NEW.DEPOSIT - NEW.WITHDRAWL;
END
$$
DELIMITER ;

If you see the CONTINUE HANDLER FOR NOT FOUND if we are adding the very first record into the table then the balance will be 0.

Now let’s have a look at this in action:

mysql> insert into bank(t_date, particular, deposit, withdrawal) values (now(), 'test', 100, 0);
Query OK, 1 row affected (0.01 sec)

mysql> select * from bank;
+---------+------------+------------+---------+-----------+---------+
| BANK_ID | T_DATE     | PARTICULAR | DEPOSIT | WITHDRAWAL| BALANCE |
+---------+------------+------------+---------+-----------+---------+
|       1 | 2012-07-19 | test       |     100 |         0 |     100 |
+---------+------------+------------+---------+-----------+---------+
1 row in set (0.00 sec)

mysql> insert into bank(t_date, particular, deposit, withdrawal) values (now(), 'test2', 0, 50);
Query OK, 1 row affected (0.00 sec)

mysql> select * from bank;
+---------+------------+------------+---------+-----------+---------+
| BANK_ID | T_DATE     | PARTICULAR | DEPOSIT | WITHDRAWAL| BALANCE |
+---------+------------+------------+---------+-----------+---------+
|       1 | 2012-07-19 | test       |     100 |         0 |     100 |
|       2 | 2012-07-19 | test2      |       0 |        50 |      50 |
+---------+------------+------------+---------+-----------+---------+
2 rows in set (0.00 sec)

mysql> insert into bank(t_date, particular, deposit, withdrawal) values (now(), 'test2', 0, 49);
Query OK, 1 row affected (0.00 sec)

mysql> select * from bank;
+---------+------------+------------+---------+-----------+---------+
| BANK_ID | T_DATE     | PARTICULAR | DEPOSIT | WITHDRAWAL| BALANCE |
+---------+------------+------------+---------+-----------+---------+
|       1 | 2012-07-19 | test       |     100 |         0 |     100 |
|       2 | 2012-07-19 | test2      |       0 |        50 |      50 |
|       3 | 2012-07-19 | test2      |       0 |        49 |       1 |
+---------+------------+------------+---------+-----------+---------+
3 rows in set (0.00 sec)

mysql> insert into bank(t_date, particular, deposit, withdrawal) values (now(), 'test2', 34, 0);
Query OK, 1 row affected (0.00 sec)

mysql> select * from bank;
+---------+------------+------------+---------+-----------+---------+
| BANK_ID | T_DATE     | PARTICULAR | DEPOSIT | WITHDRAWAL| BALANCE |
+---------+------------+------------+---------+-----------+---------+
|       1 | 2012-07-19 | test       |     100 |         0 |     100 |
|       2 | 2012-07-19 | test2      |       0 |        50 |      50 |
|       3 | 2012-07-19 | test2      |       0 |        49 |       1 |
|       4 | 2012-07-19 | test2      |      34 |         0 |      35 |
+---------+------------+------------+---------+-----------+---------+
4 rows in set (0.00 sec)

As we can see this is keeps the running total of balance up to date for each record. Now let’s have a look at doing the same thing but calculating the running total rather than storing it.

Session variables

Session variables are very powerful in MySQL and allow us to perform more dynamic operations on the data being reported back from the database. Here are a few links to other white papers showing what session variables can be used for:

In our case what we want to do is calculate the runtime balance. In our example we will generate a column called runtime which will be displayed alongside the balance calculated with the trigger above to show that there is a match between both:

mysql> set @balance := 0;
Query OK, 0 rows affected (0.01 sec)

mysql> select bank_id, t_date, particular, deposit, withdrawal, balance, @balance := @balance + deposit - withdrawal as runtime from bank order by bank_id;
+---------+------------+------------+---------+-----------+---------+---------+
| bank_id | t_date     | particular | deposit | withdrawal | balance | runtime |
+---------+------------+------------+---------+-----------+---------+---------+
|       1 | 2012-07-19 | test       |     100 |         0 |     100 |     100 |
|       2 | 2012-07-19 | test2      |       0 |        50 |      50 |      50 |
|       3 | 2012-07-19 | test2      |       0 |        49 |       1 |       1 |
|       4 | 2012-07-19 | test2      |      34 |         0 |      35 |      35 |
+---------+------------+------------+---------+-----------+---------+---------+
4 rows in set (0.00 sec)

In this case here we are starting off with a balance of 0 (SET @balance = 0;). At each row we are calculating the balance based on the DEPOSIT and WITHDRAWAL amounts (adding deposits and removing withdrawals). This is performed using the code (@balance := @balance + deposit - withdrawal). We are referring to the @balance value that is keeping track of the runtime value.