fk in insert mysql 1

fk in insert mysql

INSERT INTO joke(joke_text, joke_date, author_id)
VALUES (‘Humpty Dumpty had a great fall.’, ‘1899–03–13’, 
        (SELECT id FROM author WHERE author_name = ‘Famous Anthony’));

Here is what the above code is Doing:
1. We’re inserting a new joke into the joke table.
2. We’re specifying the joke text and the date the joke was added.
3. We’re specifying the author of the joke by using a subquery.
4. The subquery is selecting the id of the author from the author table where the author_name is Famous Anthony.

Similar Posts