Question

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)
);
Was it helpful?

Solution

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`)
);

OTHER TIPS

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 :

  1. The two key fields type and/or size doesn’t match exactly. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same.
  2. One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field.
  3. The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this.
  4. One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message - it just won’t create the key.) In Query Browser, you can specify the table type.
  5. 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.

  6. 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.

  7. You have a default value (ie default=0) on your foreign key column

  8. 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.

  9. 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

This:

FOREIGN KEY(P_ID)REFERENCES Customer(P_ID)

only works if you have

  1. an index on that Customer(P_ID) field, so you should add one
  2. use innodb -> if that is not your default engine, you should add that engine specifically.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top