Tutorial

Tables, keys and types

Create tables with primary keys, unique keys, defaults and several column types, and look at them with DESCRIBE and SHOW CREATE TABLE.

Statements on this page were checked against MySQL and MariaDB.

A table with a primary key#

SQL
CREATE DATABASE shop;
USE shop;
CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  email VARCHAR(100),
  age INT,
  balance DECIMAL(10,2) NOT NULL DEFAULT 0,
  active TINYINT(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (id),
  KEY idx_name (name)
);

A MySQL table users in database shop is the Raft DB table users of the Raft DB database shop. The columns and keys are stored as a definition document; each row is stored as a document with one field per column, under a key built from the primary key. A table without a primary key gets a hidden row id.

Look at what you made:

SQL
SHOW TABLES;
DESCRIBE users;
SHOW CREATE TABLE users;
SHOW INDEX FROM users;

DESCRIBE shows PRI for the primary key and MUL for the index idx_name, as MySQL does.

More keys#

Unique keys other than the primary key work too:

SQL
CREATE DATABASE u;
USE u;
CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT,
  email VARCHAR(50) NOT NULL,
  handle VARCHAR(20) UNIQUE,
  region VARCHAR(5) NOT NULL,
  num INT NOT NULL,
  note VARCHAR(10),
  PRIMARY KEY (id),
  UNIQUE KEY uq_email (email),
  UNIQUE KEY uq_region_num (region, num),
  KEY idx_note (note)
);

A unique key is kept with a hidden table of claims, one per value, so two sessions cannot both insert the same value. Text is compared without regard to case in the default collation, so a@x.com and A@X.COM are the same value; a NULL in a unique key claims nothing, so any number of rows may have NULL there.

Types#

The column types include (all checked against MySQL):

GroupTypes
IntegersTINYINT, SMALLINT, INT, BIGINT, each signed or UNSIGNED
Numbers with a fractionFLOAT, DOUBLE, DECIMAL(p,s) (up to 65 digits)
TextCHAR, VARCHAR, TEXT family
Dates and timesDATE, DATETIME, TIMESTAMP, TIME, YEAR, with fractional seconds
OtherJSON, BOOLEAN, ENUM, SET, BIT, BLOB, BINARY

A table that uses several of them:

SQL
CREATE TABLE a (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL DEFAULT 'x', price DECIMAL(8,2), born DATE, note TEXT, flag TINYINT(1), big BIGINT UNSIGNED, ts DATETIME DEFAULT CURRENT_TIMESTAMP, dbl DOUBLE, UNIQUE KEY uq_name (name), KEY idx_born (born));
CREATE TABLE b (x INT, y INT, PRIMARY KEY (x, y));

Typing is strict, with MySQL's own errors: a value that does not fit is refused, not silently changed (see the next page).

Look at the catalog#

The information_schema tables answer as MySQL's do:

SQL
SELECT table_name, table_type, engine FROM information_schema.tables WHERE table_schema = 'isdb' ORDER BY table_name;
SELECT column_name, ordinal_position, column_default, is_nullable, data_type, column_type, column_key, extra FROM information_schema.columns WHERE table_schema = 'isdb' AND table_name = 'a' ORDER BY ordinal_position;

(Replace isdb with your database's name.)

Change a table#

SQL
ALTER TABLE t ADD COLUMN note VARCHAR(10);
ALTER TABLE t ADD COLUMN flag TINYINT NOT NULL DEFAULT 7 AFTER name;
ALTER TABLE t ADD COLUMN pre INT FIRST;
DESCRIBE t;

(Table t is created first.) Existing rows get the default value of a new column.

Remove a table#

SQL
TRUNCATE TABLE users;
DROP TABLE users;

See also#