SQLite数据库中有一个经典的父/子表集:
CREATE TABLE ImageGallery (
ImageGalleryId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
Name VARCHAR(80) UNIQUE NOT NULL
);
CREATE TABLE Image (
ImageId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
ImageGalleryId INTEGER NOT NULL,
Filename VARCHAR(260) NOT NULL,
FullPath VARCHAR(260) NOT NULL
);
在删除图像后需要触发一个触发器,如果该图库中没有更多图像,则会删除ImageGallery,基本上是这样的:
if ((select count(*) from Image where ImageGalleryId = old.imageGalleryId) == 0) then
begin
delete from ImageGallery where ImageGalleryId = old.imageGalleryId
end
在SQLite方言中究竟是什么样的?
答案 0 :(得分:1)
SQLite没有IF语句。
但是,触发器有WHEN clause:
CREATE TRIGGER xxx
AFTER DELETE ON Image
WHEN (SELECT COUNT(*) FROM Image WHERE ImageGalleryId = OLD.ImageGalleryId) = 0
BEGIN
DELETE FROM ImageGallery WHERE ImageGalleryId = OLD.ImageGalleryId;
END;