Deep Analysis and Solutions for MySQL Error Code 1005: Can't Create Table (errno: 150)

Dec 02, 2025 · Programming · 13 views · 7.8

Keywords: MySQL | Error Code 1005 | Foreign Key Constraints

Abstract: This article provides an in-depth exploration of MySQL Error Code 1005 (Can't create table, errno: 150), a common issue encountered when creating foreign key constraints. Based on high-scoring answers from Stack Overflow, it systematically analyzes multiple causes, including data type mismatches, missing indexes, storage engine incompatibility, and cascade operation conflicts. Through detailed code examples and step-by-step troubleshooting guides, it helps developers understand the workings of foreign key constraints and offers practical solutions to ensure database integrity and consistency.

In MySQL database development, creating foreign key constraints is crucial for ensuring data integrity. However, many developers may encounter Error Code 1005 with errno 150 when executing ALTER TABLE statements, indicating an inability to create a table. This article, based on high-quality technical community discussions, delves into the root causes of this common error and provides systematic solutions.

Common Scenarios for Error Code 1005

Error Code 1005 typically occurs during foreign key constraint creation, as in the following SQL statement:

ALTER TABLE sira_no
    ADD CONSTRAINT METAL_KODU FOREIGN KEY(METAL_KODU)
    REFERENCES metal_kod(METAL_KODU)
    ON DELETE SET NULL
    ON UPDATE SET NULL ;

This statement attempts to create a foreign key on the METAL_KODU column of the sira_no table, referencing the METAL_KODU column of the metal_kod table. If execution fails, MySQL returns an error message: Error Code: 1005. Can't create table 'ebs.#sql-f48_1a3' (errno: 150). The errno 150 further specifies the reason for the foreign key constraint failure.

Root Cause Analysis

Based on in-depth technical community discussions, Error Code 1005 can stem from various factors. Below is a detailed analysis of the primary causes:

  1. Data Type Mismatch: The data types of the foreign key column and the referenced column must match exactly. For example, if the METAL_KODU column in the metal_kod table is VARCHAR(4), the corresponding column in the sira_no table must also be VARCHAR(4), not CHAR(4) or other types. Additionally, signed attributes for numeric types (e.g., SIGNED vs. UNSIGNED) must align.
  2. Missing Index: The referenced column (i.e., the METAL_KODU column in the metal_kod table) must have an index. If it is not a primary key, an explicit index must be created. For example:
    CREATE INDEX METAL_KODU_INDEX ON metal_kod (METAL_KODU);
    In some cases, even if the column is part of a composite key, a separate index may be required.
  3. Storage Engine Incompatibility: In MySQL, only the InnoDB storage engine supports foreign key constraints. If related tables use the MyISAM engine, foreign key creation will fail. This can be checked and modified with:
    SHOW TABLE STATUS LIKE 'table_name';
    ALTER TABLE table_name ENGINE=InnoDB;
  4. Cascade Operation Conflict: If the foreign key column is defined as NOT NULL, but cascade operations are set to ON DELETE SET NULL or ON UPDATE SET NULL, a conflict arises. Solutions include modifying the column to allow NULL values or adjusting the cascade operations.
  5. Charset and Collation Inconsistency: The charset and collation of the foreign key and referenced columns must be identical. Consistency is required not only at the column level but also potentially at the table level.
  6. Syntax or Typographical Errors: Carefully review table names, column names, and constraint names in the SQL statement to ensure no typos. Foreign key names must be unique within the database to avoid conflicts with existing keys.
  7. Default Value Issues: If the foreign key column has a default value (e.g., DEFAULT 0), it may conflict with the reference constraint, especially if the referenced column does not permit that value.

Solutions and Best Practices

To address the above causes, follow these steps for troubleshooting and resolution:

  1. Verify Data Types and Attributes: Use DESCRIBE table_name or SHOW CREATE TABLE table_name commands to compare the definitions of foreign key and referenced columns.
  2. Ensure Index Existence: Confirm that the referenced column has an appropriate index. For columns in composite keys, consider creating a separate index.
  3. Check Storage Engine: Ensure all related tables use the InnoDB engine.
  4. Adjust Cascade Operations: Set cascade operations appropriately based on business needs to avoid conflicts with column definitions.
  5. Unify Charset and Collation: Maintain consistency in charset and collation at the database, table, and column levels.
  6. Test Foreign Key Creation: Gradually test foreign key creation in a development environment, using tools like MySQL Workbench for debugging.

Through systematic troubleshooting, most Error Code 1005 issues can be resolved. Understanding the underlying mechanisms of foreign key constraints aids in designing more robust database architectures.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.