やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

SQLite3構文 insert

 レコードを挿入する。

成果物

構文

insert into * values (*)

create table T(A text);
insert into T values('A');
select * from T;
  • values()内はテーブル定義した列の数・順でなければならない

insert into *(*) values (*)

create table T(A integer primary key, B text);
insert into T(B) values('A');
select * from T;
  • integer primary key列は省略できる。省略時はrowid+1(last_insert_rowid()+1)
  • default値がある列は省略できる。省略したときの値はdefault値になる
  • values()には少なくとも1つ以上の値がなければならない(すべて省略することは不可)
create table T(A integer primary key, B text, C text default '');
insert into T(B) values('A');
select * from T;
1|A|

 すべての列を省略することはできない。

create table T(A integer primary key, B text default '', C text default '');
insert into T() values();
insert into T() values;
insert into T();
insert into T values();
insert into T values;
insert into T;
Error: near ")": syntax error
Error: near ")": syntax error
Error: near ")": syntax error
Error: near ";": syntax error
Error: near ";": syntax error
Error: near ";": syntax error

replace into * values (*)

create table T(A integer primary key, B text);
replace into T(A,B) values(1,'A');
replace into T(A,B) values(1,'B');
select * from T;
1|B

 主キーが重複したときは更新する。もしinsert into文なら以下エラーになる。

Error: UNIQUE constraint failed: T.A

insert or ... into * values (*)

replace

create table T(A integer primary key, B text);
insert or replace into T(A,B) values(1,'A');
insert or replace into T(A,B) values(1,'B');
select * from T;
1|B

 主キーが重複したときは更新する。もしinsert into文なら以下エラーになる。

Error: UNIQUE constraint failed: T.A

rollback

create table T(A integer primary key, B text);
insert or rollback into T(A,B) values(0,'a');
begin;
insert or rollback into T(A,B) values(1,'A');
insert or rollback into T(A,B) values(1,'B');
insert or rollback into T(A,B) values(2,'C');
end;
insert or rollback into T(A,B) values(9,'z');
select * from T;
Error: UNIQUE constraint failed: T.A
0|a
2|C
9|z

 B挿入時にエラーになってロールバックした。

abort

create table T(A integer primary key, B text);
insert or abort into T(A,B) values(0,'a');
begin;
insert or abort into T(A,B) values(1,'A');
insert or abort into T(A,B) values(1,'B');
insert or abort into T(A,B) values(2,'C');
end;
insert or abort into T(A,B) values(9,'z');
select * from T;
Error: UNIQUE constraint failed: T.A
0|a
1|A
2|C
9|z

 B挿入時にエラーになってそのステートメントのみ中断した。

fail

create table T(A integer primary key, B text);
insert or fail into T(A,B) values(0,'a');
begin;
insert or fail into T(A,B) values(1,'A');
insert or fail into T(A,B) values(1,'B');
insert or fail into T(A,B) values(2,'C');
end;
insert or fail into T(A,B) values(9,'z');
select * from T;
Error: UNIQUE constraint failed: T.A
0|a
1|A
2|C
9|z

 B挿入時にエラーになってそのステートメントのみ失敗した。

ignore

create table T(A integer primary key, B text);
insert or ignore into T(A,B) values(0,'a');
begin;
insert or ignore into T(A,B) values(1,'A');
insert or ignore into T(A,B) values(1,'B');
insert or ignore into T(A,B) values(2,'C');
end;
insert or ignore into T(A,B) values(9,'z');
select * from T;
Error: UNIQUE constraint failed: T.A
0|a
1|A
2|C
9|z

 B挿入時にエラーになってそのステートメントのみ無視した。

insert into * values (*) on conflict do nothing

create table T(A integer primary key, B text);
insert into T(A,B) values(0,'a') on conflict do nothing;
begin;
insert into T(A,B) values(1,'A') on conflict do nothing;
insert into T(A,B) values(1,'B') on conflict do nothing;
insert into T(A,B) values(2,'C') on conflict do nothing;
end;
insert into T(A,B) values(9,'z') on conflict do nothing;
select * from T;
0|a
1|A
2|C
9|z

 1,Bを挿入するところでError: UNIQUE constraint failed: T.Aエラーが出るはずなのに出ない。これがon conflict do nothingの効果。

