CTTable::Alter() scans the table, field, index, and segment structures to decide which changes need to be made and how to do it. At the very least, it may only update the table DODA if the only change done was, for example, in field types that are compatible with each other: changing from types CT_INT4 and CT_INT4U. Then, if the only changes occurred in a single index: a single index was added or deleted or the index properties changed, only that index is rebuilt. If more than one index changed, or more than one index was added or deleted, then it may be necessary to rebuild all indexes of the table. If fields were added, deleted, inserted, or the changes in the field property were not compatible with each other, then Alter needs to perform a full rebuild of the table.
A table is rebuilt by creating a temporary table with the correct current properties taking in consideration all changes. All records are read from the original table and written into the temporary table. Once all data records have been moved, the original table is deleted and the temporary table is renamed with the name of the original table.
// add one field to the table and rebuild it
CTTable ATable(ADatabase);
// open the table
ATable.Open("MyTable", CTOPEN_NORMAL);
// add one field to the table
ATable.AddField("Wages", CT_CURRENCY, 8);
// alter the table
try
{
ATable.Alter(CTDB_ALTER_NORMAL);
}
catch (CTException &err)
{
printf("Alter table failed with error %d\n", err.GetErrorCode());
}
CTTable::Alter() method take as parameter an alter table action parameter:
Action |
Value |
Explanation |
---|---|---|
CTDB_ALTER_NORMAL |
0 |
Check for table changes before altering the table and perform only the changes required. |
CTDB_ALTER_INDEX |
1 |
Force rebuild of all indexes, regardless of table changes. |
CTDB_ALTER_FULL |
3 |
Force full table rebuild, regardless of table changes. |
CTDB_ALTER_PURGEDUP |
4096 |
Purge duplicate records |
CTDB_ALTER_TRUNCATE |
8192 |
Quickly remove all records |