Alter Table
We waited for this command for years. Finally, starting in VFP 3, we could change the structure of a table without resorting to all kinds of tricks. ALTER TABLE lets you add and remove fields, change a field, or change defaults and rules. ALTER TABLE works not only on tables, but also on cursors built with CREATE CURSOR, though some clauses aren't relevant for cursors.
Usage |
ALTER TABLE TableName ADD | ALTER [ COLUMN ] FieldName FieldType [ ( nFieldWidth [ , nDecimals ] ) ] [ NULL | NOT NULL ] [ CHECK lFieldRule [ ERROR cFieldRuleMessage ] ] [ DEFAULT uDefaultExpression ] [ PRIMARY KEY | UNIQUE ] [ REFERENCES ReferencedTable [ TAG ReferencedTag ] ] [ NOCPTRANS ] [ NOVALIDATE ] ALTER TABLE TableName ALTER [ COLUMN ] FieldName [ NULL | NOT NULL ] [ SET DEFAULT uDefaultExpression] [ SET CHECK lFieldRule [ ERROR cFieldRuleMessage ] ] [ DROP DEFAULT ] [ DROP CHECK ] [ NOVALIDATE ] ALTER TABLE TableName [ DROP [ COLUMN ] FieldName ] [ SET CHECK lTableRule [ ERROR cTableRuleMessage ] ] [ DROP CHECK ] [ ADD PRIMARY KEY uPrimaryKeyExpression [ [ FOR lPrimaryKeyFilter ] TAG PrimaryKeyTag ] ] [ DROP PRIMARY KEY ] [ ADD UNIQUE uUniqueKeyExpression [ FOR lUniqueKeyFilter ] [ TAG UniqueKeyTag1 ] ] [ DROP UNIQUE TAG UniqueKeyTag2 ] [ ADD FOREIGN KEY [ uForeignKeyExpression ] [ FOR lForeignKeyFilter ] TAG ForeignKeyTag1 REFERENCES ReferencedTable [ TAG ReferencedTag ] ] [ DROP FOREIGN KEY TAG ForeignKeyTag2 [ SAVE ] ] [ RENAME COLUMN OldFieldName TO NewFieldName ] [ NOVALIDATE ] |
Parameter |
Value |
Meaning |
FieldName |
Name |
The name of the field in the table to be added, changed or deleted. Long names can be used for tables in a database. |
FieldType |
Single character |
The type to use for the field. Valid types are: C – Character |
nFieldWidth |
Numeric |
The width of the field. For many types, the width is fixed and nFieldWidth should be omitted. For Double fields, nFieldWidth must be omitted. |
nDecimals |
Numeric |
The number of decimal places for the field. Relevant only for some of the numeric types. For Double fields, nDecimals may be included, even though nFieldWidth is omitted. For example, ALTER TABLE MyTable ADD COLUMN MyNewField B(3) creates a Double field with three decimal places. |
lFieldRule |
Logical |
An expression that provides the field-level rule. Can call a function. |
cFieldRuleMessage |
Character |
The error message to display when the field rule is violated. |
uDefaultExpression |
Same type as specified by FieldType |
An expression that provides a default value for the field. |
ReferencedTable |
Name |
The name of the table for which the field or expression is a foreign key. A regular index tag is created for this field or expression. |
ReferencedTag |
Name |
The name of the tag in ReferencedTable to which the field or expression refers. |
Omitted |
The field or expression refers to the primary key in ReferencedTable. |
|
lTableRule |
Logical |
An expression that provides the table-level rule. Functions can be called. In VFP 3, table-level rules may not change the data in the table. |
cTableRuleMessage |
Character |
The error message to display when the table-level rule is violated. |
uPrimaryKeyExpression |
Any type but Memo, General, or Picture |
An expression that forms the primary key for this table. |
PrimaryKeyTag |
Name |
The name to assign the tag created for the primary key. |
lPrimaryKeyFilter |
Logical |
An expression that filters the records in the primary key index. Only records for which the expression is true are included in the index. Don't do this—see below for the reason. |
uUniqueKeyExpression |
Any type but Memo, General, or Picture |
An expression that forms a candidate key for this table. |
UniqueKeyTag1 |
Name |
The name to assign the candidate key tag created. |
lUniqueKeyFilter |
Logical |
An expression that filters the records in the candidate key index. Only records for which the expression is true are included in the index. Don't do this either, although we don't feel quite as strongly about this one. |
UniqueKeyTag2 |
Name |
The name of a candidate key to be deleted. |
uForeignKeyExpression |
Any type but Memo, General, or Picture |
An expression that is a foreign key into another table. A regular index tag is created for this expression. |
ForeignKeyTag1 |
Name |
The name of the tag to be created for uForeignKeyExpression. |
lForeignKeyFilter |
Logical |
An expression that filters the records in the foreign key index. Only records for which the expression is true are included in the index. Need we say it again—don't do this. |
ForeignKeyTag2 |
Name |
The name of the tag that is part of a persistent relation to be removed. If SAVE is included, the tag is retained—only the relation is removed. If SAVE is omitted, the tag is also deleted. |
OldFieldName |
Name |
The name of a field to be renamed. |
NewFieldName |
Name |
The new name of the field. |
You'll get weird errors and unexpected behavior if you try to remove both the default and rule for a field in the same statement; the error and behavior depend on the order in which you use the DROP clauses. DROP DEFAULT DROP CHECK results in either a "no rule to drop" error if there's no table-level rule, or removes the table-level rule if there was one. (Clearly, VFP is confusing the second and third forms of the command in this case.) DROP CHECK DROP DEFAULT results in a "variable 'default' is not found" error. Using these clauses in separate commands works fine. |
The order of the FOR and TAG clauses is important: You'll get a "syntax error" if you try to use TAG before FOR. |
Example |
OPEN DATABASE Testing CREATE TABLE Test1 (cId C(3), cName c(20) UNIQUE) CREATE TABLE Test2 (cTest1Id C(3), dDate D) ALTER TABLE Test1 ADD COLUMN nTotal N(4) ALTER TABLE Test1 ALTER COLUMN cId C(3) PRIMARY KEY ; DEFAULT GetId("Test1") ALTER TABLE Test1 ALTER COLUMN cName SET CHECK NOT EMPTY(cName) ALTER TABLE Test1 DROP UNIQUE TAG cName ALTER TABLE Test2 ADD FOREIGN KEY cTest1Id TAG cTest1Id ; REFERENCES Test1 |
See Also |
AfterAddRelation, AfterDropRelation, AfterModifyTable, BeforeAddRelation, BeforeDropRelation, BeforeModifyTable, Create Cursor, Create Table, Index, Modify Structure, Set Safety |