Microsoft KB Archive/130601

= How to Create a Primary Index in a Visual FoxPro Program =

Article ID: 130601

Article Last Modified on 2/11/2000

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q130601



SUMMARY
Microsoft Visual FoxPro has a new index type, Primary. The Primary index, however, cannot be created in a program by using the INDEX command. This article shows by example how to add or modify a Primary index in a program.



MORE INFORMATION
A Primary index is an index that never permits duplicate values on the specified field or expression. The Primary index may be established in the database container interface or in a program by using the CREATE TABLE or ALTER TABLE command.

A table with a Primary index must be part of a database and cannot be a free table. The first example shows how to create a table with a Primary index. The second example shows how to alter an existing table to change a Primary key. The third example adds a Primary index to an existing table.

Example 1 - Create a New Table with a Primary Index
 Create a new database, or open an existing one.  Issue the following command in the Command window to create a table with a Primary index on the cSsn key field: CREATE TABLE employee (cSsn C(11) PRIMARY KEY, cLast C(20),;     cFirst C(20)) 

Example 2 - Modify an Existing Table to change a Primary Index
 Open the database used in example 1.  Issue the following command from the Command window to move the Primary index from the cSsn field to the cLast field: ALTER TABLE employee DROP PRIMARY KEY ADD PRIMARY KEY cLast TAG cLast 

Example 3 - Modify an Existing Table to add a Primary Index
  Add a new table to an existing database with this command: CREATE TABLE noprime (cLast C(20)) </li>  From the Command window, add a Primary index to the existing table by using one of the following commands: ALTER TABLE noprime ADD PRIMARY KEY cLast TAG cLast

-or-

ALTER TABLE noprime ALTER COLUMN cLast C(20) PRIMARY KEY </li></ol>

Additional query words: VFoxWin

Keywords: kbcode KB130601

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.