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、在庫75。
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
customer テーブルに新顧客を追加:id 5、name 'Eve'、city 'York'。入力するカラムをリストアップし、対応するVALUES を記述する。
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
顧客
2のベンが最初の注文を入れます。id14、customer_id2、order_date'2024-04-10'、total7.25を含めてordersに追加します。日付はシングルクォーテーションで囲みます。
実行ボタンをクリックして出力を確認してください。