PostgreSQL - 为视图添加“约束”

时间:2017-12-01 16:50:49

标签: sql postgresql

我试图在can_graduate中添加一种方法来检查这个人是否已经阅读了足够的sum_credits(180)来毕业,我想知道是否有人可以帮助我。我检查Count(UnreadMandatory)= 0的地方我认为我想添加像sum_credits> = 180这样的东西,但我无法让它工作。

这是我在这里的第一篇文章,所以请善待。

这是我遇到问题的观点: https://pastebin.com/ezUtRMGB

    CREATE VIEW PathToGraduation AS
    SELECT student.national_ID,

    (SELECT sum(passedcourses.credit)
    FROM passedcourses, has_classification
    WHERE passedcourses.national_ID = student.national_ID AND has_classification.code = passedcourses.code) AS sum_credits,

    (SELECT count(UnreadMandatory)
    FROM UnreadMandatory
    WHERE UnreadMandatory.national_ID = student.national_ID) AS remaining_courses,

    (SELECT sum(passedcourses.credit)
    FROM passedcourses, has_classification
    WHERE passedcourses.national_ID = student.national_ID AND has_classification.code = passedcourses.code AND has_classification.type = 'Math_class') AS math_credits,

    (SELECT count(passedcourses.credit)
    FROM passedcourses, has_classification
    WHERE passedcourses.national_ID = student.national_ID AND has_classification.code = passedcourses.code AND has_classification.type = 'Seminar_class') AS seminar_courses_taken,

    (SELECT count(passedcourses.code)
    FROM passedcourses, has_classification
    WHERE passedcourses.national_ID = student.national_ID AND has_classification.code = passedcourses.code AND has_classification.type = 'Research_class') AS research_credits,

    (SELECT 'YES' AS can_graduate
    FROM UnreadMandatory
    WHERE student.national_ID = UnreadMandatory.national_ID
    HAVING COUNT(UnreadMandatory) = 0
    UNION
    (SELECT 'NO' AS can_graduate
    FROM UnreadMandatory
    WHERE student.national_ID = UnreadMandatory.national_ID
    HAVING COUNT(UnreadMandatory) != 0))

1 个答案:

答案 0 :(得分:0)

您可能正在寻找WITH CHECK OPTION

根据文件。

  

此选项控制自动可更新视图的行为。   指定此选项时,视图上的INSERT和UPDATE命令   将被检查以确保新行满足视图定义   条件(即,检查新行以确保它们是   通过视图可见)。如果不是,则更新将是   被拒绝。如果未指定CHECK OPTION,则INSERT和UPDATE   允许视图上的命令创建不可见的行   通过观点。

CREATE VIEW