简体   繁体   English

CodeIgniter影响postgres中的行

[英]affected rows in postgres with CodeIgniter

I have a following query: 我有以下查询:

UPDATE "user" SET "first_name" = 'abc', "last_name" = '', "mobile" = '988988888', "comment" = 'Hello' WHERE "id" = '15' 更新“用户” SET“名字” =“ abc”,“姓氏” =“”,“移动” =“ 988988888”,“评论” =“你好”,其中“ id” =“ 15”

and above data is already in db. 以上数据已经在数据库中。 means i submit form without changing any data. 表示我无需更改任何数据即可提交表格。

I hit above query in terminal and it says : UPDATE 1 我在终端中打了上面的查询,它说: UPDATE 1

CI CODE:

$query = $this->db->get_where('user',array('id'=>$id),1);
if ($query->num_rows() > 0)
{
    $this->db->update('user', $data, array('id'=>$id));         
    echo $afftected_rows = $this->db->affected_rows();exit;
}

DB SCHEMA(export using psql cmmand)

CREATE TABLE "user" (
  id integer NOT NULL,
  first_name character varying(50),
  last_name character varying(50),
  mobile character varying(50),  
  comment character varying(500)
);

so what is the problem? 那是什么问题呢? why it is return 1, even if i don't change any data. 为什么返回1,即使我不更改任何数据也是如此。 is this the normal behaviour for postgres? 这是postgres的正常行为吗?

Yes, it's the normal behavior. 是的,这是正常现象。

MySQL has the notion of "matched rows" and "affected rows" which might differ: when a "matched row" would be updated with the values that it already holds, it's not affected. MySQL具有“匹配的行”和“受影响的行”的概念,它们可能有所不同:当将“匹配的行”更新为其已保存的值时,它不会受到影响。

In PostgreSQL, only the count of "affected rows" comes back to the user, and all "matched rows" are affected. 在PostgreSQL中,只有“受影响的行”的计数返回给用户,并且所有“匹配的行”都会受到影响。


To be complete, strictly speaking, this behavior is amendable in PostgreSQL. 严格来说,要完整,此行为在PostgreSQL中是可修改的。 Skipping these updates is always possible with a fairly generic trigger. 总是可以使用相当通用的触发器来跳过这些更新。

Example: 例:

First the baseline,default behavior, to compare against: 首先将基线与默认行为进行比较:

test=> create table update_test(val text);
CREATE TABLE
test=> insert into update_test values('abc');
INSERT 0 1
test=> update update_test set val='abc';
UPDATE 1

This UPDATE 1 indicates that 1 row was affected, even though the value is the same. UPDATE 1表示影响了1行,即使该值相同。

Now let's tell it to skip the rows for which values don't change. 现在,让我们告诉它跳过不更改其值的行。

-- generic trigger to void the update of one row
create function void_update() returns trigger language plpgsql as 
 'begin return null; end';

-- trigger affecting unchanged rows in a BEFORE UDPATE event
create trigger update_trigger 
  before update on update_test for each row
  when (old is not distinct from new)
  execute procedure void_update();

Now we get the MySQL-like behavior: 现在我们得到类似MySQL的行为:

test=> update update_test set val='abc';
UPDATE 0

0 row is affected, because we're updating the only row of the table with a value it already has. 0行受到影响,因为我们正在使用表中已有的值更新表的唯一行。

Test again with more data, some rows to skip, other rows to change: 再次测试更多数据,跳过一些行,更改其他行:

test=> insert into update_test values('def'),('ghi');
INSERT 0 2
test=> select * from update_test ;
 val 
-----
 abc
 def
 ghi
(3 rows)

test=> update update_test set val='ghi';
UPDATE 2

Only 2 rows were affected since the last row already contained 'ghi' 由于最后一行已经包含“ ghi”,因此仅影响了2行

Check that the update actually works: 检查更新是否确实有效:

test=> select * from update_test ;
 val 
-----
 ghi
 ghi
 ghi
(3 rows)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM