I have found some tutorials, but they still leave me with questions.
Let's take a classic example of 2 tables, one for customer details and one for order details.
The customers
table in the database has:
- an autoincrementing integer
customer_id
as primary key - a text field for customer name
- a text field for contact details
And the orders
table has:
- an integer
customer_id
which is a foreign key referencing thecustomers
table - some other stuff, such a reference to a bunch of item numbers
- an integer
order_value
to store the cash value of the order
I need two dataset components, two queries and a connection.
So far, so good? Or did I miss something already?
Now, the tutorials say that I have to set the MasterSource of the of the datasource which coresponds to the DB grid showing the orders
table to be the datasource which corresponds to the customers
table and the MasterFields
, in this case, to customer_id
.
Anything else? Should I for instance set the Detailfields
of the query of the datasource which corresponds to the customers
table to customer_id
?
Should I use the properties, or a paramaterized query?
Ok, at this point, we have followed the classic tutorials and can scroll through the customers
DB grid and see all orders for the current customer shown in the orders
DB grid. When the user clicks the customers
DB grid I have to Close(); then Open(); the orders
query to refresh its corresponding DB grid.
However, those tutorials always seem to posit a static database with existing contents which never change.
When I asked anothter question, I gave an example where I was using a Command to INSERT INTO orders...
and was told that that is A Bad Thing` and I should:
- OrdersQuery.Append();
- OrdersQuery.FieldByName('customer_id') := [some value]';
- OrdersQuery.FieldByName('item_numbers') := [some value]';
- OrdersQuery.FieldByName('order_value') := [some value]';
- OrdersQuery.Post();
Is that correct?
I ask because it seems to me that a Command puts data in and a query should only take it out, but I can see that a command doesn't have linkage to the DB grid via a datasource's query.
Is this a matter of choice, or must the query be used?
If so, it seems that I can't use even simple SQL functions such as SUM, MIN< AVG, MAX in the query and have to move those into my code.
If I must use the query, how do I implement SQL UPDATE
and DROP
?
And, finally, can I have a Master/Detail/Detail relationship?
Let's say I want a 3rd DB grid, which shows the total and average of all orders for a customer. It gets its data from the orders
table (but can't use SUM and AVG) which is updated each time the user selects a different customer, thus giving a Master/Detail/Detail relationship. DO I just set that up as two Master/Detail relationships? I.E, the DB grid, datasource, query for the total and average orders refers only to orders
and has no reference to customers
, even if it does use customer_id
?
Thanks in advance for any help and clarification. I hope that this question will become a reference for others in the future (so, feel free to edit it).