I find innodb quite annoying when I try to design a db structure, at least compared with MyIsam, which seems to have less limitations
Say, if I want to create a simple library system. And I have four tables.
1,table book_item
, which records the book_name, author, publish time and those basic information about books
2, table book
, which represents a specific real object of the book item. So a book_item object can relate to many book objects.
3, table tag
, which represents a book tag. Like science, literature, architecture and so on.
4, table tag_book_item_relation
, which relates tags to book_items.
So, the relations are as below.
1,we have a book item to book is one-to-many relationship
2,book_item to tag is many-to-many relationship.
Note here, engine for the table are all innodb If I try to create the tables, it will fail:
Error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'yet_another_test.book' (errno: 121)
However, if I change the engine of book
or tag_book_item_relation
to MyISAM, everything will be fine.
So, I am wondering what is going wrong if I use engine innodb for tablebook
and tag_book_item_relation
The sql script is here(forward engineering in MySQL workbench):
CREATE TABLE IF NOT EXISTS `yet_another_test`.`tag` (
`id` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `yet_another_test`.`book_item` (
`id` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `yet_another_test`.`tag_book_item_relation` (
`book_item_id` INT NOT NULL ,
`tag_id` INT NOT NULL ,
PRIMARY KEY (`book_item_id`, `tag_id`) ,
INDEX `fk_tag` (`tag_id` ASC) ,
INDEX `fk_book_item` (`book_item_id` ASC) ,
CONSTRAINT `fk_tag`
FOREIGN KEY (`tag_id` )
REFERENCES `yet_another_test`.`tag` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_item`
FOREIGN KEY (`book_item_id` )
REFERENCES `yet_another_test`.`book_item` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `yet_another_test`.`book` (
`id` INT NOT NULL AUTO_INCREMENT ,
`book_item_id` INT NOT NULL ,
PRIMARY KEY (`id`, `book_item_id`) ,
INDEX `fk_book_item` (`book_item_id` ASC) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
CONSTRAINT `fk_book_item`
FOREIGN KEY (`book_item_id` )
REFERENCES `yet_another_test`.`book_item` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;