CREATE TABLE School ( ID INTEGER PRIMARY KEY , Name TEXT NOT NULL
); CREATE TABLE events ( ID INTEGER PRIMARY KEY , details TEXT NOT NULL, start_data TEXT NOT NULL, end_data TEXT NOT NULL, title TEXT NOT NULL, FOREIGN KEY fk_events_ID REFERENCES School(ID)
);
CREATE TABLE CALENDAR ( ID INTEGER PRIMARY KEY, title TEXT NOT NULL, details TEXT NOT NULL, start_data TEXT NOT NULL, end_data TEXT NOT NULL, subject INTEGER NOT NULL, FOREIGN KEY fk_CALENDAR_ID REFERENCES School(ID)
);
CREATE TABLE GRADES ( ID INTEGER PRIMARY KEY , title TEXT NOT NULL, coefficent REAL NOT NULL, value REAL NOT NULL, date_time TEXT NOT NULL, Subject INTEGER NOT NULL, FOREIGN KEY fk_GRADES_ID REFERENCES School(ID)
CodePudding user response:
In foreign key syntax, you must name the column(s) in the table that are the foreign key. You also need to define a column first, so you can define the data type or other options for that column.
WRONG:
FOREIGN KEY fk_events_ID REFERENCES School(ID)
RIGHT:
school_id INTEGER,
FOREIGN KEY fk_events_ID (school_id) REFERENCES School(ID)
Read https://sqlite.org/foreignkeys.html for more information about foreign keys in SQLite.
CodePudding user response:
In foreign keys you need brackets and it is a good habit to specify everything that could go wrong with constraints, so you know exactly what was the problem In many cases, the manuals could help you in the first place. https://www.sqlite.org/docs.html
ID INTEGER PRIMARY KEY,
Name TEXT NOT NULL
);
CREATE TABLE events (
ID INTEGER PRIMARY KEY,
details TEXT NOT NULL,
start_data TEXT NOT NULL,
end_data TEXT NOT NULL,
title TEXT NOT NULL,
CONSTRAINT fk_events FOREIGN KEY (fk_events_ID) REFERENCES School(ID)
);
CREATE TABLE CALENDAR (
ID INTEGER PRIMARY KEY,
title TEXT NOT NULL,
details TEXT NOT NULL,
start_data TEXT NOT NULL,
end_data TEXT NOT NULL,
subject INTEGER NOT NULL,
CONSTRAINT fk_calendar FOREIGN KEY (fk_CALENDAR_ID) REFERENCES School(ID)
);
CREATE TABLE GRADES (
ID INTEGER PRIMARY KEY,
title TEXT NOT NULL,
coefficent REAL NOT NULL,
value REAL NOT NULL,
date_time TEXT NOT NULL,
Subject INTEGER NOT NULL,
CONSTRAINT fk_grades FOREIGN KEY (fk_GRADES_ID) REFERENCES School(ID)
);```