89774 2022-10-16 15:01 采纳率: 100%
浏览 43
已结题

如何在课程样例数据数据表tblselectcourses中,创建写入数据触发器tri_insc,模拟外键作用,即已经注册tblstudents的学生才能选tblcourses中的课程?

在课程样例数据数据表tblselectcourses中,创建写入数据触发器tri_insc,模拟外键作用,即已经注册tblstudents的学生才能选tblcourses中的课程。
create table tblStudents
(
stuID char(8) not null primary key COMMENT '学号',
stuName char(4) not null COMMENT '姓名',
stuGender enum('男','女') not null COMMENT '性别',
departID tinyint COMMENT '系部编号'
)
create table tblSelectCourses
(
StuID char(8) not null comment '学号',
courseID char(3) not null comment '课程编号',
credit decimal(4,1) comment '成绩',
primary key(StuID,courseID)
)

  • 写回答

1条回答 默认 最新

  • 夜郎king 2022博客之星IT其它领域TOP 12 2022-10-16 15:43
    关注

    您好,mysql中创建触发器的语法如下:

    DELIMITER $
    create trigger tri_insert_blocks_infos 
    before insert on tblSelectCourses for each row
    begin
    DECLARE c INT;
    set c=(SELECT COUNT(1) FROM tblStudents WHERE stuID =new.StuID);
    if c<=1 then
    执行插入
    elseif c>1 then
    不用管
    end if ;
    end $
    DELIMITER ;
    

    试试这种方式行不行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月25日
  • 已采纳回答 10月17日
  • 创建了问题 10月16日