td.Fields['ID'].Set_Attributes(dbAutoIncrField);
//DELPHI 支持的DAO数据格式
td.Fields.Append(td.CreateField ('dbBoolean',dbBoolean,0));
td.Fields.Append(td.CreateField ('dbByte',dbByte,0));
td.Fields.Append(td.CreateField ('dbInteger',dbInteger,0));
td.Fields.Append(td.CreateField ('dbLong',dbLong,0));
td.Fields.Append(td.CreateField ('dbCurrency',dbCurrency,0));
td.Fields.Append(td.CreateField ('dbSingle',dbSingle,0));
td.Fields.Append(td.CreateField ('dbDouble',dbDouble,0));
td.Fields.Append(td.CreateField ('dbDate',dbDate,0));
td.Fields.Append(td.CreateField ('dbBinary',dbBinary,0));
td.Fields.Append(td.CreateField ('dbText',dbText,0));
td.Fields.Append(td.CreateField ('dbLongBinary',dbLongBinary,0));
td.Fields.Append(td.CreateField ('dbMemo',dbMemo,0));
动态建表?
var
dbname: string;
db: Database;
td: TableDef;
idx: Index;
rel: Relation;
begin
if InputQuery ('Database name', 'Enter database name', dbname) then
begin
if __DBEngine35 = nil then CreateEngine (Dao35, '');
db := __DBEngine35.CreateDatabase (ExtractFilePath (Application.ExeName) + dbname, ';LANGID=0x0409;CP=1252;COUNTRY=0', 0);
td := db.CreateTableDef ('Orders', 0, '', '');
td.Fields.Append (td.CreateField ('Order_ID', dbInteger, 0));
td.Fields.Append (td.CreateField ('Order_DATE', dbDate, 0));
td.Fields.Append (td.CreateField ('Order_CLIENT', dbText, 50));
td.Fields.Append (td.CreateField ('Order_TOTAL', dbCurrency, 0));
idx := td.CreateIndex ('Primary');
idx.Primary := True;
idx.Fields.Append (idx.CreateField ('Order_ID', dbInteger, 0));
td.Indexes.Append (idx);
idx := td.CreateIndex ('By_Client');
idx.Fields.Append (idx.CreateField ('Order_CLIENT', dbText, 50));
td.Indexes.Append (idx);
db.TableDefs.Append (td);
td := db.CreateTableDef ('Items', 0, '', '');
td.Fields.Append (td.CreateField ('Item_ID', dbInteger, 0));
td.Fields.Append (td.CreateField ('Order_ID', dbInteger, 0));
td.Fields.Append (td.CreateField ('Item_NAME', dbText, 40));
td.Fields.Append (td.CreateField ('Item_COUNT', dbInteger, 0));
td.Fields.Append (td.CreateField ('Item_PRICE', dbCurrency, 0));
idx := td.CreateIndex ('Primary');
idx.Primary := True;
idx.Fields.Append (idx.CreateField ('Item_ID', dbInteger, 0));
td.Indexes.Append (idx);
idx := td.CreateIndex ('By_Order');
idx.Fields.Append (idx.CreateField ('Order_ID', dbInteger, 0));
td.Indexes.Append (idx);
db.TableDefs.Append (td);
rel := db.CreateRelation ('ItemsOrders', 'Orders', 'Items', dbRelationUpdateCascade);
rel.Fields.Append (rel.CreateField ('Order_ID', dbInteger, 0));
rel.Fields ['Order_ID'].ForeignName := 'Order_ID';
db.Relations.Append (rel);
db.Close;
end;
end;