和关系是 1:n,其中book->type
和是可选的。这很容易用 MySQL Workbench 创建。book->grade
type
grade
这应该这样做:
-- -----------------------------------------------------
-- Table `type`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `type` (
`id` INT(10) NOT NULL ,
`name` VARCHAR(256) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `grade`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `grade` (
`id` INT(10) NOT NULL ,
`name` VARCHAR(256) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `book`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `book` (
`id` INT(10) NOT NULL ,
`title` VARCHAR(256) NOT NULL ,
`type_id` INT(10) NULL ,
`publication_date` DATE NULL ,
`value` DECIMAL(10,2) NULL ,
`price` DECIMAL(10,2) NULL ,
`notes` TEXT NULL ,
`signed` TINYINT(1) NULL ,
`grade_id` INT(10) NULL ,
`bagged` TINYINT(1) NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_book_grade` (`grade_id` ASC) ,
INDEX `fk_book_type1` (`type_id` ASC) ,
CONSTRAINT `fk_book_grade`
FOREIGN KEY (`grade_id` )
REFERENCES `grade` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_book_type1`
FOREIGN KEY (`type_id` )
REFERENCES `type` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tag`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tag` (
`id` INT(10) NOT NULL ,
`value` VARCHAR(64) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `booktag`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `booktag` (
`book_id` INT(10) NOT NULL ,
`tag_id` INT(10) NOT NULL ,
INDEX `fk_booktag_book1` (`book_id` ASC) ,
INDEX `fk_booktag_tag1` (`tag_id` ASC) ,
PRIMARY KEY (`book_id`, `tag_id`) ,
CONSTRAINT `fk_booktag_book1`
FOREIGN KEY (`book_id` )
REFERENCES `book` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_booktag_tag1`
FOREIGN KEY (`tag_id` )
REFERENCES `tag` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `person`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `person` (
`id` INT(10) NOT NULL ,
`fname` VARCHAR(64) NOT NULL ,
`lname` VARCHAR(64) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `bookillustrator`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bookillustrator` (
`book_id` INT(10) NOT NULL ,
`person_id` INT(10) NOT NULL ,
INDEX `fk_bookillustrator_person1` (`person_id` ASC) ,
INDEX `fk_bookillustrator_book1` (`book_id` ASC) ,
PRIMARY KEY (`book_id`, `person_id`) ,
CONSTRAINT `fk_bookillustrator_person1`
FOREIGN KEY (`person_id` )
REFERENCES `person` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bookillustrator_book1`
FOREIGN KEY (`book_id` )
REFERENCES `book` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `bookauthor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bookauthor` (
`book_id` INT(10) NOT NULL ,
`person_id` INT(10) NOT NULL ,
INDEX `fk_bookauthor_person1` (`person_id` ASC) ,
INDEX `fk_bookauthor_book1` (`book_id` ASC) ,
PRIMARY KEY (`book_id`, `person_id`) ,
CONSTRAINT `fk_bookauthor_person1`
FOREIGN KEY (`person_id` )
REFERENCES `person` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bookauthor_book1`
FOREIGN KEY (`book_id` )
REFERENCES `book` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `publisher`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `publisher` (
`id` INT(10) NOT NULL ,
`name` VARCHAR(64) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `bookpublisher`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `bookpublisher` (
`book_id` INT(10) NOT NULL ,
`publisher_id` INT(10) NOT NULL ,
INDEX `fk_bookpublisher_book1` (`book_id` ASC) ,
INDEX `fk_bookpublisher_publisher1` (`publisher_id` ASC) ,
PRIMARY KEY (`book_id`, `publisher_id`) ,
CONSTRAINT `fk_bookpublisher_book1`
FOREIGN KEY (`book_id` )
REFERENCES `book` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bookpublisher_publisher1`
FOREIGN KEY (`publisher_id` )
REFERENCES `publisher` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;