Wishaal Khan
Wishaal Khan

Reputation: 135

Create table query

I am simply trying to make a table. The code I have is:

{
    Connection.Open();
    OleDbCommand Command = new OleDbCommand();
    Command.Connection = Connection;

    Command.CommandText = @"CREATE TABLE Login
    (
       Login_ID int NOT NULL PRIMARY KEY,
       ID int  FOREIGN KEY REFERENCES Stud_Rec(ID)
    )";

    Command.ExecuteNonQuery();
    Connection.Close();
}

I get the following error:

Syntax error in CONSTRAINT clause

I believe my syntax should be all correct, and don't know what's wrong with my code. Any help would be appreciated.

Upvotes: 2

Views: 684

Answers (2)

Wishaal Khan
Wishaal Khan

Reputation: 135

I have solved it as;

CREATE TABLE Login
(
     Login_ID int NOT NULL PRIMARY KEY,
     ID int,
     FOREIGN KEY (ID) REFERENCES Stud_Rec(ID)
)

Upvotes: 0

juergen d
juergen d

Reputation: 204924

CREATE TABLE Login
(
     Login_ID int NOT NULL PRIMARY KEY,
     ID int,
     FOREIGN KEY (ID) REFERENCES Stud_Rec(ID)
)

You missed a comma before foreign and did not name the foreign key.

If you use a SQL tool to highlight the errors in your SQL code you find it way easier.

Upvotes: 2

Related Questions