0

Есть 2 таблицы

db.execSQL("Create table STUDGROUPS (" + BaseColumns._ID + " integer primary key autoincrement, faculty text, course int, name text, head text," +
                "constraint name_u unique (name) on conflict abort);");
db.execSQL("Create table Students (" + BaseColumns._ID + " integer primary key autoincrement, name text, IDGROUP int," +
                "constraint idgroup_fk foreign key(IDGROUP) references STUDGROUPS(_id) on delete cascade on update cascade);");

В группе не должно быть больше 6 студентов и меньше 3, т.е. нужно запреть вставку, если есть с одинаковым id группы 6 студентов и удаление, есть оно приведет к нахождению в группе меньше 3 человек.

Важным условием является сохранение каскада. Поэтому, при удалении студента необходимо проверить, удалена ли его группа. Если да, то ограничения на меньше 3 снимаются и студент удаляется, если нет, то удаление запрещаем.

Помогите написать такие триггеры.Help!

1 ответ 1

1
create trigger prevent_big_groups before insert on students
begin
    select case when (
        select count(*) from students where idgroup = NEW.idgroup
    ) = 5 then raise(ABORT, 'Too much students in the group') end;
end;

create trigger prevent_small_groups before delete on students
begin
    select case when (
        select count(*) from students where idgroup = OLD.idgroup
    ) = 3 then raise(ABORT, 'Not enough students in the group') end
    from studgroups where id = OLD.idgroup;
end;

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.