#453 Books and Their Authors
Display the titles of all books and the surnames of their authors (title, surname):
-- Create the authors table
CREATE TABLE authors (
id INT PRIMARY KEY AUTO_INCREMENT,
surname VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
patronymic VARCHAR(255) NOT NULL,
birth_date DATE,
country VARCHAR(100),
biography TEXT
);
-- Create the books table
CREATE TABLE books (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
author_id INT NOT NULL,
publish_date DATE,
isbn VARCHAR(20) UNIQUE,
pages INT,
FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
);
Additional exercise: remove NOT NULL from author_id and remove FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE. How can all books and authors now be displayed, including books without an author?
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor