I'm using SQLite3 for simple application (like agenda). Agenda is multilingual. That's the reason why I have table rooms in more languages (count of languages must be independent)
At first, I have a table rooms with this definition:
CREATE TABLE rooms
(
room_code VARCHAR2(32),
lang_code CHAR(2),
room_name VARCHAR(32),
PRIMARY KEY (room_code, lang_code),
FOREIGN KEY (lang_code) REFERENCES languages (lang_code),
CHECK (LENGTH(lang_code) = 2),
CHECK (room_code <> '')
);
Then I have a table events with this definition:
CREATE TABLE events
(
room_code VARCHAR2(32) NOT NULL,
message_code VARCHAR(64) NOT NULL,
time_start DATETIME NOT NULL ,
time_end DATETIME NOT NULL,
PRIMARY KEY (message_code, room_code),
FOREIGN KEY (room_code) REFERENCES rooms (room_code),
-- Check the time is between 00:00 and 23:59
CHECK (time_start and time_end BETWEEN TIME('00:00') AND TIME('23:59')),
-- Start time must be lower than end time
CHECK (time_start < time_end),
-- Message code cannot be empty
CHECK (message_code <> '')
);
But in fact, I got this error in any attempt of insert:
Error: near line 91: foreign key mismatch - "events" referencing "rooms"
Why can I not reference another table? I read that all foreign keys of table B must be PRIMARY KEY in table A. But this dependency is OK for me. Foreign key room_code from table events it's also primary key room_code in table rooms.
Is that possible the solution of this instead of TRIGGERS? In fact, I want use rather KEYS instead of TRIGGERS if is it possible. Thanks
Best regards