我正在尝试构建一个数据库,但我对MySql一无所知。以下是我的目标: http://snag.gy/eryLf.jpg
这是我到目前为止编码的内容(表格是按照从左到右的顺序创建的,如图所示)
CREATE TABLE person (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(64),
lname VARCHAR(64)
) ENGINE = InnoDB;
CREATE TABLE bookauthor (
book_id INT(11),
author_id INT(11),
FOREIGN KEY (author_id) REFERENCES person (id) ON UPDATE CASCADE,
FOREIGN KEY (book_id) REFERENCES book (id) ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE TABLE bookpublisher (
book_id INT(11),
publisher_id INT(11),
FOREIGN KEY (book_id) REFERENCES book (id) ON UPDATE CASCADE,
FOREIGN KEY (publisher_id) REFERENCES publisher(id) ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE TABLE publisher (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(64)
) ENGINE = InnoDB;
CREATE TABLE bookillustrator (
book_id INT(11),
illustrator_id INT(11),
FOREIGN KEY (book_id) REFERENCES book(id) ON UPDATE CASCADE,
FOREIGN KEY (illustrator_id) REFERENCES person(id) ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE TABLE book (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(256),
type_id INT(11),
publication_date DATE,
value DECIMAL(10,2),
price DECIMAL(10,2),
notes TEXT,
signed TINYINT(1),
grade_id INT(10),
bagged TINYINT(1)
) ENGINE = InnoDB;
CREATE TABLE booktag (
book_id INT(11),
tag_id INT(11),
FOREIGN KEY (book_id) REFERENCES book(id) ON UPDATE CASCADE,
FOREIGN KEY(tag_id) REFERENCES tag(id) ON UPDATE CASCADE
) ENGINE = InnoDB;
CREATE TABLE tag (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
value VARCHAR(64)
) ENGINE = InnoDB;
CREATE TABLE type (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(64)
) ENGINE = InnoDB;
CREATE TABLE grade (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(256)
) ENGINE = InnoDB;
书中还会显示什么样的关系来打字,并预定成绩?
答案 0 :(得分:1)
book->type
和book->grade
关系为1:n,其中type
和grade
是可选的。这可以通过MySQL Workbench轻松创建。
这应该这样做:
-- -----------------------------------------------------
-- 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;