为注册限制实现postgresql触发器

时间:2015-03-05 05:44:30

标签: sql postgresql triggers

我试图在注册表单中创建一个触发器,这样如果插入的部分id超过限制,那么它将引发异常。节表包含注册表单表中节ID的限制。当我尝试在超出该部分限制的注册中输入新部分时,它不会弹出任何例外。有什么建议吗?

到目前为止,我有:

CREATE OR REPLACE FUNCTION enroll_lim()
RETURNS TRIGGER AS
 $BODY$
declare counts INTEGER;
declare counts1 INTEGER;
    BEGIN
     select count(ef.section_id) into counts FROM enroll_form ef
    WHERE ef.section_id = new.section_id;

        SELECT slimit into counts1 
        FROM section 
        WHERE new.section_id = section.section_id;
        IF counts > counts1 THEN
            RAISE EXCEPTION 'LIMIT REACHED!';

        END IF;
        RETURN NEW;
        END;
        $BODY$
  LANGUAGE plpgsql VOLATILE

CREATE TRIGGER enroll
  BEFORE INSERT
  ON enroll_form
  FOR EACH ROW
  EXECUTE PROCEDURE enroll_lim();

0 个答案:

没有答案