Syntax Error near ScanID

不打扰是莪最后的温柔 提交于 2019-12-02 08:30:34

问题


Evening all

I have been working on a small application but kind of stuck at the SQLite foreign key constraint. Basically what i have is one "HostLookuptable" as

CREATE TABLE tblHostLookup ( 
HostID INTEGER PRIMARY KEY AUTOINCREMENT, 
HostName TEXT);

And one "ScanLookuptable" as

CREATE TABLE tblScanLookup ( 
ScanID INTEGER PRIMARY KEY AUTOINCREMENT, 
ScanDate TEXT);

Then there is another table that will have mapping between two tables as "ScanHistorytable"

CREATE TABLE tblScanHistory (
ScanHistoryID INTEGER PRIMARY KEY AUTOINCREMENT,
HostID INTEGER,
FOREIGN KEY(HostID) REFERENCES tblHostLookup(HostID),
ScanID INTEGER,
FOREIGN KEY(ScanID) REFERENCES tblScanLookup(ScanID));

But i keep getting an error saying

Syntax error near ScanID

Why so? Are we not allowed to have more than one foreign key in a table? Any help in this regard would be great.

Thanks


回答1:


You cannot mix table columns and table constraints; the constraints must be listed after all the columns:

CREATE TABLE tblScanHistory (
    ScanHistoryID INTEGER PRIMARY KEY AUTOINCREMENT,
    HostID INTEGER,
    ScanID INTEGER,
    FOREIGN KEY(HostID) REFERENCES tblHostLookup(HostID),
    FOREIGN KEY(ScanID) REFERENCES tblScanLookup(ScanID)
);

Or, simpler:

CREATE TABLE tblScanHistory (
    ScanHistoryID INTEGER PRIMARY KEY AUTOINCREMENT,
    HostID INTEGER REFERENCES tblHostLookup(HostID),
    ScanID INTEGER REFERENCES tblScanLookup(ScanID)
);


来源:https://stackoverflow.com/questions/17817077/syntax-error-near-scanid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!