Condividi tramite


Naming Fields

You specify field names as you build a new table. These field names can be 10 characters long for free tables or 128 characters long for database tables. If you remove a table from a database, the table's long field names are truncated to 10 characters.

To name a table field

For example, to create and open the table customer with three fields, cust_id, company, and contact, you could issue the following command:

CREATE TABLE customer (cust_id C(6), company C(40), contact C(30))

In the previous example, the C(6) signifies a field with Character data and a field width of 6. Choosing data types for your table fields is discussed later in this section.

Using the ALTER TABLE command, you add fields, company, and contact to an existing table customer:

ALTER TABLE customer ;
   ADD COLUMN (company C(40), contact C(30))

Using Short Field Names

When you create a table in a database, Visual FoxPro stores the long name for the table's fields in a record of the .dbc file. The first 10 characters of the long name are also stored in the .dbf file as the field name.

If the first 10 characters of the long field name are not unique to the table, Visual FoxPro generates a name that is the first n characters of the long name with the sequential number value appended to the end so that the field name is 10 characters. For example, these long field names are converted to the following 10-character names:

Long Name Short Name
customer_contact_name customer_c
customer_contact_address customer_2
customer_contact_city customer_3
... ...
customer_contact_fax customer11

While a table is associated with a database, you must use the long field names to refer to table fields. It is not possible to use the 10-character field names to refer to fields of a table in a database. If you remove a table from its database, the long names for the fields are lost and you must use the 10-character field names (stored in the .dbf) as the field names.

You can use long field names composed of characters, not numbers, in your index files. However, if you create an index using long field names and then remove the referenced table from the database, your index will not work. In this case, you can either shorten the names in the index and then rebuild the index; or delete the index and re-create it, using short field names. For information on deleting an index, see Deleting an Index.

The rules for creating long field names are the same as those for creating any Visual FoxPro identifier, except that the names can contain up to 128 characters.

For more information about naming Visual FoxPro identifiers, see Creating Visual FoxPro Names.

See Also

Field Creation | Choosing Data Types | Working with Tables | Table Designer | Creating Default Field Values | Restricting Access to Fields