Reputation:
I'm a student and I'm just trying out some basic mysql. However I keep getting the same error when it comes to the following piece of code.
Error Code: 1005. Can't create table 'mydatabase.orders' (errno: 150)
CREATE TABLE Customer
(
P_ID int(3) UNSIGNED NOT NULL,
LastName varchar(10) NOT NULL,
FirstName varchar(10) NOT NULL,
Address varchar(20) NOT NULL,
City varchar(10) NOT NULL
);
CREATE TABLE Orders
(
O_ID int(3) UNSIGNED NOT NULL,
OrderNo int NOT NULL,
P_ID int(3) UNSIGNED NOT NULL,
PRIMARY KEY (O_ID),
FOREIGN KEY(P_ID)REFERENCES Customer(P_ID)
);
Upvotes: 1
Views: 3280
Reputation: 64399
This:
FOREIGN KEY(P_ID)REFERENCES Customer(P_ID)
only works if you have
Adding an index would look like this:
INDEX(P_ID)
I wasn't able to try it out for your, so it is just "dry" coding here, but I'm pretty sure this is it. Take care of things like ,
placement obviously, but this should work.
Upvotes: 1
Reputation: 6114
Error Code: 1005 -- there is a wrong primary key reference in your code
usually it's due to a reference FK field not exist. might be you have typo mistake,or check case it should be same, or there's a field-type mismatch. FK-linked fields must match definitions exactly.
First Steps:
If you have admin permission on the server, you may want to start by running the MySQL command “SHOW INNODB STATUS” (or MySQL 5.5 “SHOW ENGINE INNODB STATUS”) immediately after receiving the error. This command displays log info and error details.
If your script runs fine on one server, but gives an error when you try to run it on a different server, then there is a good chance that #6 is the problem. Different versions of MySQL have different default charset setting and you may have unknowingly assigned different charsets on the different servers.
Some Known causes may be :
You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL. You can fix this by either changing your cascade or setting the field to allow NULL values.
Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns.
You have a default value (ie default=0) on your foreign key column
One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint.
You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship
10 The name of your foreign key exceeds the max length of 64 chars.
for more details refer : MySQL Error Number 1005 Can’t create table
Upvotes: 1
Reputation: 6534
This should work, there was no PRIMARY KEY
on the Customer table.
EDIT deleted the ON DELETE CASCADE ON UPDATE CASCADE
clause, maybe is not needed
CREATE TABLE Customer(
P_ID int(3) UNSIGNED NOT NULL,
LastName varchar(10) NOT NULL,
FirstName varchar(10) NOT NULL,
Address varchar(20) NOT NULL,
City varchar(10) NOT NULL,
PRIMARY KEY (`P_ID`)
);
CREATE TABLE Orders(
O_ID int(3) UNSIGNED NOT NULL,
OrderNo int(3) NOT NULL,
P_ID int(3) UNSIGNED NOT NULL,
PRIMARY KEY (O_ID),
CONSTRAINT `P_ID` FOREIGN KEY(`P_ID`) REFERENCES `Customer` (`P_ID`)
);
Upvotes: 0
Reputation: 2822
Run the following queries:
CREATE TABLE Customer
(
P_ID int(3) UNSIGNED NOT NULL,
LastName varchar(10) NOT NULL,
FirstName varchar(10) NOT NULL,
Address varchar(20) NOT NULL,
City varchar(10) NOT NULL,
PRIMARY KEY (P_ID)
);
CREATE TABLE Orders
(
O_ID int(3) UNSIGNED NOT NULL,
OrderNo int NOT NULL,
P_ID int(3) UNSIGNED NOT NULL,
PRIMARY KEY (O_ID),
FOREIGN KEY(P_ID)REFERENCES Customer(P_ID)
);
Before declaring P_ID as foreign key, it must be declared as primary key.
Upvotes: 0