Appendix: Glossary of Database Terms
In this section you will design and create your own database without a script to help you. Use either the SQLite CLI or DBeaver.
A local vet wants to keep track of their clients and the pets they bring in. Create a new empty database called pets.db and write the DDL to create the following two tables:
owners
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY AUTOINCREMENT |
| first_name | TEXT | NOT NULL |
| last_name | TEXT | NOT NULL |
| TEXT | UNIQUE |
pets
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY AUTOINCREMENT |
| name | TEXT | NOT NULL |
| species | TEXT | NOT NULL |
| breed | TEXT | |
| age | INTEGER | |
| owner_id | INTEGER | NOT NULL, FOREIGN KEY → owners |
Once both tables exist, insert at least 3 owners and 6 pets, making sure at least two owners have more than one pet.
Check: run
.tablesin the CLI (or look in the Database Navigator in DBeaver) to confirm both tables were created. Then runSELECT * FROM pets;to see your data.
The vet also wants to record when each pet came in for a check-up. Add a third table to pets.db:
visits
| Column | Type | Constraints |
|---|---|---|
| id | INTEGER | PRIMARY KEY AUTOINCREMENT |
| visit_date | TEXT | NOT NULL |
| reason | TEXT | |
| pet_id | INTEGER | NOT NULL, FOREIGN KEY → pets |
Insert at least 4 visits across different pets.
Think: why is there no direct link between
visitsandowners? How would you find all visits for a given owner?
Download music_library.sql (music_library.sql) from the course materials. Create a new empty database called music_library.db and load the script into it using either the SQLite CLI or DBeaver.
Once it is loaded, run the following query to verify everything is in place:
SELECT al.title, al.release_year, ar.name AS artist
FROM albums al
JOIN artists ar ON ar.id = al.artist_id
ORDER BY al.release_year
LIMIT 10;
You should see 10 albums with their artist names, ordered by year.
Use books_library.db (books_library.sql ) for exercises 4–8, and music_library.db (music_library.sql ) ffor exercises 9–10. Write and run each query in DBeaver or the SQLite CLI.
Write queries to find:
'Horror' genre.'British'.first_name and last_name into one column called author).published_year.Switch to music_library.db and write queries to find:
'Hip-Hop' genre, showing album title and artist name.All exercises in this section use books_library.db.
'Comedy Fantasy' to 'Fantasy'.nationality of one author of your choice (pick something plausible).published_year of one of the books you inserted in Exercise 11.Check: after each UPDATE, run a SELECT to confirm the change took effect.
Think: what order do you need to delete in, and why?
For all JavaScript exercises, create a new project folder, install better-sqlite3, and copy in books_library.db.