When a database is first created, the index file contains only a physical index. The physical index allows records to be accessed in the physical order in which they are stored in the database. Since the physical index supports only sequential access, we need to create additional indexes if we want to randomly access the database records.
The imkindex function is called to make the additional indexes. It requires three arguments, the database handle, the name of the new index, and the names of the record fields that will comprise the index. The field names must match those passed to the icreate_db function when the database was created. The fields that comprise an index are often called key fields.
The following example program indexes the BOOKS database by the author name and ISBN number. The author index is arbitrarily named "Author_Index". It is comprised of the two record fields, "Author_Last" and "Author_First". The last name field is listed before the first name field so that the records will be sorted first by last name, then by first name. The ISBN index is arbitrarily named "ISBN_Index,U". It is comprised of the single record field named "ISBN". The ",U" appended to "ISBN_Index" identifies this index as being unique. This means that no two records will be allowed to store the same value in the "ISBN" field.
Notice that the imkindex function returns I_ERROR if an error occurs while attempting to make the new index. A value of I_OK is returned if the index is successfully created.
When a new index is created, the appropriate fields from each existing record are automatically stored in the new index. Likewise, each time a new record is added to the database, the appropriate fields of the new record are automatically stored in each existing index.