I'm trying to reference two seperate tables to create a third table, but SQL says there's a syntax error near FOREIGN
import sqlite3 as db
connection = db.connect("doctor_who_database")
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS doctor_who_database(
item_id TEXT PRIMARY KEY,
item TEXT)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS doctor_who_database2(
person_id TEXT PRIMARY KEY,
season TEXT,
person TEXT)""")
cursor.execute("""CREATE TABLE IF NOT EXISTS doctor_who_database3(
id TEXT PRIMARY KEY,
item TEXT,
season TEXT,
person TEXT)""")
cursor.execute("""FOREIGN KEY(item_id) REFERENCES doctor_who_database(item_id)""")
The fields in FOREIGN KEY
must be present in the table the FK is created upon, item_id
is not a field in doctor_who_database3
, also FOREIGN KEY
is not a command in itself, it should be put in the CREATE TABLE
, for example
cursor.execute("""CREATE TABLE IF NOT EXISTS doctor_who_database3(
id TEXT PRIMARY KEY,
item TEXT,
season TEXT,
person TEXT,
FOREIGN KEY(id) REFERENCES doctor_who_database(item_id)""")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.