AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
主页 / user-270026

Cromega08's questions

Martin Hope
Cromega08
Asked: 2023-03-04 08:21:57 +0800 CST

MySQL:如何让触发器在另一个表中插入行?

  • 7

请阅读所有内容,包括最后的注释和编辑

问题

我有一个带有 3 个不同表的 MySQL 数据库:

  1. 为了节省用户
mysql> describe users;
+-------------+-----------------------------------+------+-----+---------+----------------+
| Field       | Type                              | Null | Key | Default | Extra          |
+-------------+-----------------------------------+------+-----+---------+----------------+
| id          | int                               | NO   | PRI | NULL    | auto_increment |
| firstname   | varchar(255)                      | NO   |     | NULL    |                |
| lastname    | varchar(255)                      | YES  |     | NULL    |                |
| username    | varchar(255)                      | NO   | UNI | NULL    |                |
| pswd        | varchar(255)                      | NO   |     | NULL    |                |
| permissions | enum('admin', 'student')          | NO   |     | NULL    |                |
| active      | tinyint(1)                        | NO   |     | 1       |                |
+-------------+-----------------------------------+------+-----+---------+----------------+
7 rows in set (0,01 sec)
  1. 为了救学生

mysql> describe students;
+--------------+--------------+------+-----+--------------+-------------------+
| Field        | Type         | Null | Key | Default      | Extra             |
+--------------+--------------+------+-----+--------------+-------------------+
| id           | int          | NO   | PRI | NULL         |                   |
| firstname    | varchar(255) | NO   |     | not assigned |                   |
| lastname     | varchar(255) | YES  |     | NULL         |                   |
| id_number    | int          | NO   | UNI | -1           |                   |
| registration | date         | NO   |     | curdate()    | DEFAULT_GENERATED |
+--------------+--------------+------+-----+--------------+-------------------+
5 rows in set (0,01 sec)
  1. 拯救管理员
mysql> describe admins;
+--------------+--------------+------+-----+--------------+-------------------+
| Field        | Type         | Null | Key | Default      | Extra             |
+--------------+--------------+------+-----+--------------+-------------------+
| id           | int          | NO   | PRI | NULL         |                   |
| firstname    | varchar(255) | NO   |     | not assigned |                   |
| lastname     | varchar(255) | YES  |     | NULL         |                   |
| registration | date         | NO   |     | curdate()    | DEFAULT_GENERATED |
+--------------+--------------+------+-----+--------------+-------------------+
4 rows in set (0,00 sec)

我想为用户创建一个插入后触发器,以根据字段users.permissions在其他触发器中插入一行。

  • 扳机:
delimiter $$
create trigger users_ai
after insert
on users for each row
begin
    set @id_to_update := new.id;
    set @table_to_update := (select permissions
                            from users
                            where [email protected]_to_update
                        );  
    set @query := concat(
        'update ',
        quote(@table_to_update),
        's set firstname=(select firstname from users where id=',
        quote(@id_to_update),
        '),lastname=(select lastname from users where id=',
        quote(@id_to_update),
        ') where id=',
        quote(@id_to_update)
    );
    exec @query;
end;
$$
delimiter ;

但是,总是出现以下错误:

SQL Error [1064] [42000]: 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 '' at line 5

Error position: line: 4

DBeaver(我用来处理 DB 的编辑器)抛出了以下细节:

org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [1064] [42000]: 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 '' at line 5
    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:133)
    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:582)
    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$1(SQLQueryJob.java:491)
    at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:173)
    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:498)
    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:920)
    at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:3805)
    at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:123)
    at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:173)
    at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:121)
    at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$ResultSetDataPumpJob.run(ResultSetViewer.java:5068)
    at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:105)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.sql.SQLSyntaxErrorException: 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 '' at line 5
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:763)
    at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:329)
    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.lambda$0(JDBCStatementImpl.java:131)
    at org.jkiss.dbeaver.utils.SecurityManagerUtils.wrapDriverActions(SecurityManagerUtils.java:96)
    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
    ... 12 more

我试过的

  1. 检查语法、空格或奇怪字符等基本内容。
  2. 在触发器中设置 @id_to_update变量的不同形式:
    • 新的.id
    • last_insert_id()
    • StackOverflow 上的这段代码:
      set @id_to_update = (select id from users order by id desc limit 1);
      if @id_to_update is null or @id_to_update = '' then set @id_to_update = 1;
      

笔记

  1. 我对数据库的了解是基础知识。
  2. 这只是我在空闲时间制作的“玩具项目”的一部分,只是为了学习新事物。
  3. MySQL版本:
    ~ mysql --version
    mysql  Ver 8.0.32-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))
    

编辑

根据@Ergest Basha 的回答和其他建议,将所有内容替换为条件,对表格进行一些更改并尝试使所有内容更简单,现在看起来像:

  1. 用户:
mysql> describe users;
+-------------+-----------------------------------+------+-----+---------+----------------+
| Field       | Type                              | Null | Key | Default | Extra          |
+-------------+-----------------------------------+------+-----+---------+----------------+
| id          | int                               | NO   | PRI | NULL    | auto_increment |
| firstname   | varchar(255)                      | NO   |     | NULL    |                |
| lastname    | varchar(255)                      | YES  |     | NULL    |                |
| username    | varchar(255)                      | NO   | UNI | NULL    |                |
| pswd        | varchar(255)                      | NO   |     | NULL    |                |
| permissions | enum('admin','student')           | NO   |     | NULL    |                |
| active      | tinyint(1)                        | NO   |     | 1       |                |
+-------------+-----------------------------------+------+-----+---------+----------------+
7 rows in set (0,01 sec)
  1. 学生:
mysql> describe students;
+--------------+--------------+------+-----+-----------+-------------------+
| Field        | Type         | Null | Key | Default   | Extra             |
+--------------+--------------+------+-----+-----------+-------------------+
| id           | int          | NO   | PRI | NULL      |                   |
| firstname    | varchar(255) | NO   |     | NULL      |                   |
| lastname     | varchar(255) | YES  |     | NULL      |                   |
| id_number    | int          | NO   | UNI | -1        |                   |
| registration | date         | NO   |     | curdate() | DEFAULT_GENERATED |
+--------------+--------------+------+-----+-----------+-------------------+
5 rows in set (0,00 sec)
  1. 管理员:
mysql> describe admins;
+--------------+--------------+------+-----+-----------+-------------------+
| Field        | Type         | Null | Key | Default   | Extra             |
+--------------+--------------+------+-----+-----------+-------------------+
| id           | int          | NO   | PRI | NULL      |                   |
| firstname    | varchar(255) | NO   |     | NULL      |                   |
| lastname     | varchar(255) | YES  |     | NULL      |                   |
| registration | date         | NO   |     | curdate() | DEFAULT_GENERATED |
+--------------+--------------+------+-----+-----------+-------------------+
4 rows in set (0,00 sec)
  1. 用户表触发器:
delimiter $$
create trigger users_ai
after insert
on users for each row
begin
    case new.permissions
        when 'student' then
            insert into students (id, fistname, lastname)
            values (new.id, new.firstname, new.lastname);
        else
            insert into admins (id, firstname, lastname)
            values (new.id, new.firstname, new.lastname);
    end case;
end
$$
delimiter ;

但仍然无法正常工作,并在begin语句后继续出现相同的错误:

SQL Error [1064] [42000]: 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 '' at line 8

此时不知道是否只是我机器上的错误,衷心感谢所有建议。

mysql
  • 3 个回答
  • 37 Views

Sidebar

Stats

  • 问题 199037
  • 回答 263511
  • 最佳答案 131755
  • 用户 66345
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve