触发功能,以检查学生是否有classess

时间:2017-01-06 01:59:57

标签: sql postgresql function triggers plpgsql

我正在Postgres建立一个数据库,为学生注册课程。

如果想要报名参加课程的学生在他之前注册的那个时间没有任何其他课程,我会坚持创建功能检查。

共有6个表格:

CREATE TABLE "semester" (
    "semester_id" serial primary key,
    "semester_describtion" varchar);

CREATE TABLE "course" (
    "c_id" serial not null,
    "c_name" varchar NOT NULL,
    "c_describtion" varchar,
    "semester_id" int,
    "startDate" DATE,
    "endDate" DATE,
    PRIMARY KEY (c_id, semester_id),
    FOREIGN KEY (semester_id) references semester(semester_id));

CREATE TABLE "staff" (
    "staff_id" serial primary key,
    "staff_lastname" varchar(40) NOT NULL,
    "staff_firstname" varchar(40) NOT NULL,
    "staff_email" varchar(40));

CREATE TABLE "student" (
    "s_id" serial PRIMARY KEY,
    "s_lastname" varchar(40) NOT NULL,
    "s_firstname" varchar(40) NOT NULL,
    "semester_id" int default '1',
    foreign key (semester_id) references semester(semester_id));

CREATE TABLE "location" (
    "location_id" serial,
    "room_nr" int,
    "building_id" varchar(40) DEFAULT 'D5',
    "max_students" int default '10',
    primary key (location_id, max_students));

CREATE TABLE "course_section" (
    "c_section_id" serial,
    "c_id" int NOT NULL,
    "semester_id" int NOT NULL,
    "staff_id" int,
    "location_id" int not null,
    "c_section_date" DATE,
    "c_section_time" TIME default '08:00',
    "c_section_during" TIME default '01:30',
    "c_section_type" TEXT not null,
    "max_students" int default '10',
    "students" int default '0',
    check(c_section_type = 'L' OR c_section_type = 'W'
       OR c_section_type = 'C' OR c_section_type = 'I'),
    primary key (c_id, staff_id, location_id,c_section_date,c_section_time),
    foreign key (c_id, semester_id) references course(c_id, semester_id),
    foreign key (staff_id) references staff(staff_id),
    foreign key (location_id, max_students) references location(location_id, max_students)
    );

CREATE TABLE "enrollment" (
    "s_id" int not null,
    "mark" int,
    "c_id" int not null,
    "semester_id" int not null,
    primary key (s_id,c_id,semester_id),
    foreign key (s_id) references student(s_id),
    foreign key (c_id, semester_id) references course(c_id, semester_id));

触发功能和触发器:

create function check_date() returns trigger as '
declare
date1 date;
time1 time;
begin

Select c_section_date, c_section_time into date1, time1 from enrollment e 
inner join course c using (c_id)
inner join course_section cs using (c_id);

IF EXISTS  (select e.s_id , cs.c_section_date, cs.c_section_time from enrollment e 
inner join course c using (c_id)
inner join course_section cs using (c_id)
where new.s_id=e.s_id AND date1=c_section_date and time1 = c_section_time) THEN
RAISE NOTICE  ''You have classess on that time'';
Return null;
ELSE
return new;
END IF;
END;
' language 'plpgsql';

create trigger t_check_date before insert on enrollment for each row execute procedure check_date();

问题是它只能为一个班级注册学生。当我想要插入enrollment第二个课程时,尽管时间不同,但仍显示RAISE NOTICE

1 个答案:

答案 0 :(得分:0)

你的功能会像这样:

CREATE OR REPLACE FUNCTION check_date() 
  RETURNS trigger AS
$func$
BEGIN
   IF EXISTS (
      SELECT 1
      FROM   course_section cs1
      WHERE  cs1.c_id        = NEW.c_id
      AND    cs1.semester_id = NEW.semester_id
      AND EXISTS (
         SELECT 1
         FROM   enrollment e 
         JOIN   course_section cs USING (c_id, semester_id)
         WHERE  e.semester_id      = NEW.semester_id
         AND    e.s_id             = NEW.s_id
         AND    cs.c_section_date  = cs1.c_section_date
         AND    cs.c_section_time  = cs1.c_section_time))  THEN

      RAISE NOTICE 'You have classes at that time';
      RETURN null;
   END IF;

   RETURN NEW;
END
$func$  LANGUAGE plpgsql;

表格course只是我们可以省略的垫脚石。 enrollmentcourse_section可以直接加入(c_id, semester_id)

但你真的应该检查的不只是 date 一个时间。您应该检查重叠课程的时间范围。