-- WITHOUT ROWID database for the sqlite3 storage-interop traces. -- Built by the sqlite3 CLI; read back by ocaml-sqlite. -- -- sqlite3 stores a WITHOUT ROWID table in an index b-tree whose key is a -- record of the PRIMARY KEY columns followed by all remaining columns -- (file format 2.4, "Representation Of WITHOUT ROWID Tables"), not in a -- rowid-keyed table b-tree. Every table here exercises one property of that -- layout: the key order, the column permutation, DESC and COLLATE in the key, -- interior pages, overflow chains, and the ordinary tables that share the file -- and must stay readable whatever happens to the rest. -- An ordinary rowid table sharing the file. Reading it is the guard that one -- table's storage does not take the whole database down. CREATE TABLE plain (id INTEGER PRIMARY KEY, label TEXT); INSERT INTO plain VALUES (1,'one'),(2,'two'),(3,'three'),(4,'four'); -- The simple case: one text PRIMARY KEY, declared first. The b-tree order is -- the key order, which is not the insertion order. CREATE TABLE kvpair (k TEXT PRIMARY KEY, v INTEGER) WITHOUT ROWID; INSERT INTO kvpair VALUES ('delta',4),('alpha',1),('charlie',3),('bravo',2); -- The PRIMARY KEY is neither first nor alone, so the stored record is -- (b, a, c) while the table declares (a, b, c). CREATE TABLE reordered (a INTEGER, b TEXT PRIMARY KEY, c TEXT) WITHOUT ROWID; INSERT INTO reordered VALUES (10,'zulu','z'),(11,'alpha','a'),(12,'mike','m'); -- Composite PRIMARY KEY: stored (y, x, z), ordered by y then x. CREATE TABLE composite (x TEXT, y INTEGER, z TEXT, PRIMARY KEY (y, x)) WITHOUT ROWID; INSERT INTO composite VALUES ('p',2,'q'),('r',1,'s'),('a',2,'b'),('c',1,'d'); -- DESC in the key: the b-tree itself is in descending order, so a scan that -- re-sorts the rows by PRIMARY KEY ascending answers backwards. CREATE TABLE descending (k TEXT, v INTEGER, PRIMARY KEY (k DESC)) WITHOUT ROWID; INSERT INTO descending VALUES ('a',1),('b',2),('c',3); -- COLLATE in the key: the b-tree is in NOCASE order, which is not byte order. CREATE TABLE nocase (k TEXT COLLATE NOCASE PRIMARY KEY, v INTEGER) WITHOUT ROWID; INSERT INTO nocase VALUES ('Bravo',2),('alpha',1),('Charlie',3),('delta',4); -- An INTEGER PRIMARY KEY that is not a rowid alias: WITHOUT ROWID stores it in -- the record like any other column. CREATE TABLE intkey (n INTEGER PRIMARY KEY, s TEXT) WITHOUT ROWID; INSERT INTO intkey VALUES (30,'thirty'),(10,'ten'),(20,'twenty'); -- A UNIQUE constraint and a CREATE INDEX on a WITHOUT ROWID table. sqlite3 -- writes no sqlite_master row for the PRIMARY KEY (the table is that index) -- and numbers the UNIQUE autoindex _2, so a reopen has to read the numbering -- with that gap to connect each row to the constraint it stands for. CREATE TABLE indexed (k TEXT PRIMARY KEY, u INTEGER UNIQUE, g TEXT) WITHOUT ROWID; INSERT INTO indexed VALUES ('k1',7,'g1'),('k2',5,'g2'),('k3',9,'g1'); CREATE INDEX indexed_by_g ON indexed (g); -- Rows past the index max_local spill into overflow chains. CREATE TABLE wide (k TEXT PRIMARY KEY, big TEXT) WITHOUT ROWID; INSERT INTO wide VALUES ('w1', replace(hex(zeroblob(1500)), '0', 'a')), ('w2', replace(hex(zeroblob(2500)), '0', 'b')), ('w3', 'short'); -- Enough rows to split the index b-tree, so the read walks interior pages and -- the keys held in them, not only leaves. CREATE TABLE bulk (k TEXT PRIMARY KEY, v INTEGER) WITHOUT ROWID; INSERT INTO bulk WITH RECURSIVE seq(i) AS ( SELECT 1 UNION ALL SELECT i + 1 FROM seq WHERE i < 800 ) SELECT printf('key-%04d', i), i * 3 FROM seq;