Creating a table
Go Up to Working with Tables
You can create new database tables at design time or at runtime. The Create Table command (at design time) or the CreateTable
method (at runtime) provides a way to create tables without requiring SQL knowledge. They do, however, require you to be intimately familiar with the properties, events, and methods common to dataset components, TIBTable
in particular. This is so that you can first define the table you want to create by doing the following:
- Set the
Database
property to the database that will contain the new table. - Set the
TableName
property to the name of the new table. - Add field definitions to describe the fields in the new table. At design time, you can add the field definitions by double-clicking the
FieldDefs
property in the Object Inspector to bring up the collection editor. Use the collection editor to add, remove, or change the properties of the field definitions. At runtime, clear any existing field definitions and then use theAddFieldDef
method to add each new field definition. For each new field definition, set the properties of theTFieldDef
object to specify the desired attributes of the field. - Optionally, add index definitions that describe the desired indexes of the new table. At design time, you can add index definitions by double-clicking the
IndexDefs
property in the Object Inspector to bring up the collection editor. Use the collection editor to add, remove, or change the properties of the index definitions. At runtime, clear any existing index definitions, and then use theAddIndexDef
method to add each new index definition. For each new index definition, set the properties of theTIndexDef
object to specify the desired attributes of the index.
- Note: At design time, you can preload the field definitions and index definitions of an existing table into the
FieldDefs
andIndexDefs
properties, respectively. Set theDatabase
andTableName
properties to specify the existing table. Right click the table component and choose Update Table Definition. This automatically sets the values of theFieldDefs
andIndexDefs
properties to describe the fields and indexes of the existing table. Next, reset theDatabase
andTableName
to specify the table you want to create, cancelling any prompts to rename the existing table. If you want to store these definitions with the table component (for example, if your application will be using them to create tables on user’s systems), set theStoreDefs
property toTrue
.
Once the table is fully described, you are ready to create it. At design time, right-click the table component and choose Create Table. At runtime, call the CreateTable
method to generate the specified table.
- Important: If you create a table that duplicates the name of an existing table, the existing table and all its data are overwritten by the newly created table. The old table and its data cannot be recovered.
The following code creates a new table at runtime and associates it with the employee.gdb
database.
Before it creates the new table, it verifies that the table name provided does not match the name of an existing table:
var NewTable: TIBTable; NewIndexOptions: TIndexOptions; TableFound: Boolean; begin NewTable := TIBTable.Create; NewIndexOptions := [ixPrimary, ixUnique]; with NewTable do begin Active := False; Database := 'employee.gdb'; TableName := Edit1.Text; TableType := ttDefault; FieldDefs.Clear; FieldDefs.Add(Edit2.Text, ftInteger, 0, False); FieldDefs.Add(Edit3.Text, ftInteger, 0, False); IndexDefs.Clear; IndexDefs.Add('PrimaryIndex’, Edit2.Text, NewIndexOptions); end; {Now check for prior existence of this table} TableFound := FindTable(Edit1.Text); {code for FindTable not shown} if TableFound = True then if MessageDlg('Overwrite existing table ' + Edit1.Text + '?', mtConfirmation, mbYesNo, 0) = mrYes then TableFound := False; if not TableFound then CreateTable; { create the table} end; end;