6.1. Schemas and Instances
A Cslib.SQL.DbSchema names the tables and declares each table's columns, independently of
any data. A Cslib.SQL.Database instance gives each table a list of rows, which is a bag:
duplicates matter and the order does not. A row is a function from column positions to cells,
and a cell is NULL (none) or a value. The instance below has a duplicate employee, an employee
without a department, and two departments with id 1.
inductive Tbl where
| emp
| dept
deriving DecidableEq
instance : Fintype Tbl where
elems := {.emp, .dept}
complete t := t:Tbl⊢ t ∈ {Tbl.emp, Tbl.dept} ⊢ Tbl.emp ∈ {Tbl.emp, Tbl.dept}⊢ Tbl.dept ∈ {Tbl.emp, Tbl.dept} ⊢ Tbl.emp ∈ {Tbl.emp, Tbl.dept}⊢ Tbl.dept ∈ {Tbl.emp, Tbl.dept} All goals completed! 🐙
/-- `emp(name text, dept int)` and `dept(id int, title text)`. -/
abbrev S : DbSchema where
Table := Tbl
arity _ := 2
columns
| .emp => ![⟨some "emp", "name", .text⟩, ⟨some "emp", "dept", .int⟩]
| .dept => ![⟨some "dept", "id", .int⟩, ⟨some "dept", "title", .text⟩]
abbrev emp : Query S 2 := .table Tbl.emp
abbrev dept : Query S 2 := .table Tbl.dept
abbrev D : Database S
| Tbl.emp => [![some (.text "ann"), some (.int 1)],
![some (.text "bob"), some (.int 1)],
![some (.text "cy"), none],
![some (.text "ann"), some (.int 1)]]
| Tbl.dept => [![some (.int 1), some (.text "eng")],
![some (.int 1), some (.text "ops")],
![none, some (.text "tmp")]]
example : D.Conforms := ⊢ D.Conforms All goals completed! 🐙
An instance conforms to its schema when every cell is NULL or a value of its column's declared
type. Equivalences and type soundness quantify over conforming instances only.