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

Model with rules

Read a schema and distinguish identity, required values, uniqueness and limits.

Learning paths16 min

Let's discover together

  1. 1

    Read schema.sql. kits describes materials; loans records events. Separating them avoids repeating all material attributes for each loan. Start with the questions and rules your model must support.

  2. 2

    PRIMARY KEY identifies a row. code is UNIQUE and NOT NULL: it cannot repeat or be missing. label may repeat. stock uses CHECK to require a nonnegative integer after SQLite affinity conversion; INTEGER alone is insufficient in an ordinary table.

  3. 3

    NULL means missing or unknown, not zero or empty text. CHECK can accept NULL when its expression is not false; add NOT NULL for required data. Database rules complement application validation.

  4. 4

    The example creates a practice table in memory. On separate runs, repeat an id and try negative stock. Read the violated constraint; do not remove it just to make the command pass.

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.

-- Model with rules
CREATE TABLE practice (id INTEGER PRIMARY KEY, code TEXT NOT NULL UNIQUE, stock INTEGER NOT NULL CHECK(typeof(stock)='integer' AND stock>=0));
INSERT INTO practice VALUES(1,'T-01',0);
SELECT id, code, stock FROM practice ORDER BY id;
Compare with reference output
[
  {
    "columns": [
      "id",
      "code",
      "stock"
    ],
    "rows": [
      [
        1,
        "T-01",
        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

Read a schema and distinguish identity, required values, uniqueness and limits.

Now it's your turn

Discovery workshop

Read, predict and check

Read a schema and distinguish identity, required values, uniqueness and limits.
Build your answers and check when you are ready.
A moment to understand

What requires a present, unique code?

I need a hint

PRIMARY KEY identifies a row. code is UNIQUE and NOT NULL: it cannot repeat or be missing. label may repeat. stock uses CHECK to require a nonnegative integer after SQLite affinity conversion; INTEGER alone is insufficient in an ordinary table.

Loading your journey…

Take it with you

Design a fictional hobby-object table. Explain its key, an optional value and two rejected values.

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.