Skip to main content
TRACK B·DATA AND PERSISTENCE·Verified June 2026 · Lua 5.4 · ox_lib 3.x
Learning with an AI assistant?
Copies this lesson plus 2026 ground rules (no lua54 'yes', Cfx.re Portal, correct callback signatures) as a ready-to-paste mentor prompt.

SQL fundamentals and your first queries

Every script you have built so far forgets everything on restart. Cooldowns, bank balances, notes -- all gone. A database is the fix. It is a separate program that stores rows of data on disk and gives them back when you ask. This lesson teaches you the SQL you need: the four commands every database interaction uses, and then oxmysql, the bridge that lets your FiveM scripts run those commands safely from Lua so they can remember things forever.

You'll ship
Comfort writing SELECT, INSERT, UPDATE, and DELETE by hand, plus a working resource that creates a table, inserts a row with a safe parameterized query, reads it back, and updates it.
Time
~45 minutes
You'll learn
What a relational database is -> the four SQL commands (SELECT, INSERT, UPDATE, DELETE) -> install oxmysql -> CREATE TABLE -> ? placeholders -> read one row -> UPDATE, and what .await actually does
Prereqs
Modules 01-06 complete. A running FiveM server with MariaDB or MySQL installed, HeidiSQL (or another SQL client) connected to it, and the oxmysql resource downloaded.
Quasar School: ped data + SQL (part 2).
Alphi: SQL, JS, Lua, and Git masterclass.
BEFORE YOU START

SQL fundamentals: the four commands

Every day-to-day database interaction is one of four commands, sometimes called CRUD: SELECT to read, INSERT to create, UPDATE to change, DELETE to remove. Run these directly in HeidiSQL (the standard GUI that ships alongside MariaDB) or the mysql command line to get comfortable with the syntax before you wire any of it into Lua. The examples below assume a players table with name, money, and job columns.

SELECT: read data

code
-- Get all players
SELECT * FROM players;

-- Get only the name and money columns
SELECT name, money FROM players;

-- Get players with money over 5000
SELECT name, money FROM players WHERE money > 5000;

-- Get the top 5 richest players
SELECT name, money FROM players ORDER BY money DESC LIMIT 5;

* means "all columns", WHERE filters rows, ORDER BY sorts (DESC is highest first), and LIMIT caps the number of results.

INSERT: create new data

code
-- Add a new player
INSERT INTO players (name, money, job)
VALUES ('NewPlayer', 500, 'unemployed');

-- Add multiple rows at once
INSERT INTO items (name, label, weight)
VALUES
    ('bread', 'Bread', 1),
    ('water', 'Water Bottle', 1),
    ('medkit', 'Medkit', 2);

Column names go in parentheses, values in the same order, and string values need single quotes.

UPDATE: change existing data

code
-- Give a player more money
UPDATE players SET money = 1000 WHERE name = 'NewPlayer';

-- Give everyone 100 bonus (be careful with no WHERE!)
UPDATE players SET money = money + 100;

Always use WHERE unless you mean to update every row. Without it, UPDATE players SET money = 0 wipes everyone's money.

DELETE: remove data

code
-- Remove a specific player
DELETE FROM players WHERE name = 'NewPlayer';

-- Remove all players with 0 money
DELETE FROM players WHERE money = 0;

Never run DELETE without WHERE unless you want to empty the entire table. There is no undo; back up before you run one, and test the same condition as a SELECT first so you know exactly which rows it will hit.

Try the SELECT ... WHERE pattern yourself against a small sample table:

idnamemoneyjob
2Blair4200mechanic
4Devon1800police
2 of 4 rows match.

Keep reading the full lesson

Sign in to start, then unlock every step of this lesson and the full FiveM School with a membership.

Still ahead in this lesson
  • How it works
  • If something went wrong
  • What you can do now
  • Try it yourself

The remainder of SQL fundamentals and your first queries is available to FiveM School members.