postgresql 的 mysql_insert_id 替代品

发布于 2024-07-05 04:36:26 字数 117 浏览 7 评论 0原文

PostgreSQL 的 mysql_insert_id() php 函数有替代方案吗? 大多数框架通过查找 ID 中使用的序列的当前值来部分解决问题。 但是,有时主键不是串行列......

is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks are solving the problem partially by finding the current value of the sequence used in the ID. However, there are times that the primary key is not a serial column....

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

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

发布评论

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

评论(4

弱骨蛰伏 2024-07-12 04:36:26

来自 php.net:

$res=pg_query("SELECT nextval('foo_key_seq') as key");
$row=pg_fetch_array($res, 0);
$key=$row['key'];
// now we have the serial value in $key, let's do the insert
pg_query("INSERT INTO foo (key, foo) VALUES ($key, 'blah blah')");

这应该始终提供唯一的密钥,因为从数据库检索到的密钥将永远不会再次检索。

From php.net:

$res=pg_query("SELECT nextval('foo_key_seq') as key");
$row=pg_fetch_array($res, 0);
$key=$row['key'];
// now we have the serial value in $key, let's do the insert
pg_query("INSERT INTO foo (key, foo) VALUES ($key, 'blah blah')");

This should always provide unique key, because key retrieved from database will be never retrieved again.

—━☆沉默づ 2024-07-12 04:36:26

查看 INSERT 语句的 RETURNING 可选子句。 (链接到官方 PostgreSQL 文档)

但基本上,您可以这样做:

INSERT INTO table (col1, col2) VALUES (1, 2) RETURNING pkey_col

并且 INSERT 语句本身返回受影响行的 id(或您指定的任何表达式)。

Check out the RETURNING optional clause for an INSERT statement. (Link to official PostgreSQL documentation)

But basically, you do:

INSERT INTO table (col1, col2) VALUES (1, 2) RETURNING pkey_col

and the INSERT statement itself returns the id (or whatever expression you specify) of the affected row.

寂寞笑我太脆弱 2024-07-12 04:36:26

您还可以使用:

$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_result($result, 0, 'foo_id');

您必须在 pg_fetch_result 中指定您要查找的行号和字段名称,这是获取您需要的数据的更精确的方法,但我不知道这是否有效查询的性能会受到一些影响。 请记住,此方法适用于 PostgreSQL 8.2 及更高版本。

You also can use:

$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_result($result, 0, 'foo_id');

You have to specify in pg_fetch_result the number of the row and the name of the field that you are looking for, this is a more precise way to get the data that you need, but I don't know if this has some penalty in the performance of the query. Remember that this method is for PostgreSQL versions 8.2 and up.

凉薄对峙 2024-07-12 04:36:26

从 PostgreSQL 的角度来看,在伪代码中:

 * $insert_id = INSERT...RETURNING foo_id;-- only works for PostgreSQL >= 8.2. 

 * INSERT...; $insert_id = SELECT lastval(); -- works for PostgreSQL >= 8.1

 * $insert_id = SELECT nextval('foo_seq'); INSERT INTO table (foo...) values ($insert_id...) for older PostgreSQL (and newer PostgreSQL)

pg_last_oid() 仅适用于有 OID 的情况。 自 PostgreSQL 8.1 起,OID 默认处于关闭状态。

因此,根据您使用的 PostgreSQL 版本,您应该选择上述方法之一。 当然,理想情况下,使用数据库抽象库来抽象上述内容。 否则,在低级代码中,它看起来像:

方法一:INSERT... RETURNING

// yes, we're not using pg_insert()
$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_row($result);
$insert_id = $insert_row[0];

方法二:INSERT; lastval()

$result = pg_execute($db, "INSERT INTO foo (bar) values (123);");
$insert_query = pg_query("SELECT lastval();");
$insert_row = pg_fetch_row($insert_query);
$insert_id = $insert_row[0];

方法三:nextval(); INSERT

$insert_query = pg_query($db, "SELECT nextval('foo_seq');");
$insert_row = pg_fetch_row($insert_query);
$insert_id = $insert_row[0];
$result = pg_execute($db, "INSERT INTO foo (foo_id, bar) VALUES ($insert_id, 123);");

最安全的选择是第三种方法,但它很笨拙。 最干净的是第一个,但您需要运行最近的 PostgreSQL。 不过,大多数数据库抽象库尚未使用第一种方法。

From the PostgreSQL point of view, in pseudo-code:

 * $insert_id = INSERT...RETURNING foo_id;-- only works for PostgreSQL >= 8.2. 

 * INSERT...; $insert_id = SELECT lastval(); -- works for PostgreSQL >= 8.1

 * $insert_id = SELECT nextval('foo_seq'); INSERT INTO table (foo...) values ($insert_id...) for older PostgreSQL (and newer PostgreSQL)

pg_last_oid() only works where you have OIDs. OIDs have been off by default since PostgreSQL 8.1.

So, depending on which PostgreSQL version you have, you should pick one of the above method. Ideally, of course, use a database abstraction library which abstracts away the above. Otherwise, in low level code, it looks like:

Method one: INSERT... RETURNING

// yes, we're not using pg_insert()
$result = pg_query($db, "INSERT INTO foo (bar) VALUES (123) RETURNING foo_id");
$insert_row = pg_fetch_row($result);
$insert_id = $insert_row[0];

Method two: INSERT; lastval()

$result = pg_execute($db, "INSERT INTO foo (bar) values (123);");
$insert_query = pg_query("SELECT lastval();");
$insert_row = pg_fetch_row($insert_query);
$insert_id = $insert_row[0];

Method three: nextval(); INSERT

$insert_query = pg_query($db, "SELECT nextval('foo_seq');");
$insert_row = pg_fetch_row($insert_query);
$insert_id = $insert_row[0];
$result = pg_execute($db, "INSERT INTO foo (foo_id, bar) VALUES ($insert_id, 123);");

The safest bet would be the third method, but it's unwieldy. The cleanest is the first, but you'd need to run a recent PostgreSQL. Most db abstraction libraries don't yet use the first method though.

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