Rullst AcademyRullst Academy/Lesson
Databases and SQL I · Lesson 05 / 12

Insert and respect constraints

Insert with explicit columns and diagnose constraint violations.

Learning paths16 min

Let's discover together

  1. 1

    INSERT INTO table (columns) VALUES (values) makes mappings explicit. Avoid relying on every column’s order. Text uses single quotes; apostrophes in handwritten literals are doubled. Applications should parameterize input, as we will see.

  2. 2

    The example inserts Echo with valid zero stock. Repeating R-01 violates UNIQUE. A missing kit_id violates the foreign key: the runner enables foreign_keys on each connection instead of assuming SQLite defaults.

  3. 3

    Check the rule named in the error and correct input. Do not switch to a conflict-removing operation without understanding consequences. The database enforces rules; an application must still explain errors usefully.

  4. 4

    Each run starts from the initial schema. Put INSERT and SELECT in the same file to query an insertion. Saving lesson.sql saves instructions, not in-memory rows.

Your data workbench

Our model: kits.id ← loans.kit_id. One material can have many loans; names may repeat. We use fictional data only.

Predict before opening the reference. It shows verified output from the original example, not execution of your text. Run your changes with the kit on your computer.

-- Insert and respect constraints
INSERT INTO kits (id, code, label, category, stock) VALUES (5,'X-01','Echo','paper',0);
SELECT id, label, stock FROM kits WHERE id=5;
Compare with reference output
[
  {
    "columns": [
      "id",
      "label",
      "stock"
    ],
    "rows": [
      [
        5,
        "Echo",
        0
      ]
    ]
  }
]

Run the command in the extracted folder’s terminal. Use python3 on macOS/Ubuntu or py if that is your Windows launcher. Every run starts in memory; read README.md for saving files and recovery practice.

python lab.py run lesson.sql
My files and GitHub

The editor saves your .sql draft in this browser. Download it and replace lesson.sql in the kit; for the final project, availability.sql. The ZIP remains the original template. GitHub publishing sends only the confirmed file, without executing SQL or sending a database.

Official links and next step

✦
YOUR MISSION

Insert with explicit columns and diagnose constraint violations.

Now it's your turn

Discovery workshop

Read, predict and check

Insert with explicit columns and diagnose constraint violations.
Build your answers and check when you are ready.
A moment to understand

Why is a repeated code rejected?

I need a hint

The example inserts Echo with valid zero stock. Repeating R-01 violates UNIQUE. A missing kit_id violates the foreign key: the runner enables foreign_keys on each connection instead of assuming SQLite defaults.

Loading your journey…

Take it with you

Insert a valid object and test two violations separately. Record input, rule and result, including expected errors.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.