SQLite is a lightweight relational database management system (RDBMS). It's a great way to get started learning SQL because it's so easy to install and run, compared with other RDBMSes.
If you haven't already, please follow the software setup instructions to install SQLite.
In a pinch, you can also run SQLite in your browser using
sqlime.org
. But this is a bit of an unholy hack, so it's
not recommended for real work. Note that sqlime.org
does not implement the
shell interface of SQLite, but only the SQL parts! Any command that
starts with a .
such as .help
will not work.
There are many useful "dot commands" in the SQLite shell:
.help
to get a list of SQLite shell commands..tables
to get a list of table names..schema
to print the schema of the whole database as a bunch of CREATE
TABLE
statements..mode box
to print query results nicely..headers on
to show column names on query results.There are also many useful pages of official documentation
Start the SQLite shell. (The details depend on your operating system. See the installation instructions.)
Create a table called books
with columns title
and year_published
:
CREATE TABLE books(
title TEXT,
year_published INT
);
Run the .tables
command. You should see books
listed.
Add some rows to the table:
INSERT INTO books
VALUES
('Database Systems: The Complete Book', 2008),
('Dune', 1965);
List all books:
SELECT *
FROM books;
By default, SQLite prints each row on one line with fields separated by a |
character:
Database Systems: The Complete Book|2008
Dune|1965
For a nicer appearance, say .mode ascii
and re-run the SELECT
statement above to get:
+-------------------------------------+----------------+
| title | year_published |
+-------------------------------------+----------------+
| Database Systems: The Complete Book | 2008 |
| Dune | 1965 |
+-------------------------------------+----------------+
(Other modes to try: .mode box
and .mode column
)
Some of the items listed below are good things about SQLite, some not so good (in our opinion), and others are sometimes good and sometimes bad! (See also the list of distinctive features of SQLite and the list of quirks.)
SQLite has many quirks, but also several advantages. If you choose to use it for a project after this course, here is some real-world advice. (You do not necessarily need to follow this advice on the homework.)
Each person will develop there own personal taste for which quirks of SQLite are worth taking advantage of and which are worth steering clear of. Here is some advice on what has worked well for us in the past when using SQLite:
PRAGMA foreign_keys =
ON
at the beginning of
every connection (supported in SQLite since 2009).INTEGER PRIMARY
KEY
in SQLite. Every table with a primary key should either have an INTEGER
PRIMARY KEY
(declared exactly like that, and not INT PRIMARY KEY
!) or
else the table should be declared WITHOUT
ROWID
(supported in SQLite since
2013).INTEGER
, REAL
, TEXT
, and BLOB
.STRICT
(supported in SQLite since 2021). This means that column types will actually
be enforced. Note that intermediate query results that are not stored in a
STRICT table can still be dynamically typed. And it's important to
understand that the physical storage of all tables, including STRICT tables,
is dynamically typed. But in our experience, this helps prevent bugs in
applications.UPSERT
where
appropriate.