Lesson 10 +10 XP

INSERT INTO

The INSERT INTO statement adds new rows to a table.

Two syntaxes

Specify the columns, then the values:

Code

sql
INSERT INTO Customers (CustomerName, ContactName, Country)

### VALUES ('Cardinal', 'Tom B. Erichsen', 'Norway');

Or insert values for all columns (in table order) without naming them:

Code

sql

### INSERT INTO Customers
VALUES (1, 'Cardinal', 'Tom B. Erichsen', 'Norway');

Insert several rows at once

Code

sql
INSERT INTO Customers (CustomerName, ContactName, Country)

### VALUES
  ('Cardinal', 'Tom B. Erichsen', 'Norway'),
  ('Wolski', 'Zbyszek Piestrzeniewicz', 'Poland');

Text vs numbers

  • Text and dates go in single quotes.
  • Numbers are written without quotes.
Code

sql
INSERT INTO Orders (OrderID, CustomerID, OrderDate)

### VALUES (10248, 1, '2024-01-05');

Column order must match

The values are placed into the listed columns in order, so the first value goes into the first column you named.

TL;DR

  • INSERT INTO adds rows.
  • Name the columns, then list values in the same order.
  • Text and dates use single quotes; numbers don't.
  • You can insert several rows at once.