Product Documentation

c-treeDB API API for C

Previous Topic

Next Topic

Creating a table under transaction control

You can add an extra level of data integrity when creating a new table by placing the code to create the table inside a transaction. If the transaction is aborted, the table entry in the database dictionary file is removed, and the table data and index files are deleted from disk.

When a table is created inside a transaction, and until the transaction is committed or aborted, the newly-created table must be opened with CTOPEN_EXCLUSIVE mode, otherwise the table open operation will fail. After the transaction is committed, the table can be opened in non-exclusive mode.

The code fragment below creates a new table under transaction control. Again no error checking code is included in the example:

/* allocate a new table handle */

hTable = ctdbAllocTable(hDatabase);

/* begin a transaction */


/* add a field

ctdbAddField(hTable, "Field0", CT_INTEGER, 4);

/* add another field */

ctdbAddField(hTable, "Field1", CT_CHAR, 30);

/* create the table */

ctdbCreateTable(hTable, "MyTable", CTCREATE_NORMAL);

/* commit the transaction */


Note: If you open a table that was created inside the current transaction (that is, the transaction has not yet been committed or aborted), you must open the table in exclusive mode by specifying the CTOPEN_EXCLUSIVE mode to ctdbOpenTable().