Вставка строк
INSERT adds a new row to a table. List the columns, then a matching set of VALUES:
INSERT INTO product (code, title, price, in_stock)
VALUES ('P04', 'Ruler', 1.20, 75);
Text goes in single quotes; numbers do not. The values must line up with the columns, in the same order.
INSERT changes the data but returns no rows itself, so Run just reports how many rows changed. To see the new row, run a SELECT afterwards.
The checker does this for you: it runs your INSERT, then reads the table back to confirm your row is there.
Question. Add the product P05, an Eraser costing 0.60, with 120 in stock.
Answer. When you give a value for every column, you may leave out the column list. The values must then follow the table's own column order (code, title, price, in_stock):
INSERT INTO product VALUES ('P05', 'Eraser', 0.60, 120);
Reading the table back:
code | title | price | in_stock
P01 | Notebook | 2.5 | 40
P02 | Pen | 0.8 | 200
P03 | Backpack | 18 | 15
P05 | Eraser | 0.6 | 120
Listing the columns is still the safer habit: the statement keeps working if the table gains a column.
- Wrong number of values.
INSERT INTO product VALUES ('P05', 'Eraser', 0.60)fails:table product has 4 columns but 3 values were supplied. - A primary key that is already used. Inserting a second
'P01'fails withUNIQUE constraint failed: product.code. - Text and dates go in single quotes:
'Ruler','2024-04-10'. Numbers do not.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Добавить новый продукт в таблицу product: код 'P04', название 'Ruler', цена 1.20, in_stock 75.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Добавьте нового клиента в таблицу customer: id 5, name 'Eve', city 'York'. Перечислите столбцы, которые вы заполняете, а затем укажите соответствующий VALUES.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Бен (клиент 2) делает первый заказ. Добавьте его в orders: поле id = 14, поле customer_id = 2, поле order_date = '2024-04-10', поле total = 7.25. Дата пишется в одинарных кавычках.
Нажмите Запустить, чтобы увидеть результат здесь.