Next: , Previous: Allowed Fields, Up: Constraints on Record Sets


7.4 Keys and Unique Fields

The %unique and %key special fields are used to avoid several instances of the same field in a record, and to implement keys in record sets. Their usage is:

     %unique: field1 field2 ... fieldN
     %key: field

The field names are separated by one or more blank characters.

Normally it is permitted for a record to contain two or more fields of the same name. The %unique special field revokes this permissiveness. A field declared “unique” cannot appear more than once in a single record.

For example, an entry in an address book database could contain an Age field. It does not make sense for a single person to be of several ages. So, a field could be declared as “unique” in the corresponding record descriptor as follows:

     %rec: Contact
     %mandatory: Name
     %unique: Age

Several %unique fields can appear in the same record descriptor. The set of unique fields is the union of all the entries.

%key makes the referenced field the primary key of the record set. The primary key behaves as if both %unique and %mandatory had been specified for that field. Additionally, there is further restriction, viz: a given value of a primary key field may appear no more than once within a record set.

Consider for example a database of items in stock. Each item is identified by a numerical Id field. No item may have more than one Id, and no items may exist without an associated Id. Additionally, no two items may share the same Id. This common situation can be implementing by declaring Id as the key in the record descriptor:

     %rec: Item
     %key: Id
     %mandatory: Title
     
     Id: 1
     Title: Box
     
     Id: 2
     Title: Sticker big

It would not make sense to have several primary keys in a record set. Thus, it is not allowed to have several %key fields in the same record descriptor. It is also forbidden for two items to share the same `Id' value. Both of these situations would be data integrity violations, and will be reported by a checking tool.

Elsewhere, we discuss how primary keys can be used to link one record set to another using primary keys together with foreign keys. See Queries which Join Records.