I keep getting the "ERROR 1005 (HY000): Can't create table 'jfunchio.rental' (errno: 150)" error where i run this code. I was wondering if anyone could help with what is wrong. I would really appreciate it. My code is below.

create table rental
(  item_rental_id               varchar(8),
   customer_id                  varchar(8),
   movie_id                     varchar(8),
   game_id                      varchar(8),
   rental_status_code           varchar(10),
   rental_date_out              varchar(20),
   rental_date_returned         varchar(20),
   rental_amount_due            numeric(2,2),
   primary key (item_rental_id),
   foreign key (customer_id) references customer (customer_id)
        on delete set null,
   foreign key (movie_id) references movie (movie_id)
        on delete set null,
   foreign key (game_id) references game (game_id)
        on delete set null

)  ENGINE = InnoDB;

create table customer
(  customer_id                  varchar(8),
   first_name                   varchar(20) not null,
   last_name                    varchar(20) not null,
   phone                        varchar(20),
   email                        varchar(50),
   primary key (customer_id)
) ENGINE = InnoDB;

create table movie
(  movie_id             varchar(8),
   movie_genre          varchar(20),
   release_year         numeric(4,0),
   movie_title          varchar(50),
   movie_stock          numeric(2,1),
   movie_rental_rate    numeric(2,2),
   primary key (movie_id)
) ENGINE = InnoDB;

create table game
(  game_id              varchar(8),
   game_genre           varchar(20),
   release_year         numeric(4,0)
   game_title           varchar(50),
   game_stock           numeric(2,1),
   game_rental_rate     numeric(2,2),
   primary key (game_id)
) ENGINE = InnoDB;

create table payment
(  payment_id           varchar(8),
   customer_id          varchar(8),
   paymentMethod_id     varchar(20),
   paymentAmount        numeric(2,2),
   primary key (payment_id),
   foreign key (customer_id) references customer (customer_id)
        on delete set null
) ENGINE = InnoDB;

create table cast
(  movie_id             varchar(8),
   actor_id             varchar(8),
   primary key (movie_id)
) ENGINE = InnoDB;

create table actor
(  actor_id             varchar(8),
   actor_first_name     varchar(20) not null,
   actor_last_name      varchar(20),
   primary key (actor_id)
) ENGINE = InnoDB;

create table rental_status_code
(  rental_status_code           varchar(8),
   rental_status_descript       varchar(10),
   primary key (rental_status_code)
) ENGINE = InnoDB;

Dani AI

Generated

Quick answer: ERROR 1005 with “errno: 150” means InnoDB refused to create a foreign‑key constraint — the CREATE TABLE failed because a foreign key would be invalid. The MySQL manual explains the problem and why you should run the InnoDB status output for details. MySQL: FOREIGN KEY constraints. To see the precise failure message immediately after the error run:

SHOW ENGINE INNODB STATUS\G

That output usually tells you exactly which constraint failed and why (mismatched types, missing index, engine/charset mismatch, referenced table not found, etc.). SHOW ENGINE INNODB STATUS. See also common troubleshooting examples. (StackOverflow threads show typical cases and the helpful STATUS output.)
https://stackoverflow.com/questions/8960418/foreign-key-between-mysql-innodb-tables-not-working-why

Practical checklist to fix the script:

  • Create parent (referenced) tables before child tables that declare FKs, or add FK constraints afterward with ALTER TABLE.
  • Verify both sides use the same storage engine and charset, and that the referenced column has the same data type, length, signed/unsigned and collation. Use SHOW CREATE TABLE <name>\G to compare.
  • Make sure the referenced column is indexed (PRIMARY/UNIQUE is fine).
  • If you used ON DELETE SET NULL, ensure the FK column allows NULL. The manual notes SET NULL requires nullable child columns. FOREIGN KEY reference details

Notes specific to the posted script: rental was being created before the tables it references — that alone often produces errno 150. There’s also a syntax issue in the posted game DDL (missing comma) and some NUMERIC declarations that look too small for money; in MySQL NUMERIC is DECIMAL — use a sensible precision like DECIMAL(8,2) for currency. DECIMAL / NUMERIC docs

Quick remediation steps

  1. Fix syntax errors.
  2. Create parent tables first (or create FKs with ALTER TABLE after parents exist).
  3. Run SHOW ENGINE INNODB STATUS\G if it still fails and correct the specific complaint.
    If you must load out of order for a dump, you can temporarily disable checks with SET FOREIGN_KEY_CHECKS=0; and re-enable after, but do that cautiously (see docs). foreign_key_checks guidance

References cited above give the exact rules and error diagnostics.

Is your search engine broken?

:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to error 150, that means a foreign key definition would be incorrectly formed for the altered table. You can use SHOW ENGINE INNODB STATUS to display a detailed explanation of the most recent InnoDB foreign key error in the server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.