H2 SQL database - INSERT if the record does not exist

后端 未结 4 1183
名媛妹妹
名媛妹妹 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 05:07

    The following works for MySQL, PostgreSQL, and the H2 database:

    drop table ACCESSLEVELS;
    
    create table ACCESSLEVELS(id int, name varchar(255));
    
    insert into ACCESSLEVELS select * from (
    select 0, 'admin' union
    select 1, 'SEO' union
    select 2, 'sales director' union
    select 3, 'manager' union
    select 4, 'REP'
    ) x where not exists(select * from ACCESSLEVELS);
    

提交回复
热议问题