SQLite | トリガー | トリガーを削除する

トリガーを削除する方法を説明します。トリガーは直接削除できるほか、関連するテーブルが削除されたときにも自動的に削除されます。

作成済みのトリガーを削除する

DROP TRIGGER文を使います。

DROP TRIGGER トリガー名;

削除前に.schemaコマンドでトリガーを確認します。

.schema
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;
sqlite> 

updateproductトリガーを削除します。

drop trigger updateproduct;
sqlite> drop trigger updateproduct;
sqlite> 

.schemaを再度実行して削除を確認します。

.schema
sqlite> .schema
CREATE TABLE product (id integer, name text);
CREATE TABLE history (user text, name text, sales integer);
sqlite> 

updateproductトリガーが表示されなくなりました。

トリガーが設定されたテーブルを削除する

テーブルを削除すると、そのトリガーも自動的に削除されます。

.schemaで現在のテーブルとトリガーを表示します。

.schema
.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;
sqlite> 

前の例でトリガーが削除されている場合は、表示されたスキーマから再作成してください。

updateproductトリガーはproductテーブルに設定されています。テーブルを削除し、.schemaを再度実行します。

drop table product;
sqlite> drop table product;
sqlite> 
sqlite> .schema
CREATE TABLE history (user text, name text, sales integer);
sqlite> 

productテーブルを削除すると、updateproductトリガーも自動的に削除されます。