PostgreSQL Natural Join不适用于两个相关表

时间:2017-10-02 04:17:33

标签: sql postgresql natural-join

我有两个表的以下设计,我无法使用NATURAL JOIN来测试参照完整性。它适用于INNER JOIN,但不适用于NATURAL JOIN:

表1.项目

        DROP TABLE IF EXISTS Project CASCADE;
        CREATE TABLE Project (
                ProjectKey CHAR(10)
                ,ProjectName VARCHAR(255)
                ,ProjectType CHAR(20)
                ,URL VARCHAR
                ,ProjectLeadUsername VARCHAR(255) NOT NULL
                ,ProjectLeadGroupName VARCHAR(255) NOT NULL
                ,DefaultAssignee CHAR(50)
                ,ProjectCategory VARCHAR(255) NULL
                ,Description TEXT
                ,PermissionSchemeKey CHAR(10)
                ,IssueTypeSchemeKey CHAR(20)
                ,WorkflowSchemeKey CHAR(20)

            ,CONSTRAINT Project_PK PRIMARY KEY (ProjectKey)

            ,CONSTRAINT Project_FK_PermissionScheme FOREIGN KEY (PermissionSchemeKey) REFERENCES PermissionScheme
                ON DELETE RESTRICT
                ON UPDATE CASCADE

            ,CONSTRAINT Project_FK_IssueTypeScheme FOREIGN KEY (IssueTypeSchemeKey) REFERENCES IssueTypeScheme
                ON DELETE RESTRICT
                ON UPDATE CASCADE

            ,CONSTRAINT Project_FK_WorkflowScheme FOREIGN KEY (WorkflowSchemeKey) REFERENCES WorkflowScheme
                ON DELETE RESTRICT
                ON UPDATE CASCADE

            ,CONSTRAINT Project_FK_User FOREIGN KEY (ProjectLeadUsername, ProjectLeadGroupName) REFERENCES JIRA_User
                ON DELETE RESTRICT
                ON UPDATE CASCADE

            ,CONSTRAINT Project_DI_DefaultAssignee CHECK (DefaultAssignee IN ('Project Lead', 'Unassigned'))

            ,CONSTRAINT Project_DI_ProjectType CHECK (ProjectType IN ('Software', 'Business'))

        );

表2.权限方案

        DROP TABLE IF EXISTS PermissionScheme CASCADE;
        CREATE TABLE PermissionScheme (
                PermissionSchemeKey CHAR(10)
                ,PermissionSchemeName VARCHAR(255)
                ,Description TEXT

            ,CONSTRAINT PermissionScheme_PK PRIMARY KEY(PermissionSchemeKey)
        );

查询1.不起作用的NATURAL JOIN查询(根本不返回任何记录)

        SELECT pr.ProjectKey
                ,pr.ProjectName
                ,pr.ProjectType
                ,ps.PermissionSchemeKey
                ,ps.PermissionSchemeName
                ,ps.Description  
        FROM Project pr NATURAL JOIN PermissionScheme ps

查询2.内部联接

            SELECT pr.ProjectKey
                    ,pr.ProjectName
                    ,pr.ProjectType
                    ,ps.PermissionSchemeKey
                    ,ps.PermissionSchemeName
                    ,ps.Description  
            FROM Project pr 
                INNER JOIN PermissionScheme ps
                    ON pr.PermissionSchemeKey = ps.PermissionSchemeKey

使用INNER JOIN查询,它返回所需的结果集。但是,使用NATURAL JOIN时,它根本不会返回任何记录。

1 个答案:

答案 0 :(得分:4)

NATURAL JOIN无法在此处运行的原因是因为除了Description列之外,您的表格还有PermissionSchemeKey列,因此postgres将尝试在两列上进行内部联接

ON pr.PermissionSchemeKey = ps.PermissionSchemeKey
AND pr.Description = ps.Description