SQL: Inserting Multiple Rows
Welcome to the Inserting Multiple Rows lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
When we insert many rows with separate
INSERTstatements, we're putting heavy load on the database. The database has to parse each insert statement, execute it safely by acquiring and releasing locks, and store its result on disk. All of those processes happen for every insert. There are many ways to mitigate those costs, but there's one way that reduces them all at once: multi-row inserts.The syntax is straightforward: we write an insert statement as normal, but with multiple rows of data after
VALUES. Each one becomes a separate row in the database. Now, the database only has to parse, lock, execute, and store data once. The amount of data stored is the same, but the overhead is much smaller.>
exec(`CREATE TABLE users (name TEXT NOT NULL)`);exec(`INSERT INTO users (name) VALUES('Amir'),('Betty'),('Cindy')`);exec(`SELECT * FROM users`);Result:
[{name: 'Amir'}, {name: 'Betty'}, {name: 'Cindy'}]Here's a code problem:
Use a multi-row insert statement to insert two cats, Ms. Fluff and Keanu.
exec(`CREATE TABLE cats (name TEXT NOT NULL)`);exec(`INSERT INTO cats (name) VALUES('Ms. Fluff'),('Keanu')`);exec(`SELECT * FROM cats`);- Goal:
[{name: 'Ms. Fluff'}, {name: 'Keanu'}]- Yours:
[{name: 'Ms. Fluff'}, {name: 'Keanu'}]
As usual, the database will still enforce constraints like
NOT NULL.>
exec(`CREATE TABLE users (name TEXT NOT NULL)`);exec(`INSERT INTO users (name) VALUES('Amir'),(null),('Cindy')`);Result:
Error: NOT NULL constraint failed: users.name