-
Notifications
You must be signed in to change notification settings - Fork 0
SQL Crash Course
omp-MySQL sends SQL to the database for you, but you still write the SQL. Good
news: you only need a handful of commands for 95% of a gamemode. This page teaches
them from scratch, with examples you can paste into MySQL Workbench or mysql.
Try these against a test database.
;ends each statement.
Everything is built from four commands:
| Verb | Does | Plain English |
|---|---|---|
SELECT |
read rows | "show me…" |
INSERT |
add a row | "add a new…" |
UPDATE |
change rows | "change…" |
DELETE |
remove rows | "remove…" |
SELECT * FROM accounts; -- every column, every row
SELECT name, money FROM accounts; -- just these columns
SELECT name, money FROM accounts WHERE id = 5;-- only matching rowsWHERE filters which rows. Combine conditions with AND / OR:
SELECT * FROM accounts WHERE money > 1000 AND banned = 0;ORDER BY sorts; LIMIT caps how many:
SELECT name, score FROM accounts ORDER BY score DESC LIMIT 10; -- top 10DESC = high→low, ASC (default) = low→high.
Counting & math (let the DB do it, don't pull every row):
SELECT COUNT(*) AS total FROM accounts; -- how many accounts
SELECT SUM(money) AS bank FROM accounts; -- total money
SELECT AVG(score) AS avg_score FROM accounts; -- averageAS something gives the result a name you read back with mysql_rs_get_*_by(0, "something", ...).
INSERT INTO accounts (name, money) VALUES ('Carol', 0);List the columns, then the matching values. Columns you skip use their DEFAULT
(e.g. id auto-fills, money might default to 0).
UPDATE accounts SET money = 500 WHERE id = 5; -- set money to 500
UPDATE accounts SET money = money + 100 WHERE id = 5;-- add 100
⚠️ Always includeWHEREon UPDATE/DELETE. Without it you change/delete every row:UPDATE accounts SET money = 0; -- DISASTER: everyone broke
DELETE FROM accounts WHERE id = 5; -- remove one account(Again: no WHERE = deletes the whole table's rows.)
CREATE TABLE IF NOT EXISTS accounts (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(24) NOT NULL UNIQUE,
money INT NOT NULL DEFAULT 0
) CHARACTER SET utf8mb4;See Designing your tables for what each part means.
-
LIKE— pattern match:WHERE name LIKE 'Bob%'(names starting with "Bob"). -
IN— match a list:WHERE id IN (1, 2, 3). -
NOW()— current time:INSERT ... VALUES (..., NOW()). -
JOIN— combine two tables (e.g. accounts + their vehicles):(You won't need JOINs early on — learn them when you have related tables.)SELECT a.name, v.model FROM accounts a JOIN vehicles v ON v.account_id = a.id;
You pass the SQL string to mysql_execute, and read results in the callback:
mysql_execute(g_DB, "SELECT money FROM accounts WHERE id = 5", "OnMoney");For anything with player input, don't paste it into the string — use
prepared statements or %e (see First queries).
- Create an
accountstable. -
INSERTthree players. -
SELECTthem ordered by money. -
UPDATEone player's money. -
SELECT COUNT(*). -
DELETEone player.
Once these feel natural, you can build almost anything. Next: First queries (doing this from Pawn) · Cookbook.
Understand
Use
- Installing MySQL
- Docker Compose
- Getting started
- Configuration
- SQL crash course
- Designing your tables
- Storing game data
- Dates & times
- First queries
- Async patterns
- Reading results
- Prepared statements
- Passwords & hashing
- Transactions
- Models (active-record)
- Tutorial: login system
- mysql-admin demo
Deeper
Reference