Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is AddIndexDef about?

Status
Not open for further replies.

cold25

Programmer
Feb 11, 2002
41
US
The code below displays how I'm currently making new database (.dbf) tabes to use and display in my TDBGrid.
It creates tables for me, however I don't understand the lower section concerning IndexDefs, where I'm supposed to describe indexes. I found this code in Delphi's Help and have modified it as best I could, so it would work.
Could someone please give me the gist on what indexes are for (are they used to uniquely identify specific rows in the table I create?), what problems may arise from not having them, and how (if needed) I may create the indexes to ensure corrupt tables are not created from not having them? Any help would be appreciated.

Thanks,
cold25


with Table1 do begin
Active := False;
DatabaseName := 'DBDEMOS';
TableType := ttDBase;
Table1.TableName := newName;

{ Don't overwrite an existing table }
if not Table1.Exists then begin
{ The Table component must not be active }
{ First, describe the type of table and give }
{ it a name }
{ Next, describe the fields in the table }
with FieldDefs do begin
Clear;
with AddFieldDef do begin
Name := 'FeatNum';
DataType := ftString;
Size := 10;
end;
with AddFieldDef do begin
Name := 'CODE';
DataType := ftString;
Size := 10;
end;
with AddFieldDef do begin
Name := 'ABC';
DataType := ftString;
Size := 10;
end;
with AddFieldDef do begin
Name := 'DEF';
DataType := ftString;
Size := 10;
end;
with AddFieldDef do begin
Name := 'GHI';
DataType := ftInteger;
end;
end; //end of with FieldDefs do begin

{ Next, describe any indexes }
with IndexDefs do begin
Clear;
{ The 1st index has no name because it is
{ a Paradox primary key }//I'm not working with paradox, this was a comment left from Delphi's example.
with AddIndexDef do begin
Name := '';
Fields := 'Field1';
Options := [ixPrimary];
end;
with AddIndexDef do begin

Name := 'Fld2Indx';
Fields := 'Field2';
Options := [ixCaseInsensitive];
end;
end;
{ Call the CreateTable method to create the table }
CreateTable;
end; //end of if not Table1.Exists then begin
end; //end of with Table1 do begin
 
You can set a index on a table to ensure unique records.
If you for example have a table with the following layout:

nr name street phone e-mail
001 John Philstreet 45878998 j@wol.com
002 Jim High street 45856665 js@wol.com

and you put a index on the field 'nr' then every recrod that is inserted into the table must have a value in the field 'nr' that is unique. So you can never have two records starting with "001". This is called a primary Index.
Indexes are also very helpfull if you want a to sort your table very quick on a specific field. You can for example put an index on the field "name" which means that every record that is inserted to the table is automaticly ordened by the name field.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top