Complete Intro to SQLite

5 hours CC
Complete Intro to SQLite

Course Description

SQLite is the world’s most widely-deployed database due to it’s lightning fast performance and zero configuration setup. Learn SQL queries, table management, joins, and performance optimization. Build a Node.js application with SQLite, implement full-text search, work with JSON data, and scale your database using tools like Litestream and LiteFS.

This course and others like it are available as part of our Frontend Masters video subscription.

Preview
Close

Course Details

Published: October 1, 2024

Learn Straight from the Experts Who Shape the Modern Web

Your Path to Senior Developer and Beyond
  • 200+ In-depth courses
  • 18 Learning Paths
  • Industry Leading Experts
  • Live Interactive Workshops
Get Unlimited Access Now

Table of Contents

Introduction

Section Duration: 16 minutes
  • Introduction
    Brian Holt begins the course by sharing his background and walks through the course website which contains all the notes and code samples.
  • What is SQLite
    Brian shares a brief history of SQLite and discusses several use cases. Due to its size, flexibility, and performance, SQLite is suggested to be the most widely distributed software of all time. SQLite is great for everything from production applications to local development environments.

SQL Syntax

Section Duration: 1 hour, 48 minutes
  • Getting Started with SQLite
    Brian demonstrates how to install SQLite and introduces dot commands, which are built-in commands for working with the SQLite program. Some commands help inspect the database by describing the schema or listing tables. The full list of commands can be viewed with `.help`.
  • SELECT
    Brian demonstrates how to install SQLite and introduces dot commands which are built-in commands for working with the SQLite program. Some commands help inspect the database by describing the schema or listing tables. The complete list of commands can be viewed with `.help`.
  • LIKE, OFFSET, & ORDER BY
    Brian explains the keywords used to compare, limit, and sort results. The LIKE keyword allows for partial string comparison. When a returned dataset is limited, the OFFSET adjusts where the data set starts, and ORDER BY changes the sort order.
  • INSERT & DELETE
    Brian demonstrates how to insert and delete data in SQLite. By default, these statements do not return any confirmation message or data. The RETURNING keyword allows you to display the affected rows in the terminal.
  • SQL Tables
    Brian reviews tables, which are the core repository of data in an SQLite database. Tables have columns representing the fields and each field can be one of four types: INTEGER, REAL, TEXT, or BLOB. Tables can be edited with the ALTER command or removed with the DROP command.
  • Relational Data & JOIN
    Brian introduces joins, which allow related data from multiple tables to be brought together into a dataset. Where clauses can be added to joins to limit the data returned from each table. Multiple tables can be joined to create more complex relationships.
  • Other Types of Joins
    Brian demonstrates other join operations, including left, right, and natural joins. A Venn Diagram of the various joins is shared to help illustrate the join operations. Nature joins are not recommended because of their potential for unintended results.
  • Foreign Keys
    Brian introduces foreign keys and explains how to build relationships between tables. SQLite3 doesn't enforce foreign key rules by default, so using PRAGMA foreign_keys=on; will enable enforcement.
  • Aggregation
    Brian demonstrates aggregation commands like COUNT, GROUP BY, and HAVING. Since a WHERE clause applies to the initial data set, not the aggregated data set, commands like COUNT and GROUP BY must be used in a HAVING statement to filter the results.
  • Testing Q&A
    Brian answers questions about testing SQLite queries. Most testing would happen at the application level in the form of unit or integration tests.
  • Subqueries
    Brian demonstrates subqueries which are sometimes an alternative to joins. Subqueries are nested in a WHERE clause of an outer query and are executed first.

Building a Project with Node.js & SQLite

Section Duration: 32 minutes

SQLite Features, Performance & Search

Section Duration: 39 minutes
  • Flexible Typing
    Brian highlights SQLite's flexible typing. For example, an INTEGER column will allow values that are also REAL, TEXT, and BLOB. While this can have unintended consequences, it does allow most SQL queries written for other databases (like MySQL, Postgres, etc.) to coerce to a TEXT data type.
  • Limits of SQLite
    Brian discusses limits for row, column, query, table joining, and the overall database size. These limits are beyond typical use cases and are rarely reached.
  • Views
    Brian explains that views are abstract or virtual tables. They simplify common SQL queries that may include complicated joins and are also a more secure way to present a subset of the data to a permission-restricted application or user.
  • EXPLAIN
    Brian uses EXPLAIN to analyze a query's execution. The EXPLAIN QUERY PLAN prefix provides a summarized version. When "--SCAN" appears in the results, it typically means the query looks at every row and may be inefficient or costly.
  • Indexes
    Brian uses indexes to optimize query performance. Indexes add a B-Tree (balanced tree), making lookups much faster. Instead of taking O(n) to look up items, it takes O(log n) since it can use a tree to find the item instead of scanning every item in the database.
  • Full Text Search
    Brian demonstrates the FTS5 extension that enables full-text search within a table. This eliminates the need to chain multiple LIKE clauses to search for text across multiple columns. The bm25 function, which returns the "best match" is also demonstrated in this lesson.

Working with JSON

Section Duration: 42 minutes
  • SQLite Extensions
    Brian installs sqlpkg which allows developers to install extensions for SQLite. These third-party extensions add additional features not available in SQLite by default.
  • JSON Extension
    Brian introduces the JSON extension for SQLite. It makes it possible to read and write arbitrary data, treating SQLite like a document-based database similar to MongoDB. NOTE: The JSON extension is now included in SQL and doesn't need to be installed separately.
  • JSON Operations
    Brian demonstrates some common JSON operations in SQLite. JSON allows data to be inserted, removed, and replaced. Specific values can be extracted from JSON with the -> and ->> operators.
  • JSONB
    Brian switches to JSONB, a more compact, binary representation of JSON. The output is harder to read since the format is more compact, but JSONB adds features like retrieving nested data and additional syntactic sugar, leading to a better developer experience.
  • Advanced Queries
    Brian demonstrates a few advanced queries, including counting the most favorited band with the json_each function and updating JSON data with json_insert.

Scaling SQLite

Section Duration: 53 minutes
  • Database Backups with Litestream
    Brian introduces Litestream, a database backup solution for SQLite. Litestream runs as a separate background process and continuously copies write-ahead log pages from disk to one or more replicas. This asynchronous replication provides disaster recovery similar to what is available with database servers like Postgres or MySQL.
  • Replication Overview
    Brian discusses database replication and discusses tools that can replicate SQLite databases like Litefs. Litefs simulates a virtual filesystem and then replicates it across machines. It makes SQLite think it's reading and writing to a file, while underneath it is replicating it out to other replicas.
  • Replicating SQLite with LiteFS
    Brian spins up a Docker Compose project where Litefs is used to replicate an SQLite database. Once the project is running, a load balancer manages traffic between primary and replica databases.
  • libSQL & sqld
    Brian shares a couple of projects that forked SQLite and implemented additional features. Since SQLite doesn't allow contributions, forked versions like libSQL and sqld can be alternatives to developers who require a more modern or flexible feature set.
  • Local First
    Brian discusses an emerging trend called "Local-First Development". This approach prioritizes local, in-browser storage to increase the performance of client-side applications. The browser-based database is streamed seamlessly to the server behind the scenes to ensure the data is backed up and available on the server.

Wrapping Up

Section Duration: 5 minutes

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now