I’m trying to construct a database, but I’m fairly clueless about MySql. The following is what I’m aiming at achieving:
http://snag.gy/eryLf.jpg
Here’s what I coded so far (tables are created in left to right order as seen in the picture)
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;
Also what kind of relationship is displayed in book to type, and book to grade?
The
book->typeandbook->graderelationships are 1:n wheretypeandgradeare optional. This is easily created with MySQL Workbench.This should do it:
SQL Fiddle