Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SQL替换与执行insert或update语句_Java_Sql_Mysql - Fatal编程技术网

Java SQL替换与执行insert或update语句

Java SQL替换与执行insert或update语句,java,sql,mysql,Java,Sql,Mysql,我想知道使用MySQL替换与使用update或insert语句相比有哪些优点/缺点 基本上 做 dao.replaceEntry(entry); 而不是: if(existing){ dao.insertEntry(entry); } else { dao.updateEntry(entry); } 另外,调用dao.replaceEntry调用dao.insertOrUpdate是否会产生误导?我会使用insertOrUpdate而不是replace。根据Mysql的文

我想知道使用MySQL替换与使用update或insert语句相比有哪些优点/缺点

基本上

dao.replaceEntry(entry);
而不是:

if(existing){
     dao.insertEntry(entry);
} else {
     dao.updateEntry(entry);
}

另外,调用dao.replaceEntry调用dao.insertOrUpdate是否会产生误导?

我会使用insertOrUpdate而不是replace。根据Mysql的文档

“替换”的工作原理与“插入”完全相同, 除了如果表中有一个旧的行 具有与的新行相同的值 主键或唯一索引,旧 在删除新行之前删除行 插入

如果行存在,它将执行删除,然后插入新行。更新是更好的方法,然后在替换时删除和插入,即一次操作与两次操作


我能想到的另一个问题是在数据库中使用触发器或级联删除。在REPLACE的情况下,如果已经存在具有相同ID的行,则REPLACE将首先删除相关行,然后插入。当您进行级联删除时,请不要使用REPLACE-有些人将以艰难的方式了解这一点:p。