I am trying to create this table in my php file:
mysql_query("CREATE TABLE Orders (orderID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
FOREIGN KEY (userID) REFERENCES Users (userID))");
However, whenever this query runs it doesn't create the table.
Am I creating the foreign key wrong?
Echo result of mysql_error()
after the query and you will see why the query is failing.
mysql_query("CREATE TABLE Orders (
orderID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
FOREIGN KEY (userID) REFERENCES Users (userID)
)") or die(mysql_error());
In your case, there is no column userID
, and you are trying to create a foreign key on that column. You have to define the column first. Make sure it has exact the same data type as in Users
table
mysql_query("CREATE TABLE Orders (
orderID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
userID INT,
FOREIGN KEY (userID) REFERENCES Users (userID)
)") or die(mysql_error());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.