我想我需要在 sqlite 插入或替换中进行某种级联 id 更新,但没有这样的功能。
有这样的特点;您指定ON UPDATE CASCADE
为 FOREIGN KEY 定义/子句的一部分。您还可以编写ON DELETE CASCADE
SQLite 外键支持 - 4.3。ON DELETE 和 ON UPDATE 操作。
因为这被编码为子表的一部分,并且您不能使用 ALTER TABLE COLUMN 来更改列的属性(除了重命名列)。你不得不 :-
- 重命名子表。
- 使用更改后的原始名称创建替换表,然后使用
INSERT INTO replacement_table_with_original_name SELECT * FROM renamed_table
(显然更改replacement_table_with_original_name和renamed_table)到实际名称。
- 当高兴 DROP 重命名的表。
例子
例如,考虑您的场景的以下表示(来自可用信息)然后这将添加ON UPDATE CASCADE
并且ON DELETE CASCADE
在保留现有数据的同时,它另外删除没有冲突的行,并且还更新引用的值而没有冲突或丢失:-
-- Create the original tables and load some test data
CREATE TABLE IF NOT EXISTS word_table (id INTEGER PRIMARY KEY, other_column TEXT);
INSERT INTO word_table VALUES (null,'WA'),(null,'WB'),(null,'WC');
CREATE TABLE IF NOT EXISTS collection_table (id INTEGER PRIMARY KEY, other_column TEXT);
INSERT INTO collection_table VALUES (null,'CA'),(null,'CB'),(null,'CC');
CREATE TABLE IF NOT EXISTS word_collection_map (
word_reference INTEGER NOT NULL REFERENCES word_table(id),
collection_reference INTEGER NOT NULL REFERENCES collection_table(id),
PRIMARY KEY (word_reference,collection_reference) ) WITHOUT ROWID;
INSERT INTO word_collection_map VALUES(1,1),(3,2),(3,3),(2,1),(2,2),(2,3);
-- show the result
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
;
-- ADDING CASCADE
ALTER TABLE word_collection_map RENAME TO old_word_collection_map;
CREATE TABLE IF NOT EXISTS word_collection_map (
word_reference INTEGER NOT NULL REFERENCES word_table(id) ON UPDATE CASCADE ON DELETE CASCADE ,
collection_reference INTEGER NOT NULL REFERENCES collection_table(id) ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY (word_reference,collection_reference) ) WITHOUT ROWID;
INSERT INTO word_collection_map SELECT * FROM old_word_collection_map;
DROP TABLE IF EXISTS old_word_collection_map;
-- Show results (should match first)
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
;
-- DELETE a word no conflicts
DELETE FROM word_table WHERE other_column = 'WC';
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
;
-- UPDATE the FK parent no conflicts
UPDATE collection_table SET id = 10 WHERE id = 3;
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
;
结果 :-
日志:-
-- Create the original tables and load some test data
CREATE TABLE IF NOT EXISTS word_table (id INTEGER PRIMARY KEY, other_column TEXT)
> OK
> Time: 0.118s
INSERT INTO word_table VALUES (null,'WA'),(null,'WB'),(null,'WC')
> Affected rows: 3
> Time: 0.104s
CREATE TABLE IF NOT EXISTS collection_table (id INTEGER PRIMARY KEY, other_column TEXT)
> OK
> Time: 0.097s
INSERT INTO collection_table VALUES (null,'CA'),(null,'CB'),(null,'CC')
> Affected rows: 3
> Time: 0.094s
CREATE TABLE IF NOT EXISTS word_collection_map (
word_reference INTEGER NOT NULL REFERENCES word_table(id),
collection_reference INTEGER NOT NULL REFERENCES collection_table(id),
PRIMARY KEY (word_reference,collection_reference) ) WITHOUT ROWID
> OK
> Time: 0.097s
INSERT INTO word_collection_map VALUES(1,1),(3,2),(3,3),(2,1),(2,2),(2,3)
> Affected rows: 6
> Time: 0.105s
-- show the result
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
> OK
> Time: 0.001s
-- ADDING CASCADE
ALTER TABLE word_collection_map RENAME TO old_word_collection_map
> OK
> Time: 0.108s
CREATE TABLE IF NOT EXISTS word_collection_map (
word_reference INTEGER NOT NULL REFERENCES word_table(id) ON UPDATE CASCADE ON DELETE CASCADE ,
collection_reference INTEGER NOT NULL REFERENCES collection_table(id) ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY (word_reference,collection_reference) ) WITHOUT ROWID
> OK
> Time: 0.096s
INSERT INTO word_collection_map SELECT * FROM old_word_collection_map
> Affected rows: 6
> Time: 0.094s
DROP TABLE IF EXISTS old_word_collection_map
> OK
> Time: 0.081s
-- Show results (should match first)
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
> OK
> Time: 0s
-- DELETE a word no conflicts
DELETE FROM word_table WHERE other_column = 'WC'
> Affected rows: 1
> Time: 0.086s
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
> OK
> Time: 0.001s
-- UPDATE the FK parent no conflicts
UPDATE collection_table SET id = 10 WHERE id = 3
> Affected rows: 1
> Time: 0.089s
SELECT word_table.other_column, collection_table.other_column
FROM word_collection_map
JOIN word_table ON word_table.id = word_reference
JOIN collection_table ON collection_table.id = collection_reference
> OK
> Time: 0s
结果 1(基础数据)
结果 2(架构更改后)
结果 3(删除后)
结果4(更新后)