我想把主键的值给替换掉

解决方案 »

  1.   

    replace into 是不行的,它本身就是按主键来查找,否则就insert 了mysql> create table tx (id int primary key,f1 int);
    Query OK, 0 rows affected (0.06 sec)mysql> select * from tx;
    +----+------+
    | id | f1   |
    +----+------+
    |  1 |   10 |
    |  2 |   20 |
    +----+------+
    2 rows in set (0.00 sec)mysql> REPLACE tx VALUES (1,30);
    Query OK, 2 rows affected (0.05 sec)mysql> select * from tx;
    +----+------+
    | id | f1   |
    +----+------+
    |  1 |   30 |
    |  2 |   20 |
    +----+------+
    2 rows in set (0.00 sec)mysql> REPLACE tx VALUES (3,30);
    Query OK, 1 row affected (0.06 sec)mysql> select * from tx;
    +----+------+
    | id | f1   |
    +----+------+
    |  1 |   30 |
    |  2 |   20 |
    |  3 |   30 |
    +----+------+
    3 rows in set (0.00 sec)mysql> update tx set id=9 where id=1;
    Query OK, 1 row affected (0.08 sec)
    Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from tx;
    +----+------+
    | id | f1   |
    +----+------+
    |  2 |   20 |
    |  3 |   30 |
    |  9 |   30 | // PK id was updated.
    +----+------+
    3 rows in set (0.00 sec)mysql>