EasyTable: Single-File Embedded Database,
a BDE replacement for Delphi and C++Builder



Creating a table
Previous  Top  Next


Introduction

Creating tables is accomplished through the CreateTable method of the TEasyTable component. The properties used by the CreateTable method include the FieldDefs, IndexDefs, DatabaseName, DatabaseFileName, TableName and Exists properties.

Specifying the Fields to Create

The FieldDefs property is used to specify which fields to define for the new table. The FieldDefs property is an array of TFieldDef objects, each of which contains information about the field to create. You may add new TFieldDef objects using the Add method of the TFieldDefs object stored in the FieldDefs property. The Add method accepts the following parameters for the field being defined:

Field Name (String)
Field Name parameter indicates the name to give the field.    
Data Type (TFieldType)
DataType parameter indicates the data type of the field Available TFieldType data types
Size (Word)
Size parameter indicates the size of the field. This should be specified for the String type only. For all other data types this parameter should be set as 0. For the String type this parameter indicates the length of the field. For the WideString type this parameter indicates the size of the wide string in bytes.
Required (Boolean)
Required parameter indicates whether or not the new field should be required (not Null) while adding or modifying records.

          
If AutoIndexes property is True then ascending indexes for each field are automatically created.


Specifying the Indexes to Create

The IndexDefs property is used to specify which indexes to be defined for the new table. The IndexDefs property is an array of TIndexDef objects, each of them containing information about the index to create. You may add new TIndexDef objects using the Add method of the TIndexDefs object contained in the IndexDefs property. The Add method accepts the following parameters for the index being defined:

Index Name (String)
Index Name parameter contains the name to be given to the index.
Fields List (String)
Fields List parameter contains the list of fields to be included into the index. Multiple field names specified in this parameter should be separated with a semicolon (;).
Index Options (TIndexOptions)
Index Options parameter provides information about the type of index being created (please see the component reference supplied with EasyTable for more information on the available TIndexOption options).

         
         
         

Setting the Table Information

The DatabaseName / DatabaseFileName and TableName properties are used to specify the name and database for the created table. The DatabaseName / DatabaseFileName specifies an existing database where the new table will be created. The TableName property specifies the name for the created table.

Note: If you want to ensure that database exists or create it, take a look at Creating a database topic.

Creating the Table

Any table can have a primary key on fields of any type. You may create this key with the index by means [ixPrimary] option.
The final step in creating a table is to call the CreateTable method. It is also recommended to check the Exists property of the TEasyTable component first to make sure that you are not attempting to overwrite an existing table. The following example shows how to create the CUSTOMER table included with the Delphi demo in existing database 'CustBase.edb' using the CreateTable method:

begin
with MyEasyDatabase do
begin
DatabaseName:='CustBase';
DatabaseFileName:='c:.edb';
if not Exists then
CreateDatabase;
end;

with MyEasyTable do
begin
DatabaseName:='CustBase';
TableName:='customer';
with FieldDefs do
begin
Clear;
Add('CustNo',ftAutoInc,0,False);
Add('Company',ftString,30,False);
Add('Addr1',ftString,30,False);
Add('Addr2',ftString,30,False);
Add('City',ftString,15,False);
Add('State',ftString,20,False);
Add('Zip',ftString,10,False);
Add('Country',ftString,20,False);
Add('Phone',ftString,15,False);
Add('FAX',ftString,15,False);
Add('TaxRate',ftFloat,0,False);
Add('Contact',ftString,20,False);
Add('LastInvoiceDate',ftDateTime,0,False);
end;
with IndexDefs do
begin
Clear;
Add('PrimaryKey','CustNo',[ixPrimary]);
Add('ByCompany','Company',[ixCaseInsensitive]);
end;
if not Exists then
CreateTable;
end;
end;


© AidAim Software EasyTable: Easytable Ben Delphi Database Single File Bde Replacemen