繁体   English   中英

发现 MySQL 触发器创建错误:1064

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM