sqlite3 [file.sqlite3]
.exit / .quit
.help
create table [テーブル名](フィールド1, フィールド2...);
カラム名は省略可能。例:
create table users(name, email);
.tables
drop table users;
alter table [テーブル名] rename to [新テーブル名];
.schema [テーブル名]
テーブル名を指定しない場合は、全テーブルを表示。
alter table [テーブル名] add column [カラム名];
[カラム名] integer primary key autoincrement | 自動連番 |
[カラム名] text not null | 空欄エラー |
[カラム名] text unique | ユニークな文字列以外エラー |
[カラム名] integer default 20 | デフォルト値 |
[カラム名] check(カラム名>0) | 0より小さいとエラー(例) |
テーブル作成時にも使用可能。
挿入場所を指定:
[カラム名] first | 最初 |
[カラム名] after [カラム名2] | カラム名2の後に追加 |
insert into [テーブル名](カラム1, カラム2...) values(値1, 値2...);
insert into users values('yamada', 25);
insert into users(age) values(25);
シングルクォーテーションを書く場合は、2回続けて('It''s a pen.'
)書く。
nullはシングルクォーテーションで囲わない。
update [テーブル名] set [カラム名] = [値] where [条件];
update users set name = 'tanaka' where name = 'yamada';
update users set name = 'tanaka', score = 500 where name = 'yamada';
delete from [テーブル名] where [条件];
delete from users where score <= 100;
データ番号
取得:
select ROWID, * from users;
削除:
delete from users where ROWID = 4;
select [カラム名] from [テーブル名];
select * from users;
select name from users;
order by [カラム名] / order by [カラム名] desc
select * order by score;
select * order by score desc;
limit [数]
select * order by score desc limit 5;
where
select * where score >= 200 order by score;
select * where score is null;
null
を取得するには、is null (isnull)
やis not null (not null, notnull)
を使う。
演算子:+, -, *, /, %
、=, ==, !=, <>, <, <=, >, >=
、and, or, not
distinct
select distinct team from users;
group by [カラム名]
select team, sum(score) from users group by team;
as [別名]
select id, name as username from users;
select id, name, team, sum(score) from users, games where users.id = games.user_id group by users.id;
count(*)
select count(*) from users;
max(カラム名) / min(カラム名)
select max(*) from users;
select max(*) from users;
random()
select * from users order by random() limit 1; -- 1つランダムに選ぶ
length(カラム名)
select name, length(name) from users;
sum(カラム名)
select sum(score) from users;
avg(カラム名)
select avg(score) from users;
typeof(カラム名)
select name, typeof(name) from users;
current_date / current_time / current_timestamp
insert into users(name, created) values('yamada', current_timestamp);
strfttime()
select strfttime('%Y年%m月%d日', current_timestamp);
%Y | 年 |
%m | 月(01-) |
%d | 日(00-) |
%W | 年の初めからの週(00-53) |
%j | 年の初めからの経過日数(001-366) |
%w | 曜日(0-6, 日-土) |
%H | 時(00-24) |
%M | 分(00-59) |
%S | 秒(00-59) |
%f | 秒+ミリ秒(SS.SSS) |
%s | 1970/1/1からの経過秒数 |
%% | % |
.header [on|off]
.show
.mode [モード]
mode | 書式 | メモ |
csv | カンマ区切り | 文字列は""で囲われる |
column | 指定幅で表示 | 幅は.widthで変更可能 |
html | HTMLのTABLE | <table> は出力されない
|
insert | insert文 | |
line | カラムごとに行を分けて中央揃え | カラム名と値の間は= |
list | 区切り文字で区切る | 区切り文字は.separatorで変更可能 |
tabs | タブ区切り | タブは8文字 |
tcl | |で区切る | すべて文字列扱い(""で囲われる) |
.width 10
.width 10 5 30
.separator ,
.import [ファイル名] [テーブル名]
.import users.txt users
区切り文字を.separator
で変更しておく必要がある。
.dump [テーブル名]
.dump users
出力先ファイルを.output
で指定しておく必要がある。
.read [ファイル名]
.read users.txt
データベースの中のカテゴリ(Excelのシート)
列
行
© 2019 shge.github.io 利用規約・プライバシー