0

Im trying to create a table in MySQL:

CREATE TABLE band ( 
    id int NOT NULL AUTO_INCREMENT, 
    name varchar(255) NOT NULL ,
    frontman int NOT NULL ,   
    PRIMARY KEY(id), 
    FOREIGN KEY(frontman) references person(id)  
)  

The new table will have a foreign key to this table:

CREATE TABLE person (
    id int NOT NULL AUTO_INCREMENT,
    nick varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    admin boolean,
    PRIMARY KEY(id)
)

and the error is :

#1005 - Can't create table 'apptestdb.band' (errno: 150)

Any ideas whats wrong? Thank you.

Perception
  • 79,279
  • 19
  • 185
  • 195
Wally_the_Walrus
  • 219
  • 1
  • 5
  • 17

3 Answers3

0

I got this from stackoverflow site Error 150, please refer it may help you to resolve the issue.

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.

Some Known causes may be :

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

Community
  • 1
  • 1
Abhishek Kumar
  • 229
  • 1
  • 5
  • 3
    You state that you got this at StackOverflow. Can you provide the reference to it? It would be preferable to reference the other post, instead of a direct copy/paste. – Taryn Mar 02 '13 at 14:22
0

you are making FOREIGN KEY to another table person so better share also this table .

IF you want just create this table band then remove FOREIGN KEY and it will work fine.

look here DEMO . your table works just fine .

EDIT. here your working tables with person table

 create table  person (
  id  int NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY(id))
  ;

 CREATE TABLE band ( 
 id int NOT NULL , 
 name varchar(255) NOT NULL ,
 frontman int NOT NULL ,   

 FOREIGN KEY(frontman) references person(id)
 ); 

working table demo

echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

I find finally the solution. It is simple. After each CREATE TABLE command has to be a semicolon to end the command. The way Ive run it tried to use the next CREATE TABLE command as a parameter of the first command.

Wally_the_Walrus
  • 219
  • 1
  • 5
  • 17