Reference

SQL & Redis

SQLite, PostgreSQL, Snowflake and Redis syntax, queries and everyday operations.

Scope and conventions

SQL examples name their database and assume the sample schema where indicated. Redis examples are native redis-cli commands, with optional Search/JSON features labeled. Run independent examples in your own scratch database; driver binding syntax and transaction guarantees differ.

Records & types

Distinguish text, names and comments

Redis

In redis-cli, quotes keep a command argument containing spaces together. The client removes its quoting before sending separate command arguments. These are not SQL identifier/value quoting rules; binary-safe client APIs should send the argument list directly.

Fragment

Redis · Distinguish text, names and comments
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SET ref:message "north gate"
GET ref:message

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Read native type names

Redis

A key has a native type such as string, hash, list, set or sorted set. Hash fields and ordinary strings hold bytes, even when their contents look numeric. Integer increment commands parse those bytes. JSON values have separate typed semantics when JSON support is available.

Fragment

Redis · Read native type names
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SET ref:count 24
TYPE ref:count
INCR ref:count
HSET ref:item:1 qty 2 name Notebook
TYPE ref:item:1

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Create sample records

Redis

Hashes keep named fields without a relational schema or declared foreign key. The Search index below is an additional native index over ref:items:* hashes; it requires Redis Search. Create it before inserting this tiny fixture, and account for your server’s indexing visibility before querying it.

Fragment

Redis · Create sample records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.CREATE ref:items ON HASH PREFIX 1 ref:items: SCHEMA item_id NUMERIC SORTABLE name TEXT category TAG qty NUMERIC price NUMERIC
HSET ref:items:1 item_id 1 name Notebook category paper qty 2 price 3.50
HSET ref:items:2 item_id 2 name Pen category paper qty 0 price 1.25
HSET ref:items:3 item_id 3 name Lamp qty 1 price 12.00

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Recognize keys and constraints

Redis

Redis hashes do not acquire NOT NULL, UNIQUE, CHECK or foreign-key enforcement by naming fields. Use native atomic commands or a bounded server script for the chosen invariant; a Search index is not a relational constraint.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.

Generate an identity

Redis

INCR atomically increments one integer string. It can allocate a key suffix, but incrementing and writing a separate hash are separate operations unless explicitly grouped. Gaps and retries still need an application policy.

Fragment

Redis · Generate an identity
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
INCR ref:next-item-id

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.