# MDB XML Schema documentation

Author: Manuel Lemos (mlemos@acm.org)

Version control: `@(#) $Id: xml_schema_documentation.html,v 1.5 2002/08/26 13:30:59 lsmith Exp $`

## Contents

- Introduction to XML
- Schema description format and XML
- Schema description data structure

- database
- table
- declaration
- field
- index
- index field
- initialization
- insert
- sequence
- sequence on table field
- variable

###
- Introduction to XML

The schema description format is based on XML (eXtensible Markup Language). For those that are not familiar with it, XML is standard that specifies rules to define abstract data formats based on tagged text.

Like HTML (HyperText Markup Language), XML is also based on SGML (Standard Generalized Markup Language). SGML defines rules to structure data using special text tags.

SGML tags may be used to delimit data sections. Section begin tags are of the form `<name attributes... >` and end tags are of the form `</name>`. `name` is the name of the tag and `attributes` is a set of zero or more pairs of attribute names and the values associated with the respective tag.

XML is a little stricter in the way tags may be used. While with many SGML formats some end tags are optional, in XML end tags are always required. Also, when tag attributes are used, attribute values must be specified always between quotes. These XML requirements are usually known as well-formedness.

Another important detail about XML strictness is that tag names and attributes are case sensitive. This means that tags in upper case are distinct from tags in lower case.

###
- Schema description format and XML

Unlike a common (but mistaken) belief, XML is not meant just for describing data in documents that are meant to be displayed or printed. XML is a standard that defines rules for describing abstract data may be used to for any purpose.

Even though it may be used to schemas that may be displayed or printed, Metabase schema description format is meant to provide a way for developers to design their database schemas using a DBMS independent file format. Using this format, developers may describe relations and properties of tables, field, indexes, sequences, etc..

This format uses just a subset of the XML known as SML (Simplified Markup Language). SML formats complies with the same rules as any XML format but it does not use all its possibilities to make it simpler for users to write and understand the data. For instance, files written on SML do not use any tag attributes.

Metabase schema description format is also simpler to edit by hand because tags and constant data values should always be in lower case to save the user from holding frequently the keyboard shift key.

###
- Schema description data structure

The Metabase schema description format lets the developers describe a set of database schema objects following the database objects hierarchy. This means that a database may contain tables and sequence objects, tables may contain fields and index objects and all these objects have their own attributes.

The definition of each database schema object contained within the begin and end tags of the respective container object. Therefore, the definition of each table and sequence has to be specified between the main database begin and end tags. Likewise, the definition of fields and indexes has to be specified between the respective table begin and end tags.

The properties of each schema object are also defined between the respective begin and end tags. The values of each property are also defined between the respective property being and end tags.

The values of the properties are subject of validation according to the type of each property and the context within which they are being defined.

Some properties define names of database schema objects. There are names that are accepted as valid for some DBMS that are not accepted by other DBMS. Metabase schema parser may optionally fail if such names are used to reduce the potential problems when using the same Metabase based application with different DBMS.

The schema object description tags are defined as follows:

####
- `database`

The `database` tag should be always at the top of the schema object hierarchy. Currently it may contain the definition of two types of objects: `table` and `sequence`.

The `database` schema object may have the following properties:

- `name`
(required)

Name of the database that is meant to be created when it is installed for the first time.

Default: none

- `create`

Boolean flag that indicates whether the database manager class should create the specified database or use a previously installed database of the same name.

This property may have to be set to `0` if you are splitting your database definition in multiple schema description files and only the installation of the first description file is supposed to lead to the actual database creation operation.

Another circumstance on which this property may have to be set to `0` is when the DBMS driver does not support database creation or if this operation requires special database administrator permissions that may not be available to the database user.

Default: `0`

- `overwrite`

Boolean flag that indicates whether the database manager class should overwrite previously created structures.

Default: `0`

- `description`

Free text property meant for describing the purpose of the database. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

- `comments`

Additional database comments. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

####
- `table`

The `table` is one of the main database schema objects. It may be used in a schema description multiple times, once per each table that is contained the database being described.

The `table` object definition may contain the `declaration` and `initialization` sections besides the properties. The `table` schema object may have the following properties:

- `name`
(required)

Name of the table.

Default: none

- `was`

Name that the table had before the last time it was renamed. This property is only meant to be used when the database is being upgraded and the table is meant to be renamed.

Beware that if this property is not specified and the `name` is changed, that is intended as if it is meant to drop the table with the previous name and create a new table without keeping the data that was previously stored in the table.

Default: none

- `description`

Free text property meant for describing the purpose of the table. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

- `comments`

Additional table comments. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

Example:

`<table>`

`<name>users</name>`

`<declaration>`

`<field>`

`<name>id</name>`
`<type>integer</type>`
`<notnull>1</notnull>`
`<default>0</default>`

`</field>`
`<field>`

`<name>name</name>`
`<type>text</type>`
`<length><variable>user_name_length</variable></length>`

`</field>`

`</declaration>`
`<initialization>`

`<insert>`

`<field>`

`<name>id</name>`
`<value>1</value>`

`</field>`
`<field>`

`<name>name</name>`
`<value>administrator</value>`

`</field>`

`</insert>`

`</initialization>`

`</table>`

####
- `declaration`

`declaration` is one of the sections that is part of the table definition. This section is required because it must contain the definition of the table `field` and `index` objects.

####
- `field`

`field` is one of the types of table definition object. It should be specified within the table declaration section for each field that the table should contain.

The `field` schema object may have the following properties:

- `name` (required)

Name of the field.

Default: none

- `was`

