问题
Question
I am trying to link the ID column from the Customer's table to C_ID in the Purchases table. I am still learning SQL so I have a light knowledge and am not sure why this error is occuring. If someone could offer up a solution and point where I've went wrong that would be great.
Error Message
SQL Code
CREATE TABLE Customer (
ID INTEGER,
Firstname VARCHAR (15),
Lastname VARCHAR (15),
Address VARCHAR (254),
Postcode VARCHAR (8),
Email VARCHAR (254),
Phoneno INTEGER,
Points INTEGER,
PRIMARY KEY (ID)
);
CREATE TABLE Purchases (
C_ID INTEGER,
GameName VARCHAR(30),
ConsoleType VARCHAR (20),
Price VARCHAR (254),
PaymentType VARCHAR (20),
Date TIMESTAMP,
PointsGained INTEGER,
PRIMARY KEY (C_ID),
FOREIGN KEY (ID) REFERENCES Customer(ID)
);
回答1:
I think you misplaced the primary key and foreign key column also you didn't add Purchase table primary key column
CREATE TABLE Purchases
(
ID INTEGER, -- Primary key column
C_ID INTEGER,
GameName VARCHAR(30),
ConsoleType VARCHAR (20),
Price VARCHAR (254),
PaymentType VARCHAR (20),
Date TIMESTAMP,
PointsGained INTEGER,
PRIMARY KEY (ID),
FOREIGN KEY (C_ID) -- Replace ID with C_ID
REFERENCES Customer(ID)
);
回答2:
You don't have a column named ID
in your Purchases
table.
Seems to me it should be like this:
CREATE TABLE Purchases (
ID INTEGER,
C_ID INTEGER,
GameName VARCHAR(30),
ConsoleType VARCHAR (20),
Price VARCHAR (254),
PaymentType VARCHAR (20),
Date TIMESTAMP,
PointsGained INTEGER,
PRIMARY KEY (ID),
FOREIGN KEY (C_ID) REFERENCES Customer(ID)
);
So that the ID
column is the primary key and the C_ID
column is the foreign key to customers.
来源:https://stackoverflow.com/questions/36288573/error-column-doesnt-exist-in-table-foreign-key-referencing