SQLite is a database that every developer should have experience with.
The lecture will cover reasons to use SQLite and basic commands.
Resources
Free intro to software development career course via CourseCareers:
Introduction to a larger course covering software development.
Specializations include front end, back end, or DevOps.
Ad-free and downloadable videos available on the instructor's website.
Code from the video is available on GitHub.
Pronunciation and Overview
The author of SQLite pronounces it "SQLite."
SQLite.org is the official webpage.
SQLite is often called an embedded database, and it's one of the most popular databases due to its widespread integration with software.
It is available on mobile phones, computers and bundled with many applications.
SQLite doesn't create a separate process for the database.
It is embedded in the application.
SQLite is "serverless" because the database engine runs within the same process, thread, and address space as the application.
There is no message passing or network activity involved.
Comparison with Typical Web Development Databases
Typical setup: separate database server shared by all users of a web page through a back end.
SQLite is suitable for desktop software, distributed IoT software, or appliances where many instances of the database are needed without central connection.
Consider SQLite for pairing a database with software for distribution instead of hosting as a service.
Use Cases and Benefits
SQLite is effective for web development but may not be the first choice compared to Postgres or other relational databases.
SQLite can support hundreds of thousands of visits for websites.
Free to use in production and commercial use.
Licenses are available for legal proof of usage rights.
SQLite is easy to work with and suitable for many uses.
Downloading and Setup
Download SQLite from the downloads page.
On a Mac, use Homebrew via the command: brew install SQLite.
Install Homebrew, if needed, by visiting brew.sh and pasting the provided command in the terminal.
For non-Mac or Linux systems, use one of the options on the SQLite download page.
The lesson will cover using SQLite from an application in an upcoming lesson.
For now, use SQLite directly from the terminal.
After installation, add SQLite to your path by running the command provided in the terminal.
Basic Commands
Launch SQLite and create a database file:
sqlite3 intro.db
Clear the screen: .shell clear
Use up and down arrows to access previous commands.
Dot Commands
Special commands for SQLite begin with a dot.
These commands are not SQL but provide information about the database or perform auxiliary operations.
Documentation: SQLite.org/CLI.HTML.
Examples:
.help: List available dot commands.
.tables: List tables in the database.
.show: Display current settings.
.databases: Show the path to the database file.
.schema: Display the schema of the database.
Creating Tables
A table describes an entity.
Command: create table.
Example: Create a table for user information.
Example:
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
username TEXT NOT NULL UNIQUE,
email TEXT,
age INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Each column definition includes the name, data type, and constraints.
PRIMARY KEY: uniquely identifies each row in the table.
NOT NULL: requires that the column must have a value.
UNIQUE: ensures that no two rows have the same value in this column.
DEFAULT: specifies a default value for the column if none is provided during insertion.
Working with Files
Working inside the terminal can be difficult, consider working in a file and then executing that file.
Use a code editor like Visual Studio Code.
Create a new file with a .sql extension (e.g., intro.sql).
Run the SQL file from the terminal using the .read command:
sqlite3 intro.db
.read intro.sql
Modifying Table Structure
ALTER TABLE: modifies the structure of an existing table.
Example: Add a status column to the users table. sql
ALTER TABLE users
ADD COLUMN status TEXT;
Deleting Tables
DROP TABLE: deletes a table.
Example: sql
DROP TABLE users;
DDL Commands
Data Definition Language (DDL) commands:
CREATE TABLE
ALTER TABLE
DROP TABLE
Data Manipulation Language (DML)
DML is used to insert, update, and remove data.
Inserting Data
INSERT INTO: adds new rows to a table.
Specify the table name and the columns to insert values into.