Fields are what hold the actual record data for each row in a table. Whereas a record could be considered a "row" in a table, a field could be considered a "column" in a table, or a "cell" in a record. The fields are defined at the time of the table creation.
Fields are added to the record definition in the order they are declared. In other words, the first field added is field number zero, the second added is field number one, etc. The FairCom DB API API also includes a set of functions that will allow a field to be inserted at a certain place in the records definition and fields can also be deleted from the record's definition.
Each field will have a field type (e.g., CT_INTEGER and CT_CHAR in the example below). For more information, see Field Types.
ctdbAddField() adds a new field to the end of the record definition.
/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, "Field0", CT_INTEGER, 4);
ctdbAddField(hTable, "Field1", CT_CHAR, 30);
/* create the table */
ctdbCreateTable(hTable, "MyTable", CTCREATE_NORMAL);
ctdbInsField() and ctdbInsFieldByName() each insert a new field before a previously-defined field. Use ctdbInsField() to specify the field index by its position in the record definition. The first field is number 0, the second field is number 1 and so on. Use ctdbInsFieldByName() to specify the field index by its name.
/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, "FirstField", CT_INTEGER, 4); /* This is field #0 */
ctdbAddField(hTable, "SecondField", CT_CHAR, 30); /* This is field #1 (for now!) */
/* insert a field into the location previously occupied by field "SecondField" */
ctdbInsFieldByName(hTable, "SecondField", "ThirdField", CT_BOOL, 1);
/* create the table */
ctdbCreateTable(hTable, "MyTable", CTCREATE_NORMAL);
The fields in the table are now as follows:
ctdbDelField() and ctdbDelFieldByName() delete a field from the record definition. Use ctdbDelField() to delete a field specifying the field number. The first field is number 0, the second field is number 1, and so on. Use ctdbDelFieldByName() to delete a field specifying the field name. Note that any fields with higher field numbers than the one you deleted will have one subtracted from their field numbers to fill the gap left by the field you deleted.
/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, "Field0", CT_INTEGER, 4);
ctdbAddField(hTable, "Field1", CT_CHAR, 30);
ctdbDelFieldByName(hTable, "Field1");
/* create the table with just one field ("Field0") */
ctdbCreateTable(hTable, "MyTable", CTCREATE_NORMAL);
See also: