Working with Table Aliases

A table alias is the name that Visual FoxPro uses to refer to a table open in a work area. Visual FoxPro automatically uses the file name for the default alias when you open a table. For example, if you open the file Customer.dbf in work area 0 with the following commands, the default alias customer is automatically assigned to the table:

SELECT 0
USE customer

You can then use the alias customer to identify the table in a command or function. You can also create your own alias.

Creating a User-Defined Alias

You can assign your own user-defined alias to a table when you open it.

To open a table with a user-defined alias

  • Type USE with a table alias name.

For example, to open the file Customer.dbf in work area 0 and assign it an alias of people, use the following command:

SELECT 0
USE customer ALIAS people

You must then use the alias people to refer to the open table. An alias can consist of up to 254 letters, digits, or underscores and it must begin with a letter or an underscore. Visual FoxPro automatically creates an alias if the alias you provide contains an unsupported alias character.

Using a Visual FoxPro-Assigned Alias

Visual FoxPro automatically assigns an alias to a table in certain instances:

  • If you open a single table simultaneously in multiple work areas by including the AGAIN clause with the USE command and you don't specify an alias when opening the table in each work area.
  • If a conflict occurs with aliases.

The default aliases assigned in the first 10 work areas are the work area letters "A" through "J"; the aliases assigned in work areas 11 through 32767 are W11 through W32767. You can use these Visual FoxPro-assigned aliases just as you would any default or user-defined alias to refer to a table open in a work area.

Selecting a Work Area Using an Alias

You can move to a work area from another work area with the SELECT command. For example, if Customer.dbf is open in a work area and the default alias of CUSTOMER is assigned, you can move to this work area with the following SELECT command:

SELECT customer

Referring to Tables Open in Other Work Areas

You can also refer to fields in other work areas by prefacing the field name with the alias name and a period, or the –> operator. For example, if you're in a work area and you want to access the field contact from the Customer table open in a different work area, you could use the following to reference the field:

customer.contact

If the table you want to reference is opened with an alias, you can use the alias name. For example, if the Customer table is opened with the alias people, you can reference the lastname field with the following to refer to the field:

people.lastname

Using the table name or table alias specifically identifies the desired table independently from the work area in which the table is open.

See Also

Working with Multiple Tables | Setting Temporary Relationships Between Tables | Working with Records | Establishment of Persistent Relationships with Indexes | USE Command