“没有主键” PostgreSQL 中 INNER JOIN 查询出错

发布于 2024-12-15 02:09:08 字数 936 浏览 0 评论 0原文

问题是,两个表都有主键,而且到目前为止我对任何其他标准 SELECTS/INSERTS 都没有遇到任何问题。我是 JOINS 新手,但我不明白为什么这不起作用。

我收到此错误消息:

org.postgresql.util.PSQLException: No primary key found for table devicetable.

但是,两个表的主键都是我选择的列:deviceid / username。或许这也有关系吧?

我通过 JDBC

PreparedStatement query = curSQL.getConn().prepareStatement("SELECT devicetable.pushid FROM devicetable, usertable WHERE usertable.username=? AND usertable.deviceid = devicetable.deviceid", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
query.setString(1, username);
ResultSet rs = query.executeQuery();

if (rs.next()){
    rs.updateString("pushid", pushID);
    rs.updateRow();
}

rs.close();
query.close();

使用 SQL 访问它:

SELECT devicetable.pushid FROM devicetable, usertable
WHERE  usertable.username=?
AND    usertable.deviceID = devicetable.deviceID

The thing is, both tables do have primary keys, and I haven't had any problems so far with any other standard SELECTS/INSERTS at all. I'm new to JOINS, but I can't see why this isn't working.

I'm get this error message:

org.postgresql.util.PSQLException: No primary key found for table devicetable.

However, the primary keys for both tables are the columns I'm selecting for: deviceid / username. Perhaps this has something to do with it?

I'm accessing it via JDBC

PreparedStatement query = curSQL.getConn().prepareStatement("SELECT devicetable.pushid FROM devicetable, usertable WHERE usertable.username=? AND usertable.deviceid = devicetable.deviceid", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
query.setString(1, username);
ResultSet rs = query.executeQuery();

if (rs.next()){
    rs.updateString("pushid", pushID);
    rs.updateRow();
}

rs.close();
query.close();

With the SQL:

SELECT devicetable.pushid FROM devicetable, usertable
WHERE  usertable.username=?
AND    usertable.deviceID = devicetable.deviceID

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-12-22 02:09:08

尝试显式连接:

SELECT devicetable.pushid
FROM devicetable
JOIN usertable ON usertable.deviceID = devicetable.deviceID
WHERE usertable.username = ?

Try an explicit join:

SELECT devicetable.pushid
FROM devicetable
JOIN usertable ON usertable.deviceID = devicetable.deviceID
WHERE usertable.username = ?
撧情箌佬 2024-12-22 02:09:08

SELECT 中的 JOIN 子句与主键无关。您不需要为任何 SELECT 查询定义主键。

副作用可能是您得到重复的行,但这完全是另一回事。

实际上,您的查询中没有显式 JOIN。您可以使用显式 JOIN 重写查询,如下所示:

SELECT devicetable.pushid
FROM   devicetable
JOIN   usertable USING (deviceID)
WHERE  usertable.username=?

阅读有关 手册中的 JOIN 语法

The JOIN clause in a SELECT has nothing to do whatsoever with primary keys. You don't need to have a primary key defined for any SELECT query.

A side effect may be that you get duplicate rows, but that is a different matter entirely.

Actually, there is no explicit JOIN in your query. You could rewrite your query with an explicit JOIN like this equivalent:

SELECT devicetable.pushid
FROM   devicetable
JOIN   usertable USING (deviceID)
WHERE  usertable.username=?

Read more about the JOIN syntax in the manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文