Condividi tramite


Preventing Duplicate Values in Fields

You can prevent duplicate values in one or more table fields by creating a primary or candidate index using an expression that contains those fields. When you include one or more fields in an index expression, Visual FoxPro generates an index key based on those fields for each record that uniquely identifies the record. When a new record is entered, you can compare index keys for the new record and the existing records to determine whether a duplicate record already exists and prevent the duplicate record from being added to the table.

Tip

If your table is part of a database, use a primary or candidate index. If your table is a free table or already has a primary index, you must use a candidate index.

For example, suppose you have a table that stores the area code and phone number in two columns. The following table illustrates these two columns.

Area code

Phone number

206

444-nnnn

206

555-nnnn

313

444-nnnn

Both the area code column and phone number column contain values that duplicate other values in the respective columns. However, no phone numbers are duplicated because combining both fields creates a row containing the complete phone number. Likewise, if the primary or candidate index included both fields in the index expression, the rows in the example would not be considered duplicates. Therefore, if you attempted to enter a phone number that contained exactly the same area code and phone number as exists in one of the rows, Visual FoxPro would reject the entry as a duplicate.

See Also

Concepts

Index Creation Based on Expressions

Other Resources

Working with Table Indexes

Working with Fields

Working with Tables (Visual FoxPro)