CREATE TRIGGER trigger_name ON table1 AFTER DELETE AS BEGIN DELETE table2 FROM table2 INNER JOIN deleted ON table2.foreign_key = deleted.primary_key END; 当中 foreign_key和primary_key指的是什么,能不能举例说明。
时间: 2024-01-08 18:04:54 浏览: 59
在这个触发器中,foreign_key和primary_key指的是两个表之间的关联字段。在这个例子中,假设table1和table2是两个相关联的表,其中table1的primary_key与table2的foreign_key相对应。当从table1中删除一行时,这个触发器将会删除table2表中与被删除行相关联的所有行。
例如,假设table1是一个存储学生信息的表,其中每个学生都有一个唯一的学生ID(primary_key)。table2是一个存储学生成绩的表,其中每个学生的成绩都有一个对应的学生ID(foreign_key)。在这种情况下,当从table1中删除一个学生时,这个触发器将会删除table2表中与被删除学生ID相对应的所有成绩记录。
相关问题
SELECT s.student_num, s.student_name, '计算机学院' AS college_name, c.level, c.class_name FROM student_info s JOIN classes c ON s.class_id = c.id JOIN volunteer_time_table_item t ON s.student_num = t.student_num INNER JOIN student_info s ON c.id = s.college_id INNER JOIN volunteer_time_table_item t ON s.student_num = t.student_num INNER JOIN volunteer_time_table vtt ON vtt.id = t.volunteer_time_table_id WHERE c.college_id = 1 AND s.level = 2021 AND vtt.volunteer_status = 5 AND vtt.deleted = 0 AND t.deleted = 0 AND s.deleted = 0 AND c.deleted = 0 ORDER BY t.volunteer_time_value DESC LIMIT 5;哪里有问题
这个 SQL 查询语句有以下问题:
1. 语法错误:在SELECT语句中,没有为 '计算机学院' 添加引号,应该在前后添加单引号。
2. 重复声明表别名:在JOIN语句中,重复声明了表别名 s 和 t,应该将它们改成不同的别名。
3. 错误的表连接方式:在JOIN语句中,没有为 student_info 表声明别名,应该将其改为 s 或其他合适的别名。此外,使用 INNER JOIN 连接 volunteer_time_table_item 表和 student_info 表时,应该使用 ON 关键字指定连接条件。
4. SELECT语句中重复声明列名:在SELECT语句中,声明了 student_num, student_name, level, class_name 列名,但是其中的 level 和 class_name 列名已经在 JOIN 语句中声明过了,应该删除重复的声明。
5. 语法错误:在 ORDER BY 语句中,应该为 t.volunteer_time_value 添加列名,否则会报 Unknown column 'volunteer_time_value' in 'order clause' 的错误。
修改后的 SQL 查询语句如下:
SELECT s.student_num, s.student_name, '计算机学院' AS college_name, c.level, c.class_name
FROM student_info s
JOIN classes c ON s.class_id = c.id
JOIN volunteer_time_table_item t ON s.student_num = t.student_num
INNER JOIN student_info si ON c.id = si.college_id
INNER JOIN volunteer_time_table_item ti ON si.student_num = ti.student_num
INNER JOIN volunteer_time_table vtt ON vtt.id = ti.volunteer_time_table_id
WHERE c.college_id = 1
AND s.level = 2021
AND vtt.volunteer_status = 5
AND vtt.deleted = 0
AND ti.deleted = 0
AND si.deleted = 0
AND c.deleted = 0
ORDER BY ti.volunteer_time_value DESC LIMIT 5;
SELECT DISTINCT s.student_num, s.student_name, '计算机学院' AS college_name, c.class_name, FROM student_info s JOIN classes c ON s.college_id = c.college_id JOIN volunteer_time_table_item t ON s.student_num = t.student_num JOIN volunteer_time_table vtt ON vtt.id = t.volunteer_time_table_id WHERE c.college_id = 1 AND s.level = 2021 AND t.deleted = 0 AND s.deleted = 0 AND vtt.deleted = 0 AND c.deleted = 0 ORDER BY t.volunteer_time_value DESC LIMIT 5;哪里有问题
在SELECT语句中,'计算机学院' AS college_name后面有一个逗号,应该将逗号删除,修改后的语句如下:
SELECT DISTINCT s.student_num, s.student_name, '计算机学院' AS college_name, c.class_name FROM student_info s JOIN classes c ON s.college_id = c.college_id JOIN volunteer_time_table_item t ON s.student_num = t.student_num JOIN volunteer_time_table vtt ON vtt.id = t.volunteer_time_table_id WHERE c.college_id = 1 AND s.level = 2021 AND t.deleted = 0 AND s.deleted = 0 AND vtt.deleted = 0 AND c.deleted = 0 ORDER BY t.volunteer_time_value DESC LIMIT 5;
阅读全文