14.3 Creating the Main--Detail database application
The database application that is based on a single database table, which we introduced earlier, can only manage the database tables simply, most of them are used only to browse the record information in a single database table, and if we want to browse the record information in multiple related database tables, we must create a primary-detail database application.
In the primary-detail database application, a database table is used as the primary table with comprehensive information, and other database tables are associated with the main database tables, which contain more detailed information. For example, when a database table customer.db as a primary table, it contains the customer's comprehensive information such as number, name, name of the company, and so on. The database table Orders.db contains detailed information about each customer's order, such as order number, date of shipment, date of shipment, shipping destination, etc. So when you view a customer in the Customer.db table, use the field Custno to contact the Orders.db table to automatically retrieve the details of all the orders that the customer has sent from the Orders.db table. The main-detail database embodies the characteristics of the relational database, that is, independent database tables are contacted based on their common fields. Here customer.db and orders.db have a common field of Custno.
The main 14.3.1 of a one-to-many relationship--Detail database application
There is a one-to-many relationship between the primary and the detail database tables, meaning that for a record in the main table, there are multiple records in the schedule that correspond to it. For example, create a primary-detail database application that includes two tables customer.db and orders.db, which act as primary tables and schedules, creating a good application as shown in Figure 14.5, the property settings for each part of the form
Table 14.2 Main--the attributes of each part in the application of detail type database
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Part Attribute Property value Comment
──────────────────────────────────
Table1 Active True
(primary table) DatabaseName Dbdemos
TableName CUSTOMER. Db
──────────────────────────────────
DataSource1 DataSet Table1
Autoedit False
──────────────────────────────────
Table2 Active True
Schedule DatabaseName Dbdemos
TableName ORDERS. Db
Indexfieldnames Custno The specified field Custno as
Indexed fields in Table2
Masterfield Custno Specifies a contact with the primary table
The fields
Mastersource DataSource1 Description of the connection to the primary table
Data source is DataSource
──────────────────────────────────
DataSource2 DataSet Table2
Autoedit False
──────────────────────────────────
DBGRID1 DataSource DataSource1
(corresponding primary table)
──────────────────────────────────
DBGrid2 DataSource DataSource2
(corresponding schedule)
TableName ORDERS. Db
──────────────────────────────────
DBNavigator1 DataSource DataSource1
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
A one-to-many relationship is a very common relationship. Even a simple name/address database has a one-to-many relationship because one person may have more than one address: Home address, work address, and possibly a villa address. In this example, a company customer often has multiple orders, and when we click the Forward and Backward buttons in the DBNavigator1, the record pointer in the DBGRID1 is moved, and the multiple records associated with the current record in Dbgridl are automatically displayed in DBGrid2. When a customer's information is displayed, the details of all of the customer's orders are also displayed.
Database application of 14.3.2-multi-relationship
We introduced the application of a One-to-many relationship based on two tables, and here's how to create a one-to-many relationship that browses data records from three tables.
For example, a customer may have more than one order form, and there are multiple order items in each order so that we can establish a major-detail relationship between the Customer.db table and the Orders.db table, while at orders.db A main-detail relationship is established between the table and the Items.db table.
The properties of each part in the form are shown in table 14.3
Table 14.3 Properties of each component in the application of one-to-many-Multiple relationships
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Part Attribute Property value Comment
──────────────────────────────────
Active True
Table1 DatabaseName Dbdemos
TableName CUSTOMER. Db
──────────────────────────────────
DataSource1 DataSet Table1
Autoedit False
──────────────────────────────────
Active True
DatabaseName Dbdemos
Table2 tablename ORDERS. Db
Indexfieldnames Custno
Masterfield Custno
Mastersource DataSource1
──────────────────────────────────
DataSource2 DataSet Table2
Autoedit False
──────────────────────────────────
Active True
DatabaseName Dbdemos
TABLE3 tablename ORDERS. Db
Indexfieldnames ORDERNO
Masterfield ORDERNO
Mastersource DataSource2
──────────────────────────────────
DataSource3 DataSet Table3
Autoedit False
──────────────────────────────────
DBGRID1 DataSource DataSource3
──────────────────────────────────
DBNavigator1 DataSource DataSource1
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The remaining parts of the form are tdbedit and Tlabel parts that display field values in customer.db and field values in Order.db. In this example, a total of three tables were connected, Customer. The DB table is the main table, the Orders.db table plays a dual role in the form, it is both a table of customer.db tables, but also the main Table items.db table, items.db table is a table of orders.db tables.