Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey guys I need your help creating these SQL's, I don't know why I am running into this trouble. it creates the first 2 tables, but when it gets to the Driver table, it throws a fit
Error:


MySQL said: Documentation
#1072 - Key column 'UserID' doesn't exist in table

SQL
CREATE TABLE User (
UserID int(11) NOT NULL AUTO_INCREMENT,
FirstName varchar(50) default NULL,
LastName varchar(50) default NULL,
Email varchar(50) default NULL,
Password varchar(50)default NULL,
PRIMARY KEY (UserID)
)ENGINE=INNODB;


CREATE TABLE Car (
CarID int(11) NOT NULL AUTO_INCREMENT,
Make varchar(50) default NULL,
Year int(11) default NULL,
Color varchar(50) default NULL,
PRIMARY KEY (CarID)
)ENGINE=INNODB;

CREATE TABLE Driver (
DriverID int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (DriverID),
FOREIGN KEY (UserID) REFERENCES User (UserID),
FOREIGN KEY (CarID) REFERENCES Car (CarID)
)ENGINE=INNODB;

CREATE TABLE Request (
RequestID int(11) NOT NULL AUTO_INCREMENT,
From varchar(50) default NULL,
To varchar(50) default NULL,
RequestDate Date default NULL,
PRIMARY KEY (RequestID),
FOREIGN KEY (UserID) REFERENCES User (UserID),
FOREIGN KEY (CarID) REFERENCES Car (CarID)
)ENGINE=INNODB;



Thanks for your help.
Posted
Updated 9-Jun-23 1:41am

You are trying to set up a Foreign key on a column that you have not yet created. Try this ...
SQL
CREATE TABLE Driver (
DriverID int(11) NOT NULL AUTO_INCREMENT,
UserID int,
CarID int,
PRIMARY KEY (DriverID),
FOREIGN KEY (UserID) REFERENCES User (UserID),
FOREIGN KEY (CarID) REFERENCES Car (CarID)
)ENGINE=INNODB;
 
Share this answer
 
you just write id in the primary key not write userid, carid
 
Share this answer
 
Comments
Richard Deeming 2-Jul-21 4:30am    
This question was solved nearly eight years ago, and you have added nothing new to the discussion.
CHill60 2-Jul-21 5:38am    
Completely inaccurate. The problem was because the OP was trying to use non-existing columns as FOREIGN keys to another table. There was no problem with their aptly named primary key at all
you just write id int in primary number you change the #id as #userid then work by:-shivam mandal
 
Share this answer
 
Comments
Richard Deeming 9-Jun-23 7:47am    
The question was solved ten years ago, and your "solution" is virtually identical to solution 3 - which was already not a good solution to the question.

Stick to answering new questions unless you have something new and interesting to add to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900