Skip to main content
Before you can query data you need somewhere to put it. This page teaches you the two halves of SQL that build and fill a database: DDL (Data Definition Language) shapes the structure of your tables, and DML (Data Manipulation Language) manages the rows inside them. You will build a complete Employee Management schema and populate it with realistic data by the end.

SQL Command Categories at a Glance

DDL statements define and modify the structure of your database. They affect tables and columns, not rows.

SQLite Data Types

SQLite uses a flexible type affinity system. Every column has an affinity — a preferred type — but SQLite will store any value in any column. Understanding the five native types keeps your schema intentional and your data clean.
Use TEXT for dates and times in SQLite (stored as 'YYYY-MM-DD'). SQLite has date functions that work on text in that format, and it avoids time-zone headaches.

DDL — Creating Tables

The CREATE TABLE statement defines your table’s name, its columns, each column’s type, and any constraints.

Basic Syntax

Constraints

Constraints enforce data rules at the database level so bad data never gets in.

Creating the Department Table

department_id is the primary key. department_name is required — you cannot insert a department without a name.

Creating the Employee Table

An employee might not yet be assigned to a department — perhaps they are newly hired. The FOREIGN KEY constraint still ensures that if a value is provided, it must exist in the department table. NULL means “not yet known”, which is a valid real-world state.
If you INSERT a row and omit the city column, SQLite automatically stores 'Unknown' instead of NULL. This gives you a non-null fallback without forcing every INSERT to supply the value.
In SQLite, INTEGER PRIMARY KEY is a special alias for the internal rowid. If you insert a row without supplying employee_id, SQLite auto-increments it. This is SQLite’s version of AUTO_INCREMENT.

Practice: Create a student table

Create a student table with a required student_id primary key, a required student_name, and an optional email.
email has no NOT NULL constraint, so it accepts NULL when the email is not yet known.

DDL — Altering Tables

Use ALTER TABLE when you need to change a table’s structure after it has been created.

Add a Column

The new email column is added to every existing row with a value of NULL.

Rename a Column

SQLite supports ADD COLUMN and RENAME COLUMN. It does not support DROP COLUMN in older versions (prior to 3.35.0). For complex schema changes on older SQLite, the recommended approach is to create a new table, copy data, and drop the old one.

Practice: Add a phone column

Add an optional phone column of type TEXT to the employee table.

DDL — Dropping Tables

DROP TABLE permanently deletes the table and every row inside it. There is no undo.
Use DROP TABLE IF EXISTS to avoid an error when the table might not exist:
Always double-check your target before running DROP TABLE. If foreign keys reference the table you are dropping, SQLite will raise an error to protect data integrity.

DML — INSERT

INSERT INTO adds new rows to a table.

Insert a Single Row (all columns in order)

Insert Multiple Rows at Once

Batching inserts is much faster than running separate INSERT statements for each row. Naming columns explicitly makes your SQL resilient to future schema changes and instantly readable.

Insert Multiple Employees

Practice: Insert a new employee

Insert an employee named Kiran Kumar with employee ID 106, salary 68000, city Chennai, joining date '2022-07-01', and department ID 2.

DML — UPDATE

UPDATE modifies existing rows. Always pair it with a WHERE clause — without one, every row in the table changes.

Syntax

Update a Single Employee’s Salary

Update Multiple Columns at Once

Update All Rows in a Department

This gives every Engineering employee a 10% pay rise.
Forgetting the WHERE clause in UPDATE changes every row in the table. Always verify your condition with a SELECT query first before running the UPDATE.

Practice: Correct a salary

Anitha Rao’s salary should be 56000, not 52000. Write the UPDATE statement to fix it.
Using employee_id is safer in production since names are not guaranteed to be unique:

DML — DELETE

DELETE removes rows from a table. Like UPDATE, always include a WHERE clause unless you intentionally want to clear the entire table.

Delete a Specific Row

Delete Based on a Condition

Delete All Rows (keep the table structure)

This removes every row but keeps the employee table itself intact, ready for new data.

DELETE vs. DROP

Practice: Remove an employee

Delete the employee with employee_id = 106 (Kiran Kumar, added in the INSERT practice above).

Putting It All Together

Here is the full sequence to create and populate a minimal Employee Management database from scratch:
In the next page you will run all of these statements from Python using sqlite3, so you can automate database creation as part of your application’s startup code.

Next: SQLite Setup

Connect Python to SQLite using the built-in sqlite3 module and run your DDL and DML statements from a script.