The foreign key element

Foreign keys are critical in C-Phrase. Even if your database does not use them, they must be configured in the .cphrase file if you wish for the system to use joins in resulting queries.

Here we see an example:

<table id="City" lex="city,cities,town,towns">
    <column id="name" type="text" lex="name,names,named,called"/>
    <column id="state" type="text" lex="where,located in"/>
    <column id="population" type="int" lex="population,populations,..."/>

    <primary_key key="name,state"/>
    <foreign_key from="state" to="State" lex="located in,with,of"/>
    <primary-value key="name"/>
</table>

The DTD for the foreign_key element is:

<!ATTLIST foreign_key
          from CDATA #REQUIRED
          to CDATA #REQUIRED
          lex CDATA #REQUIRED
          description CDATA #IMPLIED>

from identifies the column origin of the foreign key.

to identifies what table the foreign key is pointing toward. Note this table may appear later in the .cphrase file without causing undefined reference problems.

lex associates comma separated phrases with the foreign key. An important convention is that the first phrase is the paraphrase associated with following the foreign key in the forward direction (e.g. "Cities located in states"), while the second phrase is associated with the following the foreign key in the inverse direction (e.g. "states with cities"). This is then followed by additional phrases in any order that could refer to the foreign key relationship (e.g. "of").

A current limitation in C-Phrase is that foreign keys are only single columns. Thus any referenced table must have a single column primary keys compatible with the type of the column from which the foreign key originates.