Documentation

CslibFrontier.Languages.SQL.Schema

Database schemas and instances #

A database schema (DbSchema) names the tables and gives each table its arity and its column declarations, independently of any data: an empty table still has columns and types. The typing context of a table (DbSchema.ctx) is the list of its declared column types.

A database instance (Database S) gives each table of S its rows. The rows form a bag, which is represented by a list: duplicates matter, and the list order is a property of this representation only, since SQL promises no order without ORDER BY. An instance conforms to its schema (Database.Conforms) when every cell of every table is NULL or a value of its column's declared type.

It adapts and redesigns the databases of sql_equiv (revision 38030a6b) by Evelyn Mitchell, which mapped table names to lists of name–value rows, so that a table's columns and types came from its data and an empty table had none; here the schema is separate data that every instance of it conforms to.

Main definitions #

A database schema: the table names, and for each table its number of columns and their declarations. It does not depend on data, so an empty table has columns too.

  • Table : Type

    The table names.

  • arity : self.Table → ℕ

    The number of columns of each table.

  • columns (t : self.Table) : Fin (self.arity t) → Column

    The column declarations of each table: names, qualifiers and declared types.

Instances For
    def Cslib.SQL.DbSchema.ctx (S : DbSchema) (t : S.Table) :
    Fin (S.arity t) → SqlType

    The typing context of a table: the declared types of its columns.

    Instances For
      @[reducible, inline]

      A database instance of the schema S: for each table, its rows. A table is a bag of rows, represented by a list; the list order means nothing in SQL.

      Instances For

        The instance conforms to its schema: every row of every table has the table's declared types.

        Instances For

          The instance with every table empty conforms to every schema.