【问题标题】:SQL Trigger SyntaxSQL 触发器语法
【发布时间】:2016-05-20 04:46:37
【问题描述】:

我数据库中的每门课程都有一个对应的faculty_id。

每个学院都与其他学院有密切关系(因此,具有这些学院 ID 的课程也是如此)。教师关联和课程关联是分开的表格。

我正在尝试构建一个触发器,当faculty_affinity 上的记录被删除时,该触发器会从 course_affinity 表中删除记录。

这就是我所拥有的。它不起作用。

CREATE TRIGGER deleteCourses
AFTER DELETE
ON faculty_affinities
AS
BEGIN
    DELETE FROM course_affinities
    JOIN courses AS course1
    JOIN courses AS course2
    ON course_affinities.course1_id = course1.id
    AND course_affinities.course2_id = course2.id
    WHERE (course1.faculty_id = DELETED.faculty1_id OR course1.faculty_id = DELETED.faculty2_id)
    AND (course2.faculty_id = DELETED.faculty1_id OR course2.faculty_id = DELETED.faculty2_id);
END;

帮助别人?我做错了什么?

编辑:我收到错误

SQLSTATE[4200]: Syntax error... neas 'AS at line 4

Edit2:我的迁移

Schema::create('courses', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name', 150);
                $table->integer('faculty_id')->unsigned();
                $table->foreign('faculty_id')->references('id')->on('faculties');
                $table->boolean('active')->default(1);
                //$table->softDeletes();
            });

    Schema::create('faculties', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name', 150)->unique();
                    $table->boolean('active')->default(1);
                    //$table->softDeletes();
            });


Schema::create('course_affinities', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('course1_id')->unsigned();
                    $table->foreign('course1_id')->references('id')->on('courses');
                $table->integer('course2_id')->unsigned();
                    $table->foreign('course2_id')->references('id')->on('courses');
                $table->boolean('active')->default(1);
                //$table->softDeletes();
            });

Schema::create('faculty_affinities', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('faculty1_id')->unsigned();
                    $table->foreign('faculty1_id')->references('id')->on('faculties');
                $table->integer('faculty2_id')->unsigned();
                    $table->foreign('faculty2_id')->references('id')->on('faculties');
                $table->boolean('active')->default(1);
                //$table->softDeletes();
            });

【问题讨论】:

  • 语法错误。我编辑了带有错误消息的原始帖子。
  • 我认为您不能从JOIN 中删除,而且我知道DELETED 在MySQL 中不是有效的关键字。如果您发布表格定义,也许我们可以指导您正确的方向。
  • 如果您已有有效答案,则不应编辑您的问题,这样您的编辑会使这些答案无效。
  • 你是对的。对不起各位!
  • 这篇文章为我解决了这个问题。 stackoverflow.com/questions/18379137/…

标签: mysql sql triggers


【解决方案1】:

您的代码看起来可能是为 MS SQL Server 而不是 MySQL 编写的(deleted 虚拟表的使用暗示了这一点);对应的MySQL语法应该如下:

CREATE TRIGGER deleteCourses
AFTER DELETE ON faculty_affinities
FOR EACH ROW
BEGIN
    DELETE ca
    FROM course_affinities ca
    JOIN courses AS course1 ON ca.course1_id = course1.id 
    JOIN courses AS course2 ON ca.course2_id = course2.id    
    WHERE (course1.faculty_id = OLD.faculty1_id OR course1.faculty_id = OLD.faculty2_id)
      AND (course2.faculty_id = OLD.faculty1_id OR course2.faculty_id = OLD.faculty2_id);
END;

虽然我还没有测试过,所以请小心...

【讨论】:

  • 谢谢!这看起来不错。不过,我仍然收到错误消息:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 10 行的 '' 附近使用正确的语法
  • @JulianM。您可能需要将语句分隔符更改为分号以外的内容,或者如果您删除第 10 行末尾的分号(在 where 子句之后),它应该可以工作。有关示例,请参阅此问题:stackoverflow.com/questions/8154158/…
  • 奇怪的是,如果我直接在 phpmyadmin 或 MySQL 控制台中运行此查询,我不会收到任何错误。只有在 artisan 上运行迁移时才会出现错误。但是,我使用当前触发器编辑了我的帖子。
猜你喜欢
  • 2020-07-21
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多