简体   繁体   中英

MySQL Error Number 150 when creating Table with Foreign Key

I am having an issue creating a new table in my database . I've seen that the error code it is returning is to do with Foreign Key constraints.

I checked to ensure that the data type of the foreign key in the new table matched the data type of the primary key in the other table. They are both int(11) .

However I am still getting an error. Am I missing something? This is my SQL script for creating the new table:

CREATE TABLE `regular_features` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(200) DEFAULT NULL,
  `day` VARCHAR(200) DEFAULT NULL,
  `description` TEXT DEFAULT NULL,
  `programme_id` INT(11) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`programme_id`) REFERENCES directoryprogramme(id) 
) ENGINE=INNODB DEFAULT CHARSET=utf8;

This is the original table containing the primary key :

CREATE TABLE `directoryprogramme` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(250) NOT NULL,
  `broadcast_time` VARCHAR(100) NOT NULL,
  `description` TEXT NOT NULL,
  `days` VARCHAR(150) NOT NULL,
  `contributors` VARCHAR(250) NOT NULL,
  `directorycompany_id` INT(11) NOT NULL,
  `directorycontact_id` VARCHAR(250) NOT NULL,
  `facebook_link` VARCHAR(250) DEFAULT NULL,
  `twitter_link` VARCHAR(250) DEFAULT NULL,
  `wikipedia_link` VARCHAR(250) DEFAULT NULL,
  `web` VARCHAR(250) DEFAULT NULL,
  `imageextension` VARCHAR(10) DEFAULT NULL,
  `type` VARCHAR(20) NOT NULL DEFAULT 'other',
  PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1161 DEFAULT CHARSET=utf8;

The Foreign Key will be the id of directoryprogramme

The problem is the last line of your create statement:

ENGINE=INNODB DEFAULT CHARSET=utf8;

You mix MYISAM in ald table with INNODB in your new table.

That doesn't work.

Chnage the engine in your new table to MYISAM and it works.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM