SQLite | トリガー(Trigger) | トリガーのスキーマを確認する
トリガーの作成に使われたCREATE文を確認する2つの方法、sqlite_masterテーブルを検索する方法と.schemaコマンドを使う方法を説明します。
sqlite_masterテーブルを検索する
sqlite_masterテーブルを検索します。先にlineモードへ切り替えると結果を読みやすくできます。
select * from sqlite_master;
sqlite> .mode line
sqlite> select * from sqlite_master;
type = table
name = product
tbl_name = product
rootpage = 3
sql = CREATE TABLE product (id integer, name text)
type = table
name = history
tbl_name = history
rootpage = 2
sql = CREATE TABLE history (user text, name text, sales integer)
type = trigger
name = updateproduct
tbl_name = product
rootpage = 0
sql = CREATE TRIGGER updateproduct update of name on product
begin
update history set name = new.name where name = old.name;
end
トリガーの場合、typeはtriggerです。nameにはトリガー名、tbl_nameには対象テーブル名、sqlにはトリガーを作成したSQL文が表示されます。トリガーだけを表示するにはWHERE句を追加します。
select * from sqlite_master where type = 'trigger';
type = trigger
name = updateproduct
tbl_name = product
rootpage = 0
sql = CREATE TRIGGER updateproduct update of name on product
begin
update history set name = new.name where name = old.name;
end
.schemaコマンドで確認する
.schemaコマンドでもトリガーのスキーマを確認できます。
.schema
.schema? TABLE?
引数を省略すると、すべてのテーブルとトリガーのスキーマが表示されます。テーブル名を指定すると、一致するテーブルと関連するトリガーが表示されます。
sqlite> .schema
CREATE TABLE product (id integer, name text);
CREATE TABLE history (user text, name text, sales integer);
CREATE TRIGGER updateproduct update of name on product
begin
update history set name = new.name where name = old.name;
end;
現在のデータベースにある2つのテーブルと1つのトリガーのCREATE文を確認できます。