我在SQLite版本3.16.0上运行以下脚本wtf.sql
:
PRAGMA foreign_keys = ON;
CREATE TABLE 'ZIP'(
'Zip' INTEGER PRIMARY KEY NOT NULL,
'City' TEXT NOT NULL,
'State' TEXT NOT NULL
);
CREATE TABLE 'ADDRESS'(
'Address_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'Line_1' TEXT NOT NULL,
'Line_2' TEXT,
'Zip' INTEGER NOT NULL,
FOREIGN KEY('Zip') REFERENCES ZIP('Zip') ON DELETE CASCADE
);
INSERT OR IGNORE INTO ZIP('Zip','City','State')
VALUES ('90210','Beverly Hills','CA')
;
INSERT INTO ADDRESS('Line_1','Line_2','Zip')
VALUES ('1234 Overpriced House Street','c/o Bob','90210')
;
DELETE FROM ADDRESS
WHERE Zip = '90210'
;
SELECT *
FROM ZIP
;
将以下内容键入bash:
$ sqlite3 bits-n-books.db < wtf.sql
返回以下元组:
90210|Beverly Hills|CA
如果要正确删除级联,我需要做什么?
答案 0 :(得分:2)
INSERT INTO ADDRESS(Line_1,Line_2,Zip)
VALUES ('1234 Overpriced House Street','c/o Bob','90210'),
('667 Another Street','2nd row','90210');
DELETE FROM ADDRESS WHERE Line_2 = '2nd row';
现在应该怎么办?是否还应删除引用90210
的所有其他地址?
删除父行时,ON DELETE CASCADE action会删除所有子行。在这种情况下,这意味着从ZIP
删除行将删除所有相应的ADDRESS
行。另一方面没有类似的机制。