Purpose
Alters the structure of a table. Cannot be used on linked tables. You can issue this statement from the MapBasic window in MapInfo Pro.
Syntax
Alter Table table (
[ Add columnname columntype [ , ...] ]
[ Modify columnname columntype [ , ...] ]
[ Drop columnname [ , ...] ]
[ Rename oldcolumnname newcolumnname [ , ...] ]
[ Order columnname, columnname [ ,...] ] )
[ Interactive ]
table is the name of an open table.
columnname is the name of a column; column names can be up to 31 characters long, and can contain letters, numbers, and the underscore character, and column names cannot begin with numbers.
columntype indicates the datatype of a table column (including the field width if necessary).
oldcolumnname represents the previous name of a column to be renamed.
newcolumnname represents the intended new name of a column to be renamed.
Description
The Alter Table statement lets you modify the structure of an open table, allowing you to add columns, change column widths or datatypes, drop (delete) columns, rename columns, and change column ordering.
Each columntype should be one of the following: integer, SmallInt, float, decimal( size, decplaces ), char(size), date, or logical, DateTime.
By including an Add clause in an Alter Table statement, you can add new columns to your table. By including a Modify clause, you can change the datatypes of existing columns. A Drop clause lets you delete columns, while a Rename clause lets you change the names of existing columns. The Order clause lets you specify the order of the columns. Altogether, an Alter Table statement can have up to five clauses. Note that each of these five clauses can operate on a list of columns; thus, with a single Alter Table statement, you can make all of the structural changes that you need to make (see example below).
The Order clause affects the order of the columns, not the order of rows in the table. Column order dictates the relative positions of the columns when you browse the table; the first column appears at the left edge of a Browser window, and the last column appears at the right edge. Similarly, a table's first column appears at the top of an Info Tool window.
If a MapBasic application issues an Alter Table statement affecting a table which has memo fields, the memo fields will be lost. No warning will be displayed.
An Alter Table statement may cause map layers to be removed from a Map window, possibly causing the loss of themes or cosmetic objects. If you include the Interactive keyword, MapInfo Pro prompts the user to save themes and/or cosmetic objects (if themes or cosmetic objects are about to be lost).
Example
In the following example, we have a hypothetical table, "gcpop.tab" which contains the following columns: pop_88, metsize, fipscode, and utmcode. The Alter Table statement below makes several changes to the gcpop table. First, a Rename clause changes the name of the pop_88 column to population. Then the Drop clause deletes the metsize, fipscode, and utmcode columns. An Add clause creates two new columns: a small (2-byte) integer column called schoolcode, and a floating point column called federalaid. Finally, an Order clause specifies the order for the new set of columns: the schoolcode column comes first, followed by the population column, etc.
Open Table "gcpop"
Alter Table gcpop
(Rename pop_88 population
Drop metsize, fipscode, utmcode
Add schoolcode SmallInt, federalaid Float
Order schoolcode, population, federalaid)
See Also:
Add Column statement, Create Index statement, Create Map statement, Create Table statement