Q:

Basic Join Tables and Foreign Keys

I am creating a library app for personal development and would like to further my knowledge of MySql.
I currently have two tables 'book' and 'category'
book
id   title                    author      category     isbn
---- -------                 ----------   ----------  ------- 
1    Treasure Chest          Jim Jones     1           14252637
2    Pirates Boat            Sue Smith     2           88447737
3    Adventure Land          Harry Jo      3           01918273
4    Winter Week             Sam Dill      3           00999337

category
id       cat_name   
----     -------         
1        Horror
2        Kids
3        Fiction
4        Science

I am doing a simple search where I want to display all books (select * from book) and I would like to display all the books with their corresponding categories. This works but displays the category number instead of the category name.
e.g
id      title           author      category  isbn
2710    Animals 123     Tiny Touch    2       978-1-84958-877-5 
2709    Animal Homes    Tiny Touch    2       978-1-84958-880-5 
2708    Black and White Usborne       3       978-1-4095-2393-2 
2707    Babies          Usborne       1       978-1-4095-3575-1 

How can I alter these tables in such a way that the cat_name and category are joined?
I tried to run a command in phpmyadmin as below however it returned an error;
ALTER TABLE book
ADD FOREIGN KEY (category) REFERENCES category(cat_name);

error
#1452 - Cannot add or update a child row: a foreign key constraint fails (`sslib`.`#sql-1ab4_1dae`, CONSTRAINT `#sql-1ab4_1dae_ibfk_1` FOREIGN KEY (`category`) REFERENCES `category` (`cat_name`))

PHP
$sql = "SELECT * FROM book";
$res = $conn->query($sql) or trigger_error($conn->error."[$sql]");
while($row = $res->fetch_array()) { //line 101

    echo '<tbody>';
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['title'] . '</td>';
    echo '<td>' . $row['author'] . '</td>';
    echo '<td>' . $row['category'] . '</td>';
    echo '<td>' . $row['isbn'] . '</td>';
    echo '<td>' . $row['publicationYear'] . '</td>';
    echo '</tr>';
    echo '</tbody>';
};

I have been looking at foreign keys and indexes however I can't seem to make any sense of them. 
I am quite new to this so any help is much appreciated.

A:

Make sql join to display all the books with their corresponding categories:
select
  b.title as 'book_title',
  c.cat_name as 'category_name'
from
  book b
  inner join category c on (c.id = b.category);

When you insert new book record, you have to make sure that category exists. MySQL has already taken care of that by providing Referential Integrity (in your post foreign key). Referential integrity doesn't work with all engines. It's supported in InnoDB. First make sure you use InnoDB, as from MySQL 5.5 is default.
You might want to try MySQL Workbench. It supports graphical table representation and connections between them, also generates sql script to create them. 

