PostgreSQL Natural Join 不适用于两个相关表
PostgreSQL Natural Join is not working for two related tables
我有两个表的以下设计,我在使用 NATURAL JOIN 连接时遇到问题以测试参照完整性。它适用于 INNER JOIN,但不适用于 NATURAL JOIN :
Table 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'))
);
Table 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 查询无效(return 根本没有记录)
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 查询,return得到了想要的结果集。但是,当使用 NATURAL JOIN 时,return 根本没有记录。
NATURAL JOIN
在这里不起作用的原因是因为除了 PermissionSchemeKey
列之外,您的两个表还有一个 Description
列,因此 postgres 将尝试内部联接两列。
即
ON pr.PermissionSchemeKey = ps.PermissionSchemeKey
AND pr.Description = ps.Description
我有两个表的以下设计,我在使用 NATURAL JOIN 连接时遇到问题以测试参照完整性。它适用于 INNER JOIN,但不适用于 NATURAL JOIN :
Table 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'))
);
Table 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 查询无效(return 根本没有记录)
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 查询,return得到了想要的结果集。但是,当使用 NATURAL JOIN 时,return 根本没有记录。
NATURAL JOIN
在这里不起作用的原因是因为除了 PermissionSchemeKey
列之外,您的两个表还有一个 Description
列,因此 postgres 将尝试内部联接两列。
即
ON pr.PermissionSchemeKey = ps.PermissionSchemeKey
AND pr.Description = ps.Description