As discussed in the Data Dictionary Section, the Data Dictionary is NexusDB's native object structure for storing structural information. This is the most direct, powerful and efficient way to create tables in NexusDB. Here is an example of creating a "Products" table:

procedure TDataModuleMain.CreateProductsTable(aNexusDB: TnxDatabase);

var

 ProductsDict: TnxDataDictionary;

 NewIndex: TnxIndexDescriptor;

 FieldDesc: TnxFieldDescriptor;

 KeyFieldDesc: TnxKeyFieldDescriptor;

begin

 ProductsDict := TnxDataDictionary.Create;

 try

   ProductsDict.FieldsDescriptor.AddField('PRODUCTID', '', nxtAutoInc, 0, 0, True);

   ProductsDict.FieldsDescriptor.AddField('NAME', '', nxtNullString, 64, 0, True);

   ProductsDict.FieldsDescriptor.AddField('RETAILPRICE', '', nxtCurrency, 0, 0, True);

   ProductsDict.FieldsDescriptor.AddField('WHOLESALEPRICE', '', nxtCurrency, 0, 0, True);

   ProductsDict.FieldsDescriptor.AddField('MANUFACTURERID', '', nxtWord32, 0, 0, True);

   ProductsDict.FieldsDescriptor.AddField('PRODUCTNUMBER', '', nxtNullString, 64, 0, False);

   ProductsDict.FieldsDescriptor.AddField('DESCRIPTION', '', nxtBlobMemo, 0, 0, False);

   ProductsDict.FieldsDescriptor.AddField('THUMBNAIL', '', nxtBlobGraphic, 0, 0, False);

   ProductsDict.FieldsDescriptor.AddField('PRODUCTIMAGE', '', nxtBlobGraphic, 0, 0, False);

   FieldDesc := ProductsDict.FieldsDescriptor.AddField('CREATED', '', nxtDateTime, 0, 0, True);

   FieldDesc.AddDefaultValue(TnxCurrentDateTimeDefaultValueDescriptor);

   ProductsDict.FieldsDescriptor.AddField('MODIFIED', '', nxtDateTime, 0, 0, True);

   NewIndex := ProductsDict.IndicesDescriptor.AddIndex('PK_PRODUCTID', 0, False,

     'Primary Key of Products Table', TnxCompKeyDescriptor);

   TnxCompKeyDescriptor(NewIndex.KeyDescriptor).Add(

     ProductsDict.GetFieldFromName('PRODUCTID'));

   NewIndex := ProductsDict.IndicesDescriptor.AddIndex('I_NAME', 0, True,

     'Name Index', TnxCompKeyDescriptor);

   KeyFieldDesc := TnxCompKeyDescriptor(NewIndex.KeyDescriptor).

     Add(ProductsDict.GetFieldFromName('NAME'), TnxTextKeyFieldDescriptor);

   TnxTextKeyFieldDescriptor(KeyFieldDesc).IgnoreCase := True;

   NewIndex := ProductsDict.IndicesDescriptor.AddIndex('I_MANUFACTURERID', 0, True,

     'Manufacturer Index', TnxCompKeyDescriptor);

   TnxCompKeyDescriptor(NewIndex.KeyDescriptor).Add(ProductsDict.GetFieldFromName('MANUFACTURERID'));

   NewIndex := ProductsDict.IndicesDescriptor.AddIndex('I_PRODUCTNUMBER', 0, True,

     'Product Number Index', TnxCompKeyDescriptor);

   TnxCompKeyDescriptor(NewIndex.KeyDescriptor).Add(

     ProductsDict.GetFieldFromName('PRODUCTNUMBER'));

   aNexusDB.CreateTable(true, 'PRODUCTS', '', ProductsDict);

 finally

   FreeAndNil(ProductsDict);

 end;

end;

Home | Site Contents | Documentation | NexusDB Manual V4 | Delphi Guide | Code Examples & Fragments | How to create Tables?