Question

I am writing a database in MySQL for my online portfolio website to catalog all my designs/pieces of work I have done for clients / projects and I am doing this in order to make adding new works easier instead of having to edit the mark up manually.

Anyway, I tried running the script below:

create table album (
   AlbumID int not null,
   AlbumOrder int not null,
   AlbumName varchar(100),
   primary key (AlbumID, AlbumOrder)
);

create table category (
   CategoryID int not null primary key auto_increment,
   CategoryName varchar(100)
);

create table item (
   ItemID int not null primary key auto_increment,
   CategoryID int not null,
   AlbumID int not null,
   AlbumOrder int not null,
   ItemName varchar(100),
   Description varchar(500),
   ThumbPath varchar(100),
   PhotoPath varchar(100),
   InsertDate datetime,
   EditDate datetime,
   constraint fk_catID foreign key (CategoryID) references category (CategoryID) on update cascade,
   constraint fk_albID foreign key (AlbumID) references album (AlbumID) on update cascade,
   constraint fk_albOrd foreign key (AlbumOrder) references album (AlbumOrder) on update cascade
);

I get the 1005 error saying the item table can't be created. I have no idea where the problem lies but I am sure it's really obvious!

EDIT: the engine used is innoDB.

No correct solution

OTHER TIPS

It looks like your fks to Album may be causing the issue. You should have a single FK based on the 2 columns rather than 2 FKs based on 1 column each, like so (removing the fk_albOrd line):

constraint fk_albID foreign key (AlbumID,AlbumOrder) references album (AlbumID,AlbumOrder) on update cascade

This script creates the tables without error in SQL Fiddle:

create table album (
   AlbumID int not null,
   AlbumOrder int not null,
   AlbumName varchar(100),
   primary key (AlbumID, AlbumOrder)
);

create table category (
   CategoryID int not null primary key auto_increment,
   CategoryName varchar(100)
);

create table item (
   ItemID int not null primary key auto_increment,
   CategoryID int not null,
   AlbumID int not null,
   AlbumOrder int not null,
   ItemName varchar(100),
   Description varchar(500),
   ThumbPath varchar(100),
   PhotoPath varchar(100),
   InsertDate datetime,
   EditDate datetime,
   constraint fk_catID foreign key (CategoryID) references category (CategoryID) on update cascade,
   constraint fk_albID foreign key (AlbumID,AlbumOrder) references album (AlbumID,AlbumOrder) on update cascade

);

There is nothing wrong with your statements as they exist right now.

I just executed them successfully on my test mysql environment. However, my environment might be different from yours: my default engine is InnoDB, yours might be something else.

Since you are using Foreign Key Constraints, you should specify explicitly that the engine is InnoDB ("...) engine = Innodb").

It might also be because you already have one or more tables existing. Precede each create with a DROP... IF EXISTS and it should work fine.

By running the MySQL statement show engine innodb status immediately after receiving the error, you can retrieve further details on the error (output shortened):

LATEST FOREIGN KEY ERROR

Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.

In the referenced table, there must be an index where the referenced column is listed as the first column. This is not valid for fk_albOrd foreign key (AlbumOrder).

To solve the issue, you can either add an index to the column AlbumOrder on the Album table or – preferably – get rid of fk_albOrd and change the fk_albID constraint to a group of columns as suggested by Derek Kromm:

constraint fk_albID foreign key (AlbumID, AlbumOrder) references album (AlbumID, AlbumOrder) on update cascade
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top