我试图在sqlite3中创建一堆表,我收到一个我无法解决的错误。我的sqlite3语法与外键有关,但无法弄明白。
CREATE TABLE students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(64),
last_name VARCHAR(64)
);
CREATE TABLE classes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
class_name VARCHAR(64)
);
CREATE TABLE students_classes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
students_id INTEGER,
classes_id INTEGER,
FOREIGN KEY (students_id) REFERENCES students(id),
FOREIGN KEY (classes_id) REFERENCES classes(id)
);
CREATE TABLE teachers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
classes_id INTEGER,
first_name VARCHAR(64),
last_name VARCHAR(64),
FOREIGN KEY classes_id REFERENCES classes(id)
);
CREATE TABLE grades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
students_id INTEGER,
grade_num INTEGER,
FOREIGN KEY students_id REFERENCES students(id)
);
Error: near "students_id": syntax error
答案 0 :(得分:0)
SQLite Docs似乎表明FOREIGN Key
约束中的列名必须在括号内:
我不确定实际执行的严格程度。我的SQL Fiddle表明使用括号可以解决您的问题。
请注意,SQL Fiddle
的{{1}} 已模拟,因此您的搜索结果可能会有所不同。