简体   繁体   中英

SQL Trigger Syntax

Each course on my DB has a correspondent faculty_id.

Each faculty has affinities with other faculties (and as a consequense so do the courses with those faculty_ids). Faculty affinities and courses affinities are sotred y sepparated tables.

I'm trying to build a trigger which deletes records from the course_affinity table when a record on faculty_affinity is deleted.

This is what I have. It does not work.

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;

Help someone? What am I doing wrong?

Edit: I get error

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

Edit2: my migrations

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();
            });

Your code looks like it might have been written for MS SQL Server and not MySQL (the use of the deleted virtual table hints at that); the corresponding MySQL syntax should be as follows:

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;

I haven't tested it though so proceed with some care...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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