Sunteți pe pagina 1din 4

Gdfgdfgdfgdfgdfgdfgdfgdfgdf

Gdsfsdfsdfsdfsdfsdf
F
Ds
Fdsfsdf
Sdf
Sd
F
Sdf
Sd
F
Sd
F
Sdf
Sd
Fsd
F}
SDh
Df
Gjhfg
J
fgH
sd}FG
aes-wd.gkm,sdgv.-<xsz
gb.aerkm,hfdbaster-detai reati"#shi$s %D& a#d De$hi
Master-detail data relationships are a fact of life for every Delphi database developer;
just as data relationships are a fundamental feature of relational databases.
A one-to-many relationship, often referred to as a "master-detail" or "parent-child"
relationship, is the most usual relationship between two tables in a database.
Common scenarios include customerpurchase data, patientmedical-record data, and
studentcourse-result data. !or e"ample, each customer is associated with at least one
order record. #alued customers have many order records involvin$ si$nificant sums
and often a user needs to view one in connection with the other. %n a one-to-many
relationship, a record in &able A can have 'none or one or( more than one matchin$
record in &able ), but for every record in &able ) there is e"actly one record in &able
A.
A typical master-detail data browsin$ form displays the results of a one-to-many
relationship, where one D)*rid displays 'or set of data enabled controls( the results of
the first or master table. %t then trac+s a selection in the first D)*rid to filter the
results of a second table used to display the details of the selection in the second
D)*rid.
,hen wor+in$ with the )D- and Delphi, the simplest way to assemble a master-detail
form is to use the Database !orm ,i.ard. ,i.ard simplifies the steps needed to create
a tabular or data-entry form by use of an e"istin$ database, unfortunately it is
desi$ned to use the )D- versions of &&able and &/uery components. -verythin$ the
wi.ard does, we can do by hand.
0ince, throu$h this course, we are wor+in$ with the AD1-"press set of Delphi
components, we2ll need to set all the components step by step. !irstly we have to
ma+e sure that we have two tables in a master-detail relationship.
A one-to-many relationship, often referred to as a "master-detail" or "parent-child"
relationship, is the most usual relationship between two tables in a database.
Common scenarios include customerpurchase data, patientmedical-record data, and
studentcourse-result data. !or e"ample, each customer is associated with at least one
order record. #alued customers have many order records involvin$ si$nificant sums
and often a user needs to view one in connection with the other. %n a one-to-many
relationship, a record in &able A can have 'none or one or( more than one matchin$
record in &able ), but for every record in &able ) there is e"actly one record in &able
A.
A typical master-detail data browsin$ form displays the results of a one-to-many
relationship, where one D)*rid displays 'or set of data enabled controls( the results of
the first or master table. %t then trac+s a selection in the first D)*rid to filter the
results of a second table used to display the details of the selection in the second
D)*rid.
,hen wor+in$ with the )D- and Delphi, the simplest way to assemble a master-detail
form is to use the Database !orm ,i.ard. ,i.ard simplifies the steps needed to create
a tabular or data-entry form by use of an e"istin$ database, unfortunately it is
desi$ned to use the )D- versions of &&able and &/uery components. -verythin$ the
wi.ard does, we can do by hand.
0ince, throu$h this course, we are wor+in$ with the AD1-"press set of Delphi
components, we2ll need to set all the components step by step. !irstly we have to
ma+e sure that we have two tables in a master-detail relationship.
Setti#g '$ (aster)Detai with %D&*x$ress
Creatin$ a master-detail data form is not to much complicated. 3ave an empty Delphi
form, and just follow the steps4
5. 0elect the AD1 pa$e on the Component palette. Add two &AD1&able components
and one &AD1Connection to a form.
6. 0elect the Data Access pa$e on the Component palette. Add two &Data0ource
components to a form.
7. 0elect Data Controls pa$e on the Component palette. 8lace two &Db*rid
components on a form. Add two D)9avi$ator components, too.
:. ;se the AD1Connection, the Connection0trin$ property, to lin+ to the
aboutdelphi.bdb M0 Access database, as e"plained in the first chapter of this course.
<. Connect D)*rid5 with Data0ource5, and Data0ource5 with AD1&able5. &his will be
the master table. Connect D)9avi$ator5 with Data0ource5.
=. Connect D)*rid6 with Data0ource6, and Data0ource6 with AD1&able6. &his will be
the detail table. Connect D)9avi$ator6 with Data0ource6.
>. 0et AD1&able5.&able9ame to point to the Customers table 'master(.
?. 0et AD1&able6.&able9ame to point to the 1rders table 'detail(.
%f you, at this moment, set the Active property of both AD1&able
components to true, you2ll notice that the entire 1rders table is displayed
- this is because we haven2t set up the master-detail relationship yet.
@our form should loo+ somethin$ li+e4
MasterSource and MasterFields
&he Master0ource and Master!ields properties of the &AD1&able component define
master-detail relationships in DelphiAD1 database applications.
&o create a master-detail relationships with Delphi, you simply need to set the detail
table2s Master0ource property to the Data0ource of the master table and its
Master!ields property to the chosen +ey field in the master table.
%n our case, first, set AD1&able6.Master0ource to be Data0ource5. 0econd, activate
the !ield Ain+ Desi$ner window to set the Master!ields property4 in the Detail !ields list
bo" and the Master !ields list bo" select the Cust9o field. Clic+ Add and 1B.
&hese properties +eep both tables in synchroni.ation, so as you move throu$h the
Customers table, the 1rders table will only move to records which match the +ey field
'Cust9o( in the Customers table.
-ach time you hi$hli$ht a row and select a new customer, the second $rid displays only
the orders pertainin$ to that customer.
,hen you delete a record in a master table - all the correspondin$ record in the detail
table are deleted. ,hen you chan$e a lin+ed field in a record in a master table - the
correspondin$ field in the detail table $ets chan$ed to 'in as many records as needed(.
0imple as thatC
St"$. 9ote that creatin$ a master-detail form with Delphi is not enou$h to support
referential inte$rity features on two tables. -ven thou$h we can use methods described
here to display two tables in a parent-child relation; if those two tables are not lin+ed
'one-to-many( within M0 Access - cascadin$ updates and deletes won2t ta+e place if
you try to delete or update the "master" record.
%D& Sha$i#g
0haped recordsets are an alternative to master-detail relationships. )e$innin$ with
AD1 6.D, this method is available. 0haped recordsets allow the developer to retrieve
data in a hierarchical fashion. &he shaped recordset adds a special "field" that is
actually a recordset unto itself. -ssentially, data shapin$ $ives you the ability to build
hierarchical recordsets. !or instance, a typical hierarchical recordset mi$ht consist of a
parent recordset with several fields, one of which mi$ht be another recordset.
!or an e"ample of the 03A8- command ta+e a loo+ at the shapedemo project that
shiped with Delphi 'in the DemosEAdo directory(. @ou must specify the shapin$
provider in your connection strin$, by addin$ 8roviderFM0Data0hape; to the
be$innin$.
03A8- Gselect H from customerI
A88-9D 'Gselect H from ordersI A0 1rders
J-AA&- Cust9o &1 Cust9o(
Althou$h it ta+es some time to master the 03A8- command that2s used to create these
Kueries, it can result in si$nificantly smaller resultsets. Data shapin$ reduces the
amount of traffic crossin$ a networ+, provides more fle"ibility when usin$ a$$re$ate
functions, and reduces overhead when interfacin$ with leadin$-ed$e tools li+e LMA.

S-ar putea să vă placă și