Name that the field had before the last time it was renamed. This property is only meant to be used when the database is being upgraded and the field is meant to be renamed.

Default: none

- `type` (required)

Type of the field. Valid type values are: `integer`, `text`, `boolean`, `date`, `timestamp`, `time`, `float`, `decimal`, `clob` and `blob`.

Default: none

- `default`

Default value for the field. The allowed values depend on the type of the field. If this property is not specified the default value is assumed to be `NULL`. If there is a table index on the field being declared, the `default` value must be specified because indexes can not be created on table fields with `NULL` values.

Notice that some low end DBMS do not support specifying default values in the field definition. Make sure that the you either specify all the values for each new row that is inserted in the tables or that the DBMS actually supports default values.

Only some DBMS support default values for large object fields. For this reason it is not allowed to specify default values on the definition of this type of field.

Default: none

- `notnull`

Boolean flag property that specifies whether the table field should be created with the constraint `NOT NULL`. As it suggests, this implies that it is not allowed to set this field value with `NULL`. The parser does not allow that an index is declared on a field that is not declared with the `notnull` flag property.

Default: `0`

- `unsigned`

Boolean flag property that specifies whether an integer field should be declared as unsigned integer. Note that some DBMS do not support unsigned integer fields. In such case the DBMS driver will ignore this property but it issues a warning.

Default: `0`

- `length`

Unsigned integer property that specifies the length of a text. If this property is specified the text field may not have a longer length. Text values shorter than the length are not padded. If this property is not specified the length limit is determined by the DBMS.

Default: none

- `description`

Free text property meant for describing the purpose of the field. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

- `comments`

Additional field comments. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

Example:

`<field>`

`<name>id</name>`
`<type>integer</type>`
`<notnull>1</notnull>`
`<default>0</default>`

`</field>`

####
- `index`

`index` is another type of table definition object. It should also be specified within the table declaration section for each field that the table should contain.

The `index` schema object may have the following properties:

- `name` (required)

Name of the index.

Only some DBMS support indexes on large object fields. For this reason it is not allowed to specify a large object field for indexes.

Default: none

- `was`

Name that the index had before the last time it was renamed. This property is only meant to be used when the database is being upgraded and the field is meant to be renamed.

Default: none

- `unique`

Boolean flag property that specifies whether the combination of the values of the index fields on each row of the table should unique.

Default: `0`

####
- index `field`

`field` is a section that is part of the table index declaration. It should be used once per each field on which the index should be created.

The index `field` declaration may have the following properties:

- `name` (required)

Name of the field on which the index should be created.

Default: none

- `sorting`

Type of field sorting that should be assumed when the index is created. On DBMS that support index sorting, queries that search tables on the fields specified by the given index may execute faster if the specified sorting type is chosen adequately. DBMS that do not support index sorting will ignore this property.

Valid sorting types are `ascending` and `descending`.

Default: none

Example:

`<index>`

`<name>users_index</name>`
`<field>`

`<name>id</name>`
`<sorting>ascending</sorting>`

`</field>`

`</index>`

####
- `initialization`

`initialization` is another section that is part of the table definition. This section is optional and may contain the definition of actions that should be executed when installing the database for the first time. Currently the only action that is supported is `insert`.

####
- `insert`

`insert` is a table initialization command that specifies the values of the fields of rows that should be inserted in the respective table after the database is created for the first time.

The `insert` command definition only contains one more `field` sections with `name` and `value` pairs that indicate the values of the fields are explicitly initialized when the new row is inserted. Missing fields are implicitly initialized by the DBMS with the respective default values.

####
- `sequence`

The `sequence` is another main database schema object. It may be used in a schema description multiple times, once per each sequence that is contained the database being described.

A `sequence` is an object that only contains an integer value. A sequence value is automatically incremented after every time it is fetch the next sequence value. The fetch and increment actions constitute an atomic operation. This means that if two concurrent accesses to the database fetch the next sequence value, they will always different values.

Sequences are useful to generate integer numbers that may be used to store in fields that must contain unique numbers.

The `sequence` object definition may contain one `on` section besides the properties. The `sequence` schema object may have the following properties:

- `name`
(required)

Name of the sequence.

Default: none

- `was`

Name that the sequence had before the last time it was renamed. This property is only meant to be used when the database is being upgraded and the sequence is meant to be renamed.

Default: none

- `start`

Integer property that specifies the value that the sequence will return when it is fetched the next sequence value.

Default:1

- `description`

Free text property meant for describing the purpose of the sequence. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

- `comments`

Additional table comments. This field is not processed by the schema parser. It may contain text or other XML well formed tags that may be useful for embedding additional data structures for documentation applications or other purposes.

Default: none

####
- sequence `on` table field

The sequence `on` `table` `field` section specifies a table field on which the sequence value should be synchronized.

This information is meant to be used only by the database manager class when it is added a sequence later after the database has been installed for the first time. If the sequence `on` is specified, the database manager class will override the sequence `start` value with a value that is higher than the highest value in the specified field table. Therefore, the specified field table type must be integer.

Example:

`<sequence>`

`<name>users_sequence</name>`
`<start>1</start>`
`<on>`

`<field>id</field>`
`<table>users</table>`

`</on>`

`</sequence>`

####
- `variable`

The `variable` tag is not meant to define any database schema object but rather a means to replace property values with variables defined at parsing time.

For instance, if you have several tables that store user names in text fields with the same length limit, instead of hard coding the length value in all field definitions, you may use a variable reference. The parser will replace variable references found in property definitions by the text values of the respective values passed to the parser.

The parser will issue an error if the it is referenced a variable that is not defined.