SQL Command Categories at a Glance
- DDL
- DML
- DQL (preview)
- TCL (preview)
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.DDL — Creating Tables
TheCREATE 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
Why is department_id nullable here?
Why is department_id nullable here?
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.What does DEFAULT 'Unknown' do?
What does DEFAULT 'Unknown' do?
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.How does INTEGER PRIMARY KEY work in SQLite?
How does INTEGER PRIMARY KEY work in SQLite?
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 astudent table with a required student_id primary key, a required student_name, and an optional email.
Solution
Solution
email has no NOT NULL constraint, so it accepts NULL when the email is not yet known.DDL — Altering Tables
UseALTER TABLE when you need to change a table’s structure after it has been created.
Add a Column
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 optionalphone column of type TEXT to the employee table.
Solution
Solution
DDL — Dropping Tables
DROP TABLE permanently deletes the table and every row inside it. There is no undo.
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
INSERT statements for each row.
Insert by Naming Columns (recommended)
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 ID106, salary 68000, city Chennai, joining date '2022-07-01', and department ID 2.
Solution
Solution
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
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 be56000, not 52000. Write the UPDATE statement to fix it.
Solution
Solution
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)
employee table itself intact, ready for new data.
DELETE vs. DROP
Practice: Remove an employee
Delete the employee withemployee_id = 106 (Kiran Kumar, added in the INSERT practice above).
Solution
Solution
Putting It All Together
Here is the full sequence to create and populate a minimal Employee Management database from scratch:Next: SQLite Setup
Connect Python to SQLite using the built-in
sqlite3 module and run your DDL and DML statements from a script.