我是 mysql
的新手,正在阅读有关 duplicate key update
的内容。我们一般写的语句是这样的
insert into table (col1,col2) values(1,1) on duplicate key update col2=1;
假设 col1 是 primary
。
我对这个 is 语句的理解是,如果 col1 中有重复值,则相应的语句会将 col2 更新为 1。我的问题是为什么我们在此语句中使用术语“key
”?据了解,该语句仅在存在 primary key violation
时更新。 duplicate
是否还有其他参数或函数可以使用?
谢谢
最佳答案
如果存在主 或唯一 键违规,MySQL 会更新。 manual covers it quite extensively在带有示例的单独章节中:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row.
术语key 用作语法定义的一部分。该术语仅在执行 INSERT
语句时可用。如果您通过任何其他方式插入数据(我现在只能想到 LOAD DATA
),其他机制就会到位。
关于mysql - 为什么在重复语句中使用术语键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10975806/