Skip to main content

Syntax of Foreign Keys in Class Definitions

Describes the structure of a foreign key definition. See Foreign Key Definitions for links to general information on defining foreign keys.

Introduction

A foreign key defines a referential integrity constraint. When a table containing a foreign key constraint is modified, the foreign key constraints are checked.

You can add foreign key definitions to persistent classes. They are not meaningful in other kinds of classes.

Note that you can also enforce reference integrity by defining relationship properties that connect the classes.

Details

A foreign key definition has the following structure:

/// description 
ForeignKey name(key_props) References referenced_class(ref_index) [ keyword_list ]; 

Where:

  • description (optional) is intended for display in the Class Reference. The description is blank by default. See Creating Class Documentation.

  • name (required) is the name of the foreign key. This must be a valid class member name, and must not conflict with any other class member names.

  • key_props (required) specifies the property or properties that are constrained by this foreign key. Specifically this property or properties must match the referenced value in the foreign table.

    This is a comma-separated list of property names.

    These properties must be in the same class that defines the foreign key.

  • referenced_class (required) specifies the foreign table (that is, the class to which the foreign key points).

  • ref_index (optional) specifies the unique index name within referenced_class.

    If you omit ref_index, then the system uses the IDKEY index in referenced_class.

  • keyword_list (optional) is a comma-separated list of keywords that further define the foreign key.

    See Foreign Key Syntax and Keywords for a complete keyword list.

    If this list is omitted, also omit the square brackets.

Example

ForeignKey EmpKey(EmpId) References MyApp.Employee(EmpIDX) [ OnDelete = cascade ];

In this example, EmpIDX is the unique index name in the class MyApp.Employee.

See Also

FeedbackOpens in a new tab