1

我有两个表的以下设计,我无法使用 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. INNER JOIN

            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 时,它根本不返回任何记录。

4

1 回答 1

5

原因NATURAL JOIN在这里不起作用是因为你的两个表Description除了列之外还有一个PermissionSchemeKey列,所以 postgres 将尝试在两个列上进行内部连接。

IE

ON pr.PermissionSchemeKey = ps.PermissionSchemeKey
AND pr.Description = ps.Description
于 2017-10-02T04:51:21.377 回答