Editor

Create the Database Schema

Advanced · Write the code · from Build an Inventory Management System with SQLite

From the lesson: Step 1: Design the Database Schema

Good database design starts with thinking about what you need to store. For inventory, we need two core tables: products (what we sell) and transactions (every time stock moves in or out).

Read this part of the lesson

Create an in-memory SQLite database with two tables:

1. products: columns id (INTEGER PRIMARY KEY AUTOINCREMENT), name (TEXT NOT NULL UNIQUE), category (TEXT NOT NULL), price (REAL NOT NULL), quantity (INTEGER DEFAULT 0), reorder_level (INTEGER DEFAULT 10)

2. transactions: columns id (INTEGER PRIMARY KEY AUTOINCREMENT), product_id (INTEGER NOT NULL), type (TEXT NOT NULL), quantity (INTEGER NOT NULL), timestamp (DATETIME DEFAULT CURRENT_TIMESTAMP), with a FOREIGN KEY on product_id

Print "Schema created" after committing, then print the table names.

import sqlite3
# Create in-memory database and define schema

More Inventory System exercises

All Python exercises · 50 practice problems · Practical Projects quiz