EPUB | CHM | PDF

TMySQLTable.Methods.AddIndex

Top Previous Next

Creates a new index for the table.

Syntax:

procedure AddIndex(const Name,
                   Fields: String;
                  Options: TIndexOptions,
         const DescFields: String='');

Description:

Call AddIndex to create a new index for the already-existing table associated with a dataset component. The index created with this procedure is added to the database table underlying the dataset component.

Name is the name of the new index. Name must contain an index name that is valid for MySQL.

Fields is a String value containing the field or fields on which the new index will be based. If more than one field is used, separate the field names in the list with semi-colons.

Options is a set of attributes for the index. The Options parameter may contain any one, multiple, or none of the TIndexOptions constants: ixPrimary, xUnique, ixDescending, ixCaseInsensitive, and ixExpression.

DescFields is a string containing a list of field names, separated by semi-colons. The fields specified in DescFields are the fields in the new index for which the ordering will be descending. Fields in the index definition but not in the DescFields list use the default ascending ordering. It is possible that a single index can have fields using both ascending and descending ordering.

MySQLTable1.AddIndex('MostPaid',
                     'CustNo;SaleDate;AmountPaid',
                     [ixCaseInsensitive],
                     'SaleDate;AmountPaid');

Example:

In the example below, the AddIndex method is used to create an index named NewIndex. This index is based on two fields from the associated table, CustNo and CustName. The index NewIndex incorporates two index options through the TIndexOptions constants ixUnique and ixCaseInsensitive.

MySQLTable1.AddIndex('NewIndex',
                     'CustNo;CustName',
                     [ixUnique, ixCaseInsensitive]);

warning Attempting to create an index using options that are not applicable to the table type causes AddIndex to raise an exception.