INSERT Statements
This YouTube video was created by Steve Griffith.
The INSERT
statement is used to add new data to the database. It starts with the keywords INSERT INTO
followed by the name of the table where you will be adding the record followed by a set of parentheses with the columns that you want to fill with information. The order of the columns is important.
Then comes the keyword VALUES
and another set of parentheses. Inside the second set of parentheses comes the values that we are inserting. The values MUST be in the same order as the columns inside the first set of parentheses. Strings and dates get single quotes around them. Numbers do not need quotation marks.
# Insert a record into the `movies` table
INSERT
INTO movies (`movie_title`, `director`, `year`, `genre_id`)
VALUES ('Pulp Fiction', 'Quentin Tarantino', 1994, 5);
Subqueries can also be used with the INSERT
statement to retrieve data from another table.
# Insert a record into the `movies` table using subqueries
INSERT
INTO movies (`movie_title`, `director`, `year`, `genre_id`)
VALUES ('Reservoir Dogs', 'Quentin Tarantino', 1992,
(SELECT `genre_id` FROM `genres` WHERE `genre_title` = 'Drama' LIMIT 1));