authors
People who wrote books.
A library tracks what it owns, who wrote it, who is allowed to borrow, and what is currently out. The book-to-author link is many-to-many, since books can have several authors.
People who wrote books.
Titles in the catalogue.
Library cardholders.
A junction table linking books and authors. A book can have several authors, and an author several books.
Each time a member borrows a book. returned_on stays empty until it comes back.
book_authors is a junction table between books and authors: many-to-many.loans.book_id references books.id: one row in books can have many rows in loans.loans.member_id references members.id: one row in members can have many rows in loans.Copy it into your database, or open it in Crowsfoot to edit the diagram, add notes, and export again.
CREATE TABLE authors (
id bigint PRIMARY KEY,
name varchar(160) NOT NULL
);
CREATE TABLE books (
id bigint PRIMARY KEY,
title varchar(200) NOT NULL,
isbn varchar(20),
published_year int
);
CREATE TABLE members (
id bigint PRIMARY KEY,
name varchar(120) NOT NULL,
email varchar(255) NOT NULL,
joined_on date NOT NULL
);
CREATE TABLE book_authors (
book_id bigint NOT NULL REFERENCES books (id),
author_id bigint NOT NULL REFERENCES authors (id),
PRIMARY KEY (book_id, author_id)
);
CREATE TABLE loans (
id bigint PRIMARY KEY,
book_id bigint NOT NULL REFERENCES books (id),
member_id bigint NOT NULL REFERENCES members (id),
loaned_on date NOT NULL,
due_on date NOT NULL,
returned_on date
);
New to ER diagrams? Read the crow’s foot notation guide.