Senior
//Need to describe the library model. There are 3 entities: "Author", "Book", "Reader" Physically, there is only one book and it can only belong to one reader. You need to create tables for the library to account for this.
sobes.tech AI
Answer from AI
To describe a library model with entities "Author", "Book", and "Reader", considering that each book exists as a single copy and can be with one reader, the following tables can be proposed:
-- Authors table
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
-- Books table
CREATE TABLE books (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT NOT NULL REFERENCES authors(id),
reader_id INT UNIQUE REFERENCES readers(id) -- a book can be with one reader or none
);
-- Readers table
CREATE TABLE readers (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
Explanation:
- Each book is linked to one author via
author_id. - The
reader_idfield in thebookstable indicates who currently owns the book. It is unique so that one book cannot be with multiple readers. - If the book is not issued,
reader_idwill be NULL.
This approach reflects the physical uniqueness of the book and its current owner.