mysql如果存在一行数据,主库和从库主键相同而其他列值不同,源端Update或者delete该行,从库会update和delete这一行吗

从库会update或delete,而且会update成和主库完全相同的一行。这一点主从复制和CDC复制的一样的,都是以主键为查询基准的。复制程序很聪明,如果一张表存在主键的话,update或着delete该表的话,从库的复制程序会通过主键索引查询改行,如果主键返回一行数据就默认是完全相同一行的数据,而不会再去对比其他列值。

update测试:

主库:

mysql> create table test_strict (id int not null primary key,name varchar(10));
Query OK, 0 rows affected (0.02 sec)


mysql> insert into test_strict values(1,'lisi'); 
Query OK, 1 row affected (0.00 sec)

mysql> select * from test_strict;
+----+------+
| id | name |
+----+------+
|  1 | lisi |
+----+------+
1 row in set (0.00 sec)

从库:

[root@localhost:mytest1]>select * from test_strict;
+----+------------+
| id | name       |
+----+------------+
|  1 | zhangsan88 |
+----+------------+
1 row in set (0.00 sec)

主库:

mysql> update test_strict set id=88 where id=1 and name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> 

从库:

[root@localhost:mytest1]>select * from test_strict;
+----+------+
| id | name |
+----+------+
| 88 | lisi |
+----+------+
1 row in set (0.00 sec)

delete测试:

主:

mysql> select * from test_strict;
+----+------+
| id | name |
+----+------+
| 88 | lisi |
+----+------+
1 row in set (0.00 sec)
从:

[root@localhost:mytest1]>select * from test_strict;
+----+-----------+
| id | name      |
+----+-----------+
| 88 | zhangsan3 |
+----+-----------+
1 row in set (0.00 sec)

主:

mysql> delete from test_strict where id=88 and name='lisi';
Query OK, 1 row affected (0.00 sec)

从:

[root@localhost:mytest1]>select * from test_strict;
Empty set (0.00 sec)

你可能感兴趣的:(mysql,数据库)