E
E
Etmac2016-01-08 14:29:12
MySQL
Etmac, 2016-01-08 14:29:12

Can't create table, error 150?

I read about this error, however, I can not understand how to properly link the cities and countries tables. The countries table has fields id (PRIMARY KEY) and name (data type varchar(64) ). Essence of a question: how correctly to set FK that it referred to citites? And why is the error displayed?
CREATE TABLE cities (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL,
country_id varchar(64),
UNIQUE KEY(country_id),
FOREIGN KEY(country_id) REFERENCES countries(name)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ERROR 1005 (HY000): Can't create table 'network.cities' (errno: 150)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Sukherev, 2016-01-08
@Etmac

put away

country_id varchar(64),
UNIQUE KEY(country_id),

change
FOREIGN KEY(country_id) REFERENCES countries(id)

P
Peter, 2016-01-08
@petermzg

There is an error in this line, it looks like different data types
FOREIGN KEY(country_id) REFERENCES countries(name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question