MySQL 1005 Error : SQLSTATE: HY000 (ER_CANT_CREATE_TABLE) Message: Can't create table '%s' (errno: 150)


Error No : 1005
Error Type : Server Error
Error Code : 1005 SQLSTATE: HY000 (ER_CANT_CREATE_TABLE)
Error Message : Can't create table '%s' (errno: %d)
This error may occur when you are trying to create a table and it fails. 
There are many reasons that may cause this issue. 
One of the reason is related to Foreign Key constraint that is not formed correctly (Error code 150).
MySQL QueryLanguage: SQL
CREATE TABLE `prog_lang` (
`lang_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`lang_name` VARCHAR(100) NOT NULL,
    `lang_desc` VARCHAR(255) NOT NULL,
    `active_stat` CHAR(1)
);
CREATE TABLE `topic` (
    `topic_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `topic_desc` TEXT NOT NULL,
    `lang_id` INT NOT NULL,
    CONSTRAINT `fk_prog_lang` FOREIGN KEY(`lang_id`) REFERENCES `prog_lang`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Make sure that Engine and CHARSET of the tables match: ENGINE=InnoDB DEFAULT CHARSET=utf8.




Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.