mysql复合主键update的问题

建表content_node_relation

create table content_node_relation

(
content_id varchar(20) not null,
node_id varchar(20) not null,
primary key(content_id,node_id),
content_status int(5) not null

)ENGINE=INNODB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

content_id和node_id是复合主键,执行插入命令:

mysql> insert into content_node_relation(content_id,node_id,content_status) values('xiaohuangren','lab1412',1);

mysql> insert into content_node_relation(content_id,node_id,content_status) values('xiaohuangren','lab1411',1);

此时,想要更新第一行命令。

一开始输入的语句为:

mysql> update content_node_relation set content_id = 'xiaohuangren',node_id='lab1412',content_status=3;

报错如下:

ERROR 1062 (23000): Duplicate entry 'xiaohuangren-lab1412' for key 'PRIMARY'

换了一种语句为:

mysql> update content_node_relation set content_status=3 where content_id='xiaohuangren' , node_id='lab1412';

报错如下:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'node
id='lab1412'' at line 1

后来找到了正确的修改方式:

mysql> update content_node_relation set content_status=3 where content_id='xiaohuangren' && node_id='lab1412';

总结:

复合主键更新一条记录时,通过“&&”来实现联合主键的并列关系。




你可能感兴趣的:(mysql复合主键update的问题)