Powered by SmartDoc

primary

The extention syntax primary specifies a PRIMARY constraint at a target column.

Target

A target of the primary attribute is the PRIMARY KEY constraint property.

Parameters

The primary attribute takes one of the following values as a parameter:

true
The PRIMARY KEY constraint is available.
false
The PRIMARY KEY constraint is not available.

Artifact

If true, the PRIMARY KEY constraint is specified. When only one property has the primary attribute with a value of true, the PRIMARY KEY column constraint is used. When more than two properties has the primary attributes with a value of true, the PRIMARY KEY table constraint is used.

Another feature for Relaxer table objects in the UPDATE facility. If true, the UPDATE facility is added to Relaxer table objects.

Example

List 13.3.4.1[primary.rng] is a sample schema for the primary attribute.

primary.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         xmlns:sql="http://www.relaxer.org/xmlns/relaxer/sql"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <ref name="account"/>
  </start>
  <define name="account">
    <element name="account">
      <attribute name="accountNo" sql:primary="true">
        <data type="token"/>
      </attribute>
      <element name="balance">
        <data type="int"/>
      </element>
      <element name="owner">
        <data type="token"/>
      </element>
      <ref name="address"/>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

In the schema, the extension attribute sql:primary is specified in the attribute element named accountNo. This definition means that this column is a primary key of the table.

Execution of Relaxer with the primary attribute is as follows. No special options are required.

$ relaxer -jdbc primary.rng

As a result, Relaxer generates 13 files:

The file account.ddl, in List 13.3.4.2[account.ddl], shows the effect of the primary attribute. A column accountNo will have the PRIMARY KEY constraint.

account.ddl
CREATE TABLE "account" (
	"accountNo" VARCHAR(32) PRIMARY KEY,
	"balance" INTEGER NOT NULL,
	"owner" VARCHAR(32) NOT NULL,
	"address" VARCHAR(128) NOT NULL,
	"address_zip" VARCHAR(32) NOT NULL,
	"phone" VARCHAR(512)
)

Example - table constraint

List 13.3.5.1[primary.rng] is a sample schema for the multiple primary attributes.

primary.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         xmlns:sql="http://www.relaxer.org/xmlns/relaxer/sql"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <ref name="account"/>
  </start>
  <define name="account">
    <element name="account">
      <attribute name="accountNo" sql:primary="true">
        <data type="token"/>
      </attribute>
      <element name="balance">
        <data type="int"/>
      </element>
      <element name="owner" sql:primary="true">
        <data type="token"/>
      </element>
      <ref name="address"/>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

In the schema, the two extension attribute sql:primary are specified in the attribute element named accountNo and owner. This definition means that these columns compose a primary key of the table.

Execution of Relaxer with the primary attribute is as follows. No special options are required.

$ relaxer -jdbc primary.rng

As a result, Relaxer generates 13 files:

The file account.ddl, in List 13.3.5.2[account.ddl], shows the effect of the primary attribute. A table constraint PRIMARY KEY shows that a composite of column accountNo and column owner is the primary key.

account.ddl
CREATE TABLE "account" (
	"accountNo" VARCHAR(32) NOT NULL,
	"balance" INTEGER NOT NULL,
	"owner" VARCHAR(32) NOT NULL,
	"address" VARCHAR(128) NOT NULL,
	"address_zip" VARCHAR(32) NOT NULL,
	"phone" VARCHAR(512),
	PRIMARY KEY (accountNo, owner)
)