Linux での,SQLite 3 のインストール,起動と終了,データベースの新規作成,テーブル定義の手順をスクリーンショットで説明する. SQLite 3は,リレーショナルデータベース管理システム.
SQLite 3は次の特徴を持つ.
【目次】
【サイト内の主な SQLite 3 関連ページ】
■ Ubuntu での実行手順例
sudo apt -y update sudo apt -y install sqlite3
Linux の端末 (terminal) で,次の操作を行う.
このとき,データベース名として /var/tmp/mydb を指定する.SQLite 3の流儀で,データベース名はファイル名になる.
※ データベース名はなんでも良いが、英文字と数字のみを使うのが良い.
※ データベース名は「../hoge」のような相対パス形式でもいいし,カレントディレクトリを使うつもりで「hoge2」のようにしてもいい.
sqlite3 /var/tmp/mydb
「.help」で,ヘルプが表示される.
.help
「PRAGMA encoding;」で,エンコーディングが表示される.
PRAGMA encoding;
「.exit」で終了.
.exit
ここでの設定
sqlite3
空のデータベースを作成したいので次のように操作.
「--new 」を付けているので,すでにデータベースファイルが存在するときは,その中身を空にする.
.open --new /var/tmp/hoge.db
.exit
ここで定義するテーブル: P(id, name, weight)
sqlite3
空のデータベースを作成したいので次のように操作.
「--new 」を付けているので,すでにデータベースファイルが存在するときは,その中身を空にする.
.open --new /var/tmp/hoge.db
create table order_records ( id integer primary key not null, year integer not null CHECK ( year > 2008 ), month integer not null CHECK ( month >= 1 AND month <= 12 ), day integer not null CHECK ( day >= 1 AND day <= 31 ), customer_name text not null, product_name text not null, unit_price real not null check ( unit_price > 0 ), qty integer not null default 1 check ( qty > 0 ), created_at timestamp not null default (datetime('now', 'localtime')), updated_at timestamp not null default (datetime('now', 'localtime')), check ( ( unit_price * qty ) < 200000 ) ); create trigger order_records_update after update on order_records begin update order_records set last_updated_at = (datetime('now', 'localtime')) where id = new.id; end;
begin transaction; insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2020, 7, 26, 'kaneko', 'orange A', 1.2, 10 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2020, 7, 26, 'miyamoto', 'Apple M', 2.5, 2 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2020, 7, 27, 'kaneko', 'orange B', 1.2, 8 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2020, 7, 28, 'miyamoto', 'Apple L', 3 ); commit;
select * from order_records;
begin transaction; update order_records set unit_price = 11.2 where id = 1; commit; select * from order_records;
.tables
.exit