H2 SQL database - INSERT if the record does not exist

后端 未结 4 1186
名媛妹妹
名媛妹妹 2020-12-19 04:29

I would like initialize a H2 database, but I am not sure if the records exist. If they exist I don\'t want to do anything, but if they don\'t exist I would like to write the

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 04:59

    To do this you can use MySQL Compatibility Mode in H2 database. Starting from 1.4.197 version it supports the following syntax: INSERT IGNORE INTO table_name VALUES ...

    From this pull request:

    INSERT IGNORE is not supported in Regular mode, you have to enable MySQL compatibility mode explicitly by appending ;MODE=MySQL to your database URL or by executing SET MODE MySQL statement.

    From official site:

    INSERT IGNORE is partially supported and may be used to skip rows with duplicate keys if ON DUPLICATE KEY UPDATE is not specified.

提交回复
热议问题