我尝试创建两个表car_main和car_info。car_main有一个主键和一个外键,分别是car_id和model_id。car_info有两个外键,car_id和model_id。
connection = sqlite3.connect("TutorialDB.db")
crsr = connection.cursor()
sql_command = """CREATE TABLE car_main (
car_id INTEGER PRIMARY KEY,
model_id INTEGER FOREIGN KEY REFERENCES car_main(car_id),
);"""
crsr.execute(sql_command)
sql_command = """CREATE TABLE car_info (
car_id INTEGER FOREIGN KEY REFERENCES car_main(car_id),
model_id INTEGER FOREIGN KEY REFERENCES car_main(model_id),
year INTEGER,
price INTEGER,
used BOOLEAN
);"""
crsr.execute(sql_command)
connection.close()```
1条答案
按热度按时间r7knjye21#
外键语句没有意义,因为您引用的是同一个表
也许你需要