SQL Statement Types
This script uses two primary types of SQL statements to set up the database.Data Definition Language (DDL)
CREATE TABLE
DDL statements define or modify the database structure and schema.
CREATE TABLE command defines a new table with its columns, constraints, and data types.
Data Manipulation Language (DML)
INSERT INTO
DML statements manipulate or manage data within schema objects.
INSERT INTO command adds new records (rows) into the existing users table.
Understanding AUTOINCREMENT
TheAUTOINCREMENT keyword is a column constraint applied to the primary key.
What does AUTOINCREMENT do?
What does AUTOINCREMENT do?
AUTOINCREMENT constraint:- Generates a unique value for the
idcolumn automatically - Creates monotonically increasing values (each new ID is strictly greater than previous ones)
- Starts from 1 by default
- Eliminates the need to manually specify IDs when inserting records
When you insert a new row without providing an
id, SQLite automatically generates the next available integer.AUTOINCREMENT vs INTEGER PRIMARY KEY
AUTOINCREMENT vs INTEGER PRIMARY KEY
In SQLite, there’s a subtle but important difference:INTEGER PRIMARY KEY alone:
- Automatically increments
- May reuse IDs after deletion of rows with high IDs
- Guarantees strictly increasing values
- Never reuses IDs, even after deletions
- Tracks the highest ID ever used in
sqlite_sequencetable
Use
AUTOINCREMENT when you need audit trails, replication, or when ID reuse could cause issues. For most applications, INTEGER PRIMARY KEY alone is sufficient.Example Implementation
1
Import SQLite
2
Create connection and cursor
users.db in the current directory.3
Create the table
The table is created with three columns: id, name, and email.
4
Insert sample data
5
Commit and close
Complete Example
Key Concepts
DDL
Data Definition Language
Defines database structure (CREATE, ALTER, DROP)
Defines database structure (CREATE, ALTER, DROP)
DML
Data Manipulation Language
Manipulates data (INSERT, UPDATE, DELETE, SELECT)
Manipulates data (INSERT, UPDATE, DELETE, SELECT)
Constraints
NOT NULL, UNIQUE, PRIMARY KEY
Enforce data integrity rules
Enforce data integrity rules
Parameterized Queries
Use ? placeholders
Prevents SQL injection attacks
Prevents SQL injection attacks
SQLite is perfect for development, testing, and small applications. For production web applications with high concurrency, consider PostgreSQL or MySQL.