因为我们不能在复合键上使用自动递增功能
下面有一个复合键,其唯一id递增(您称之为自动递增):-
DROP TABLE IF EXISTS table_main;
DROP TABLE IF EXISTS table_fkx;
DROP TABLE IF EXISTS table_fky;
DROP TABLE IF EXISTS table_fkz;
DROP TRIGGER IF EXISTS [autoincrement];
CREATE TABLE IF NOT EXISTS table_fkx (id INTEGER PRIMARY KEY, datacol TEXT);
CREATE TABLE IF NOT EXISTS table_fky (id INTEGER PRIMARY KEY, datacol TEXT);
CREATE TABLE IF NOT EXISTS table_fkz (id INTEGER PRIMARY KEY, datacol TEXT);
CREATE TABLE IF NOT EXISTS table_main (
id INTEGER PRIMARY KEY,
id_x INTEGER REFERENCES table_fkx(id),
id_y INTEGER REFERENCES table_fky(id),
id_z INTEGER REFERENCES table_fkz(id),
UNIQUE(id, id_x, id_y, id_z)
);
/*
CREATE TRIGGER [autoincrement]
AFTER INSERT
ON table_main
WHEN NEW.id IS NULL
BEGIN
UPDATE table_main
SET id = IFNULL((SELECT MAX(id) FROM table_main) + 1, 0),
id_x = IFNULL((SELECT MAX(id_x) FROM table_main) + 1, 0),
id_y = IFNULL((SELECT MAX(id_y) FROM table_main) + 1, 0),
id_z = IFNULL((SELECT MAX(id_z) FROM table_main) + 1, 0);
END;
*/
INSERT INTO table_fkx VALUES (10,'some data'),(33,'more data'),(56,'even more data');
INSERT INTO table_fky VALUES (73,'some data'),(1200,'more data'),(560,'even more data');
INSERT INTO table_fkz VALUES (15,'some data'),(1500,'more data'),(123456,'even more data');
INSERT INTO table_main (id_x,id_y,id_z) VALUES
(10,1200,15),(56,1200,15),(33,560,15),(10,73,15) -- etc
;
-- INSERT what could be a considered a duplicate but now is not as the autoincremnt(sic) makes it unique
INSERT INTO table_main (id_x,id_y,id_z) VALUES (33,560,15); -- i.e. same as 3rd
SELECT * FROM table_main
JOIN table_fkx ON id_x = table_fkx.id
JOIN table_fky ON id_y = table_fky.id
JOIN table_fkz ON id_z = table_fkz.id
;