Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Adding Multiple Rows to a Table1:50 with Andrew Chalkley
Inserting data into a table doesn't have to be restricted to one row at a time. You can insert multiple rows in a single statement.
Inserting multiple rows in a single statement:
INSERT INTO <table> (<column 1>, <column 2>, ...) VALUES (<value 1>, <value 2>, ...), (<value 1>, <value 2>, ...), (<value 1>, <value 2>, ...);
INSERT INTO users (username, first_name, last_name) VALUES ("chalkers", "Andrew", "Chalkley"), ("ScRiPtKiDdIe", "Kenneth", "Love"); INSERT INTO movies (title, genre, year_released) VALUES ("Starman", "Science Fiction", 1984), ("Moulin Rouge!", "Musical", 2001);
See all of the SQL used in Modifying Data With SQL in the Modifying Data With SQL Cheatsheet.
When writing queries or reading information from a database, you generally
issue one query at a time because you want to do something with the result set.
However, when you're inserting values, especially when you're populating
an initial data set, you may want to insert multiple rows at once.
Let's look at adding some more books to our books table.
Here's three new books we want to add, The Circle,
Contact, and Animal Farm.
You tend to see multiple statements like this in files that set up the initial
state of a database, called database seed files.
You're seeding information into the database.
Sometimes, if you're adding a couple of entries yourself,
you don't want to write the first bit of the query over and over again.
What you can do is separate each of the sets of values,
meaning the values in parentheses, with a comma.
You can use a return in your white space between key words, values, table and
You may want to do this to make your SQL statement clearer to read.
So, in short, you can insert multiple rows in a single statement.
You'd write INSERT INTO, the set of columns in the order you want to specify
your values, the values keyword, then each row's
values in parentheses, with each set of values separated by a comma.
You need to sign up for Treehouse in order to download course files.Sign up