[英]Error on MySQL Trigger Creation found : 1064
作為數據庫設計人員,我需要在表中創建一個觸發器,以便當PIB0000000001
列的PIB_DTL_ID
1
(自動增量,私鑰)時,我可以在PIB_DTL_CD
列創建新值 PIB0000000001
我需要創建觸發器,但我不是錯誤所在。 你能告訴我推薦的做法是什么嗎?
我想創建一個 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.