作为数据库设计人员,我需要在表上创建一个触发器,以便在执行以下操作时,可以在列PIB_DTL_CD
处将新值创建为PIB0000000001
:PIB_DTL_ID
列的值是1
(自动递增,私钥)
我需要创建触发器,但我不在错误所在。您能告诉我什么是推荐做法吗?
我想创建一个mysql触发器到
这是我的触发器的SQL代码:
CREATE TRIGGER `b_pib_ref_gen`
BEFORE INSERT ON `prd_test6`.`b_pib_detail` FOR EACH ROW
BEGIN
IF new.PIB_DTL_CD IS NULL
set @auto_id := (SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='b_pib_detail' AND TABLE_SCHEMA=DATABASE() );
set NEW.PIB_DTL_CD = CONCAT('PIB', LPAD( @auto_id + 1 , 10, '0')) ;
ENF IF;
END;
更新:
Maria DB版本是10.4.8-MariaDB
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.1.14-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [sandbox]> drop table if exists t;
Query OK, 0 rows affected (0.10 sec)
MariaDB [sandbox]> create table t
-> (id int auto_increment primary key, val int,
-> pb_id varchar(20) as (concat('pb',LPAD(id, 10, '0')))
-> );
Query OK, 0 rows affected (0.22 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> insert into t(val) values
-> (1),(2);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> select * from t;
+----+------+--------------+
| id | val | pb_id |
+----+------+--------------+
| 1 | 1 | pb0000000001 |
| 2 | 2 | pb0000000002 |
+----+------+--------------+
2 rows in set (0.00 sec)