我有一张 table :
CREATE TABLE Students (studentId TEXT PRIMARY KEY, name TEXT);
我想在表中插入记录,如果我插入一个学生两次我想要 第二个插入覆盖(更新)第一条记录。
INSERT INTO Students (StudentId, name) VALUES ('123', 'Jones');
INSERT INTO Students (StudentId, name) VALUES ('123', 'Jonas');
这样做的最佳方法是什么?
最佳答案
尝试 REPLACE
:
REPLACE INTO Students (StudentId, name) VALUES ('123', 'Jonas');
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
关于mysql - 如果它已经在表中,我如何强制 INSERT 插入到具有唯一键的表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043908/