insert into * values (*) on conflict (*) where * nothing

 conflict()にはインデックス列を入れるらしい。

create table T(A integer primary key, B text);
insert into T(A,B) values(0,'a');
begin;
insert into T(A,B) values(1,'A') on conflict(A) where A=1 do nothing;
insert into T(A,B) values(1,'B') on conflict(A) where A=1 do nothing;
insert into T(A,B) values(2,'C');
end;
insert into T(A,B) values(9,'z');
select * from T;
0|a
1|A
2|C
9|z

 where句の値をわざとずらしたが、結果は同じ。何の意味があるの? 予想では以下コードの1,'B'挿入時にError: UNIQUE constraint failed: T.Aエラーが出ると思ったのだが。

create table T(A integer primary key, B text);
insert into T(A,B) values(0,'a');
begin;
insert into T(A,B) values(1,'A') on conflict(A) where A=3 do nothing;
insert into T(A,B) values(1,'B') on conflict(A) where A=3 do nothing;
insert into T(A,B) values(2,'C');
end;
insert into T(A,B) values(9,'z');
select * from T;
0|a
1|A
2|C
9|z

insert into * values (*) on conflict (*) do update set *=* where *

 重複チェック対象の列を指定できる? 指定せねばならない?

create table T(A integer primary key, B text);
insert into T(A,B) values(0,'a');
begin;
insert into T(A,B) values(1,'A') on conflict(A) do update set B='AAA';
insert into T(A,B) values(1,'B') on conflict(A) do update set B='BBB';
insert into T(A,B) values(2,'C');
end;
insert into T(A,B) values(9,'z');
select * from T;
0|a
1|BBB
2|C
9|z

 別の列でやったらエラーになった。主キーか一意キーでないとダメらしい。

create table T(A integer primary key, B text);
begin;
insert into T(B) values('A') on conflict(B) do update set B='AAA';
insert into T(B) values('A') on conflict(B) do update set B='BBB';
end;
select * from T;
Error: ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint
Error: ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint

 別の列でやったらエラーになった。主キーか一意キーでないとダメらしい。

create table T(A integer primary key, B text unique);
begin;
insert into T(B) values('A') on conflict(B) do update set B='AAA';
insert into T(B) values('A') on conflict(B) do update set B='BBB';
end;
select * from T;
1|BBB

 できた。

 複数の列でやるとエラー。

create table T(A integer primary key, B text unique);
begin;
insert into T(A,B) values(1,'A') on conflict(A,B) do update set A=10, B='AAA';
insert into T(A,B) values(1,'A') on conflict(A,B) do update set A=10, B='BBB';
end;
select * from T;
Error: ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint
Error: ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint

 書式を変えてもダメ。

create table T(A integer primary key, B text unique);
begin;
insert into T(A,B) values(1,'A') on conflict(A,B) do update set (A,B)=(10, 'AAA');
insert into T(A,B) values(1,'A') on conflict(A,B) do update set (A,B)=(10, 'BBB');
end;
select * from T;
Error: ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint
Error: ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint

insert into * select-stmt on conflict ...

create table T(A integer primary key, B text);
insert into T(B) select 'A' union select 'B';
select * from T;
1|A
2|B
create table T(A integer primary key, B text);
insert into T(A,B) select 1,'A' union select 1,'B' on conflict(A) do update set B='AAA';
select * from T;
1|AAA

insert into * default values

create table T(A integer primary key, B text default 'DEF');
insert into T default values;
select * from T;
1|DEF

 integer primary keydefault制約がない列はNULLが入る。

create table T(A integer primary key, B text default 'DEF', C text);
insert into T default values;
select * from T;
1|DEF|

 もしそれがunique制約つきでも、NULLuniqueに違反しないのでセーフ。

create table T(A integer primary key, B text default 'DEF', C text unique);
insert into T default values;
insert into T default values;
select * from T;
1|DEF|
2|DEF|

 conflict句は使えない。

create table T(A integer primary key, B text default 'DEF');
insert into T default values on conflict do nothing;
select * from T;
Error: near "on": syntax error

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux

前回まで