How to reference primary key when inserting in MySQL I have a database containing the tables authors and books. 

I don't know how to correctly assign the foreign key in the books table. I read the information provided in this link and have constructed my DDL queries as follows:

CREATE TABLE if not exists authors(
-&gt; author_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
-&gt; name VARCHAR(30)
);

CREATE TABLE if not exists books(
-&gt; book_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
-&gt; title VARCHAR(255),
-&gt; genre VARCHAR(30),
-&gt; FOREIGN KEY(author_id) REFERENCES authors(author_id)
);


Is this the correct way to create a foreign key relationship between my authors and books tables?
You need to declare the column itself, so the books table should look like:

CREATE TABLE if not exists books
( book_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT
, title VARCHAR(255) NOT NULL
, genre VARCHAR(30) NOT NULL
, author_id INT UNSIGNED NOT NULL

,    FOREIGN KEY(author_id) REFERENCES authors(author_id)
);


I added NOT NULL on columns title and genre since I believe they are mandatory in most situations.

I would say that you also need something that uniquely identifies an author in real life, let's assume name does:

CREATE TABLE if not exists authors
( author_id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
, name VARCHAR(30) NOT NULL UNIQUE
);


Now you can insert a new book like:

INSERT INTO books (title, genre, author_id)
SELECT 'The Shining', 'Horror', author_id
FROM authors
WHERE name = 'Stephen King';