Error: Column doesn't exist in table (Foreign Key referencing)

和自甴很熟 提交于 2019-12-14 03:26:17

问题


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

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