How can the error #1005 "Can't create table 'menusubtitle' (errno: 150)" be resolved when creating foreign keys in MySQL?
Error #1005 in MySQL occurs when there is a foreign key constraint issue, such as mismatched data types or column definitions between the parent and child tables. To resolve this error, ensure that the data types and column definitions of the foreign key columns in the child table match exactly with those in the parent table.
CREATE TABLE `menu` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `menusubtitle` (
`id` INT NOT NULL AUTO_INCREMENT,
`menu_id` INT NOT NULL,
`subtitle` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`menu_id`) REFERENCES `menu`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
Keywords
Related Questions
- What are the potential security risks associated with using $_REQUEST in PHP scripts?
- What are the common reasons for encountering a white screen in PHP scripts, and how can developers troubleshoot and resolve this issue effectively?
- What is the best practice for structuring SQL queries in PHP functions for updating database records?