Search Java Programs

Tuesday, February 23, 2010

Understanding the Relational Database Model

While the relational database model did not appear in commercial products until the 1980s, Dr. Edgar F. Codd of IBM defined the relational database in 1970. The relational model simplifies database structures by eliminating the explicit parent/child relationship pointers. In a relational database, all data is organized into tables. The hierarchical and network database records are represented by table rows, record fields (or attributes) are represented as table columns, and pointers to parent and child records are eliminated.
A relational database can still represent parent/child relationships, it just does it based on data values in its tables. For example, you can represent the complex Network database model data shown in Figure 4.2 of Tip 4 with the tables shown in Figure 5.1

Customer Table
CUST_NO
LAST_NAME
FIRST_NAME
10
FIELDS
SALLY
11
CLEAVER
WARD
Salesman Table
SALESMAN_NO
LAST_NAME
FIRST_NAME
5
KING
KAREN
6
HARDY
ROBERT
Product Table
PRODUCT_NO
DESCRIPTION
SALES_PRICE
INV_COUNT
7
100 WATT SPEAKER
75.00
25
8
DVD PLAYER
90.00
15
9
AMPLIFIER
450.00
305
10
RECEIVER
750.00
25
11
REMOTE CONTROL
25.00
15
12
50 DVD PACK
500.00
25
Order Table
ORDER NO
DEL DATE
PRODUCT_NO
SALESMAN_NO
CUST_NO
101
01/15/2000
7
5
10
102
01/22/2000
12
5
11
103
03/15/2000
7
6
10
104
04/05/2000
12
7
12
105
07/05/2000
9
6
11
106
08/09/2000
7
8
11

Figure 5.1: ORDER_ TABLE with relationships to three other tables
 
In place of pointers, the relational database model uses common columns to establish the relationship between tables. For example, looking at Figure 5.1, you will see that the CUSTOMER, SALESMAN, and PRODUCT tables are not related because they have no columns in common. However, the ORDER table is related to the CUSTOMER table by the CUSTOMER_NO column. As such, an ORDER table row is related to a CUSTOMER table row where the CUSTOMER_NO column has the same value in both tables. Figure 5.1 shows CUSTOMER 10 owns ORDER 101 and ORDER 103 since the CUST_NO column for these two ORDER rows is equal to the CUSTOMER_NO column in the CUSTOMER table. The SALESMAN and PRODUCT tables are related to the ORDER table in the same manner. The SALESMAN table is related to the ORDER table by the common SALESMAN_NO column, and the PRODUCT table is related to the ORDER table by the PRODUCT_NO column.

As you may have noticed from the discussion of the hierarchical model in Tip 3, "Understanding the Hierarchical Database Model," and the network model in Tip 4, applications written to extract data from either of these models had the database structures "hard-coded" into them. To navigate the records in the network model, for example, the programmer had to know what pointers were available and what type of data existed at each level of the database tree structure. Knowing the names of the pointers let the programmer move up, down, or across the database tree; knowing what data was available at each level of the tree told the programmer the direction in which to move. Because record relationships based on pointers are hard-coded into the application, adding a new level to the tree structure requires that you change the application program's logic. Even just adding a new attribute to a network database record changes the location of the pointer within the record changes. As a result, any changes to a database record require that the application programs accessing the database be recompiled or rewritten.

When working with a relational database, you can add tables and add columns to tables to create new relationships to the new tables without recompiling existing applications. The only time you need to recompile an application is if you delete or change a column used by that program. Thus, if you want to relate an entry in the SALESMAN table to a customer (in the CUSTOMER table), you need only add a SALESMAN_NO column to the CUSTOMER table.

 



No comments:

Post a Comment

Website Design by Mayuri Multimedia