Tutorial

Inserting, selecting, updating and deleting

Insert rows, read them with WHERE, ORDER BY and LIMIT, update and delete, and see how Raft DB refuses bad values with MySQL's error numbers.

All statements were checked against MySQL and run on the users table of the previous page.

Insert#

SQL
INSERT INTO users (name, email, age, balance) VALUES ('Alice', 'alice@example.com', 30, 100.50);
SELECT LAST_INSERT_ID();
INSERT INTO users (name, email, age) VALUES ('Bob', NULL, 25), ('Carol', 'carol@example.com', 41), ('dave', 'dave@example.com', 25);

id is AUTO_INCREMENT: the first row gets 1, and LAST_INSERT_ID() answers 1. An INSERT with several rows is written as one statement; every row is checked before the first is written.

Select#

SQL
SELECT * FROM users;
SELECT id, name FROM users WHERE age = 25 ORDER BY name;
SELECT name, balance FROM users WHERE balance > 0;
SELECT name FROM users WHERE name LIKE 'a%';
SELECT name FROM users WHERE age BETWEEN 26 AND 45 ORDER BY age DESC, name;
SELECT name FROM users WHERE id IN (1, 3, 99) ORDER BY id DESC;
SELECT id, name, age + 1 AS next_age, UPPER(name) AS shout FROM users ORDER BY id LIMIT 2;
SELECT id FROM users ORDER BY id LIMIT 1, 2;
SELECT name FROM users WHERE email IS NULL;

Results come back in primary-key order when there is no ORDER BY, as InnoDB returns them. Text comparison ignores case, as in MySQL's default collation: LIKE 'a%' finds Alice. LIMIT 1, 2 skips one row and returns two. The arithmetic and string functions follow MySQL: for example 7 / 2 is 3.5000 (exact decimals), and a division by zero is NULL.

With the rows above, SELECT id, name FROM users WHERE age = 25 ORDER BY name returns Bob (id 2) and dave (id 4).

Aggregates#

SQL
SELECT COUNT(*), COUNT(email), SUM(age), AVG(age), MIN(name), MAX(age), SUM(balance) FROM users;
SELECT COUNT(*) FROM users WHERE age > 100;
SELECT SUM(age), MAX(age) FROM users WHERE age > 100;

COUNT(*) over no rows is 0 and SUM over no rows is NULL, as in MySQL.

Update#

SQL
UPDATE users SET age = age + 10 WHERE age = 25;
UPDATE users SET balance = balance * 2, email = 'new@example.com' WHERE name = 'Alice';

Assignments run left to right, as in MySQL, so SET a = a + 1, b = a sees the new a.

Replace and upsert#

SQL
INSERT INTO users (id, name, age) VALUES (1, 'Alice2', 99) ON DUPLICATE KEY UPDATE age = age + 1, name = VALUES(name);
REPLACE INTO users (id, name) VALUES (3, 'Carol-replaced');

Delete#

SQL
DELETE FROM users WHERE name LIKE 'e%';
DELETE FROM users WHERE id > 100;
DELETE FROM users ORDER BY id DESC LIMIT 1;

Mistakes are errors, with MySQL's numbers#

Raft DB checks types strictly. Each of these statements fails:

StatementError
INSERT INTO users (id, name) VALUES (2, 'Dup');1062: the value is already present for key PRIMARY
INSERT INTO users (name, age) VALUES ('Frank', 'old');1366: the integer value is not valid for the column
INSERT INTO users (name) VALUES ('this name is much too long to fit into fifty characters of varchar');1406 Data too long for column name
INSERT INTO users (email) VALUES ('no-name@example.com');1364 Field name doesn't have a default value
INSERT INTO users (nosuch) VALUES (1);1054 Unknown column nosuch
INSERT INTO users (name) VALUES ('Eve'), ('Alice'), (NULL);1048: a NULL value cannot be stored in column name; because the statement is all or nothing, Eve and Alice are not inserted

Run the statements to see the exact text: the numbers and texts are those of MySQL, which is what the comparison with MySQL checks. See MySQL error numbers.

A unique key refuses duplicates, case-insensitively: inserting 'A@X.COM' when a@x.com exists fails with 1062 The value 'A@X.COM' is already present for key 'users.uq_email' in the MySQL dialect; MariaDB words the key as 'uq_email'.

Next#

Joins and grouping.