DewDrop One
XML Definition. Any Database. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Home | Sourceforge Project Page | Mailing List | JavaDoc API | Browse CVS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DewDrop SQLML TutorialWriting a database schema in XML
The point of using DewDrop is to define your database schema in XML only once, no matter how many database products you may use. DewDrop then translates this XML into SQL DDL for your target database. At the very minimum the following rules must be adhered to:
XML Tag Reference
DewDrop uses XML Schema to ensure that your database definitions conform to the above rules and others when specifying xml elements. If you are not familiar with XML Schema, the below tag descriptions will help you understand how your database definitions are to be structured. If you are familiar with XML Schema, you can also look at DewDrop's XML Schema Database Specification directly. Elements: <database>A database has 4 attributes:
A database must be defined in the following manner: <database xmlns="http://dewdrop.sourceforge.net/xmlns/database_1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Your database definition should look exactly like the above example, except for the "name" attribute. Use "name" to define a name for your database that makes sense. Within a database definition, there are one or more table, index or alteration definitions which define the tablespace of the database you are creating. <table>A database must contain one or more tables. A table has the following attributes:
A table may contain columns, table-wide constraints (primary keys, foreign keys, unique), and native sql declarations. They must be defined in the following order: <table attributes...> column definitions ... table primary key definition (optional) table constraints in any order (e.g. <foreignKey> or <unique> ) (optional) native declarations (optional) </table> A description of all of a table's components are described later. <column>Columns specify a table's data structure, and they are the core of what defines a database. For this reason, the <column> element is the most complex, but it is easily understood after understanding its attributes. A column has the following attributes:
A column may contain a foreign key element. If you specify both a foreign key using the shortcut method (with column attributes) and a foreign key using the explicit declaration, the shortcut will be overridden. That is, the explicit declaration takes precedence.
Shortcut Method:
<column name="myColumn" type="sqlDataType" foreignKeyAttributes...(optional) /> or: Explicit Method::
<column name="myColumn" type="sqlDataType"> ... foreign key declaration ... (optional) </column> <primaryKey>A row in a database table is uniquely identified from any other row by a primary key. A <primaryKey> is a collection of column references, that together, form a unique combination, different from any other combination in any other row. Many times a primary key is just one column, meaning that column is guaranteed to be unique in every row in the table. A Primary Key has the following attributes:
A Primary Key must have at least one local columnRef element to indicate what local columns in the table will participate in the table's primary key. You can define a Primary Key in two ways:
Standard Declaration: You can define single and multi-column primary keys by using the <primaryKey> element in a table definition. If you use the <primaryKey> element in a table definition, it must be the first element after the last <column> definition. You specify what columns are to participate in the primary key by using <columnRef> elements. Example: Standard definition,
single column Primary Key
<table attributes...> <column name="columnId" type="integer"/> other column declarations ... <primaryKey> <columnRef local="columnId"/> </primaryKey> </table> A <columnRef> element's "local" attribute tells DewDrop what column to reference local to the current table. (As you'll see later, <columnRef> elements can be used to reference foreign columns in other tables too). The Primary Key element can be used to just as easily create multi-column primary keys: Example: Standard definition,
multi-column Primary Key
<table attributes...> <column name="column1" type="varChar" length="50"/> <column name="column2" type="varChar" length="30"/> other column declarations ... <primaryKey> <columnRef local="column1"/> <columnRef local="column2"/> other local columnRefs... </primaryKey> </table> If you don't specify a name attribute for the primaryKey, then DewDrop will conveniently make one for you based on the table name, for example: my_table_name_pk. Shortcut Declaration: If you don't want, you can conveniently specify if a column participates in the table's primary key by specifying a "true" value for the column's primaryKey attribute: Example: Shortcut method,
single column Primary Key:
<column name="columnId" type="integer" primaryKey="true"/> This acts exactly like the first Standard definition, single column example shown above. DewDrop will create the primary key constraint trigger name for you based on the described naming convention. If you want to specify your own name, you'll have to use the <primaryKey> standard element declaration. Multi-column primary keys using the shortcut method are nearly identical: Example: Shortcut method,
multi-column Primary Key:
<column name="column1" type="varChar" length="50" primaryKey="true"/> <column name="column2" type="varChar" length="30" primaryKey="true"/> other column declarations ... Specifying multi-column primary keys this way behaves exactly like the second Standard definition, multi-column example shown above. DewDrop will will create the primary key constraint trigger name for you based on the described naming convention. If you want to specify your own name, you'll have to use the <primaryKey> standard element declaration. <foreignKey>A row in a database table can be constrained to only reference values that are in another row in another table. This is known as a foreign key, because the local row is being keyed off of a foreign table's row. Not all databases support foreign keys, but DewDrop is intelligent enough to know the differences. DewDrop will write out Foreign Key declarations in SQL only when your target database supports them, so you don' thave to worry about possible SQL syntax errors. Like primary keys, foreign keys can be single or multi-column keys. However, foreign keys must specify the foreign table they are referencing, as well as each foreign column their local columns reference. A Foreign Key has the following attributes:
A Foreign Key must have at least one columnRef element with both a local and a foreign attribute to indicate what local columns in the table will participate in the foreign key, as well as what foreign columns this key is to reference. You can define a Primary Key in two ways:
Standard Declaration: You can define single and multi-column foreign keys by using the <foreignKey> element in a table definition. If you use the <foreignKey> element in a table definition, it must be defined after any column definitions and the <primaryKey> element, if it exists. You specify what local columns are to participate in the foreign key by using <columnRef> elements, also specifying what foreign columns are keyed. Example: Standard definition,
single column Foreign Key
<table attributes...> <column name="columnId" type="integer"/> other column declarations ... primary key declaration (optional) <foreignKey foreignTable="otherTable" onDelete="cascade"> <columnRef local="columnId" foreign="otherTableColumnId"/> </primaryKey> </table> A <columnRef> element's "local" attribute tells DewDrop what column to reference local to the current table, and the "foreign" attribute tells DewDrop what column to key off of in the foreign table. Both are required when specifying a foreign key. The Foreign Key element can be used to just as easily create multi-column foreign keys: Example: Standard definition,
multi-column Primary Key
<table attributes...> <column name="column1" type="varChar" length="50"/> <column name="column2" type="varChar" length="30"/> other column declarations ... primary key declaration (optional) <foreignKey foreignTable="otherTable" onDelete="cascade"> <columnRef local="column1" foreign="otherTableColumn1"/> <columnRef local="column2" foreign="otherTableColumn2"/> other local columnRefs... </primaryKey> </table> If you don't specify a name attribute for the foreignKey, then DewDrop will conveniently make one for you based on the table name, for example: tablename_multicol1_fk. Shortcut Declaration (single column foreign key only): If you want, instead of using the standard declaration, you can conveniently specify if a column participates in single column foreign key by using the column's foreignKey attributes (tableRef, columnRef, onDelete, and onUpdate): Example: Shortcut method,
single column Foreign Key:
<column name="columnId" type="integer" tableRef="otherTable" columnRef="otherTableColumnId" onDelete="cascade"/> This acts exactly like the first Standard definition, single column Foreign Key example shown above. If specifying a foreign key this way, both the tableRef and columnRef attributes are required, while the onUpdate and onDelete attributes are optional. DewDrop will create the foreign key constraint trigger name for you based on the described naming convention. If you want to specify your own name, you'll have to use the <foreignKey> standard element declaration. Note: Multi-column foreign keys do not have a shortcut method. If you have a foreign key that is keyed off of multiple columns you'll have to explicitly declare the foreign key with the <foreignKey> element. <index>An index helps catalog a table's columns for efficient lookup when using the database. Indices are especially helpful for tables with potentially very large amounts of data. You do not have to specify an index on a table's primary key, as all primary keys are already indexed internally by a database. To be continued....this is an ongoing document that is still being written. Sorry for the inconvenience! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright © 2003 Les A. Hazlewood.
All rights reserved. |