0

Im trying to create a table programmatically in sqlite. But it gives me an syntacs error when im trying to create a foreign key constraint. Can anybody tell me why.

this is my code:

CREATE TABLE tblUser (ID INT64 Primary Key, Name NVARCHAR(100) NOT NULL, EMail
NVARCHAR(100) NOT NULL); 

CREATE TABLE tblMachine (ID INT64 Primary Key, Name NVARCHAR(100) NOT NULL,
ProcessorID NVARCHAR(100) NOT NULL, BiosID NVARCHAR(100) NOT NULL);

CREATE TABLE tblLicenseInfo (ID INT64 Primary Key, UserID INT64 NOT NULL,
MachineID INT64 NOT NULL, ExpirationDate DATETIME NOT NULL, DateOfChange DATETIME
NOT NULL, LicenseKey NVARCHAR(100) NOT NULL),

FOREIGN KEY (UserID) REFERENCES tblUser.ID,
FOREIGN KEY (MachineID) REFERENCES tblMachine.ID;  

thank you very much.

2 Answers 2

1

You need to include the FOREIGN KEY clauses within the parenthesis of the CREATE TABLE commands. Have a look at the CREATE TABLE syntax diagram, where a FOREIGN KEY clause is a table constraint.

Moreover, the referenced column(s) of the other table need(s) to be indicated in parenthesis, not with the dot notation. This is described in the syntax diagram for the FOREIGN KEY clause.

Hence, your CREATE TABLE statement should look like this:

CREATE TABLE tblLicenseInfo (
    ID INT64 PRIMARY KEY,
    UserID INT64 NOT NULL,
    MachineID INT674 NOT NULL,
    ExpirationDate DATETIME NOT NULL,
    DateOfChange DATETIME NOT NULL,
    LicenseKey NVARCHAR(100) NOT NULL,
    FOREIGN KEY (UserID) REFERENCES tblUser (ID),
    FOREIGN KEY (MachineID) REFERENCES tblMachine (ID)
);
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much, but it still says "near "tblUser": syntax error" any ideas?
@user3292642: Right now, I'm not seeing any further problem after comparing with the syntax diagrams step by step. To be clear, are you trying to run the above commands (from your question) one by one?
had a spelling mistake, its working fine now, thanks
0

you need an option to do it : in php try

$db = new SQLite3("../SQLLite/test.db");
$db->exec('PRAGMA foreign_keys = ON');

there are other way in commande for exemple

sqlite> PRAGMA foreign_keys = ON;

source : https://www.sqlite.org/foreignkeys.html

3 Comments

Im using visual studio and c#, do i need to set some property to true or on aswell?
@user3292642 i'm the worst guy of c# in the world, can't say, sorry
okay you dont have to, at least its working now for me and i didnt do something like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.