Java Tutorial - Java Script : Relational Databases

Java Tutorial - Java Script :

Relational Databases


Relational database theory is based on the mathematics of relational algebra. A relational database can be thought of as a collection of tables. Each table has a set of columns and rows. Each row within a table shares the same set of columns as all of the other rows within that table. The columns define the type of data that can be found within the corresponding column of a row. The rows themselves represent the individual records of information within the database. Table 8.1 below illustrates these points. This might represent a table in a database supporting the Old Friends Incorporated Reunion application that we described in Chapter 3. Each column is associated with a name and a data type. Columns like FIRSTNAME and LASTNAME are character based, VISIT_COUNT is numeric, and LAST_VISIT represents a date Each record in a table should be unique. In the example, we use the USER_KEY column as the unique identifier. (In this case, we could have also used the LOGIN_ID column because it too should be unique). This unique identifier is known as the primary key. The primary key uniquely identifies a specific individual row in the table. Any specific individual record or row of a table can be selected using its primary key.

Table 8.2 illustrates a table that might be used to track login history. It uses VISIT_KEY as a primary key. It also has a column for the USER_KEY. In this case, USER_KEY is a foreign key. By using the primary key from the user table and the foreign key of the history table a relationship can be established between the two tables.

 In this case, we have a one-to-many relationship. Each user may have many visits, but each visit is associated with exactly one user. In this example, the user identified with the USER_KEY value of 2 (Jane Dough) has logged into the application twice, most recently on March 31. This example demonstrates a simple one-to-many relationship between two tables. Relational databases also support one-to-one and many-to-many relationships between tables.

By using a third table that has a separate one-tomany relationship with each of two other tables a many-to-many relationship can be created between the two tables. Although the proof is beyond the scope of this text, it can be demonstrated that relations between tables can be used to describe any arbitrarily complex structuring of data.