20_Join-in-SQL
20_Join-in-SQL
You will create a new database called myOtherBookshop that implements the database
schema shown in this diagram:
When you start the MySQL shell, you should see the MySQL prompt:
5. Create a table named Publisher based on the database design in the ER diagram
above. What kind of field types are the best for this table? Here is my suggestion. Do you
agree?
6. Can you create another table named ‘Book’ based on the database design in the diagram
above?
What sort of association would you use to connect the Publisher and Book tables?
7. Note that the Book table includes a foreign key to the Publisher table. Do you
remember how to create a table including foreign keys? Here is my suggestion. Do you
agree?
10. Insert dummy data into the Publisher table, including the rows that you can see in
the database design above.
11. Insert dummy data into the book table. Here’s an example of how you can insert data
in a table (book) with a foreign key:
Notice that you need to insert the publisher_id for Avon Books into the Book table. The
SELECT statement SELECT id FROM Publisher WHERE name = 'Avon Books'
embedded in the INSERT statement retrieves this id form the Publisher table so that it
can be inserted into the Book table.
Here you are inserting a new record for a book named ‘Database Systems’, with a price of
£40.25 published by ‘Avon Books’
12. Add two more publishers with the following data to your database:
address: ‘somewhere2’
address: ‘somewhere3’
13. Add six more books to your book table with the following data to your database:
price: 25
category: Computing
price: 31.99
category: Computing
name: ‘Biochemistry’
price: 50.40
category: Science
name: ‘Physics’
price: 12.99
category: Science
price: 28.50
category: Science
name: ‘Zoology’
price: 19.99
category: Science
14. See what data the Book and Publisher tables contain.
15. List all the books published by ‘Avon Books’ using the following SELECT statement:
Notice how we join the Book and Publisher table on the publisher id.
exit
Write a query to find all books and their publishers. Select all the columns from both tables.
Add another publisher, but don’t add a book to it. Run the query that you wrote above again.
What do you see? Is it what you expected?
End of lab
Congratulations on completing this lab.
In the next lab activity, you will explore more database operations.