JDB SQL Reference |
A database is a collection of information organized into different areas. Generally, a database covers information about a specific subject. For example, a company might have one database for personnel and another database for customer orders.
What sets a database system apart from other computer applications is that a structure exists which organizes the information. This structure allows each piece of information to be tagged. In some database systems, this structure is called the data model or database schema. Since there is a structure, the information stored in a database can be easily accessed for display to a screen or for printing in a report.
Structure of a Relational Database |
To describe the structure of JDB, you need to have a basic understanding of relational databases as a whole.
JDB, like other relational database management systems, organizes its information into tables, which consist of a row and column arrangement of data values. Generally, a database table contains a subset of related information about the main subject.
For example, if inventory is the main subject for your database, you might have tables for products, orders, and suppliers.
The two-tier sample application provided with Panther uses a JDB database called videobiz
. This database was designed for a video rental store, so it needs information about customers, video titles, and video rentals. Table 2-1 lists the database tables in the videobiz
database.
Each database table is divided into columns and rows. The columns are the various subcategories of the table, each containing a piece of the table information. The columns in the titles
table, described in Table 2-2, have information about a video title, such as the director, the type of video, and the running time.
When you start to enter data into your database tables, you will not always have a value for every column. In those cases, the value of the column is said to be NULL
. However, you must enter a value for your primary key columns and any column specified as NOT NULL
. Those columns cannot have null values.
Null values are used when the column value is unknown or unavailable. A null value is not synonymous with an entry of zero or with a blank.
When you insert information into a database table, you can enter a value for each column. Each entry is called a row. In some database systems, the equivalent of a column is called a field and the equivalent of a row is called a record.
Figure 2-1 The columns and rows associated with the titles table.
Each row of data in a JDB database table can be up to 1K. For more information, refer to page 6-12.
Every table in a well-designed relational database should have a primary key. A primary key is the column, or set of columns, that uniquely identifies a row. Because the rows of a relational database are unordered, the primary key definition lets you select a particular row of information.
JDB does not enforce unique entries for the primary key columns. So, while you will not receive an error, you should define the primary key columns when you create your database tables because:
JDB stores the primary key information. To define primary key information, choose which column or columns are the primary keys when you create your database tables. In the
In some database tables, a combination of two columns must make up the unique value. For example, the tapes table contains a No two rows of a table with a primary key are duplicated. Table 2-3 lists the columns associated with the tapes table.
If a column in a database table matches the primary key in another table, the column is referred to as a foreign key. Any value entered into a foreign key column must match a value previously entered into the primary key column in the other table. For example, the
In JDB, names for tables and columns are one-word descriptions consisting of letters, numbers and underscores. Names can be up to 31 characters in length. Tables names must be unique within the database. Column names must be unique within the table.
When you create columns in a table, you tell the database whether the data in the column are character strings, dates, or numbers. For numbers, you specify what type can be entered–integer or float, for example. For character strings, specify the maximum length. The maximum allowable length for a character string column is 255.
Notes:
Do not use JDB keywords as a name of a table or column; refer to page C-1 for a list of keywords.
titles
table, the primary key is the title_id
column, therefore, a unique value should be applied for each row in that column.
title_id
column, but a store can have several copies of a video title. Therefore, a combination of the title_id
and copy_num
columns must be used as the primary key of the tapes table. Every tape in the table is guaranteed to have a unique combination of data values in these two columns.
Foreign Keys
title_id
column is a foreign key in the tapes
table. Therefore, any value entered into the title_id
column of the tapes
table should already exist as a value in the titles
table.
Naming Database Tables and Columns
Designing Your Database |
The design process for building a database follows these general steps:
title_id
. title_id
column is found in several tables. This common data value in different tables provides the relationship needed to join tables together in order to combine the information.
In addition, you can also combine data from two tables into a single database table. For example, in the sample application, instead of having separate database tables, one for credit card codes and another for genre codes, the codes are combined into a single table of codes (called codes) used by the application. Entries into the columns code_type
, code, and dscr
describe and identify each specific code.
DELETE FROM
titlesWHERE
name = 'Henry V';
titles
table, the title_id
column is used to uniquely identify each row.
To uniquely identify each copy of a video tape, the tapes table uses the title_id
and copy_num
columns to make the primary key. The following statement affects all rows with a value of 1345 in the title_id
column:
DELETE FROM tapes WHERE title_id = 1345;
To change data about one copy of the tape, you need to list both the title_id
and the copy_num
columns in the WHERE
clause.
DELETE FROM tapes WHERE title_id = 1345 AND copy_num = 4;
Since information is stored in tables and the tables do not have any inherent relationship, it is possible to update a column in one table and not update the column in another table even if both columns correspond to the same value. To preserve the integrity of the database, it helps to chart the relationships between the tables. Then, if you update the information in one table, the chart illustrates if, and which, tables also need to be updated.
Figure 2-2 is a diagram that describes the videobiz database tables and their relationships to one another. Each table is represented in a box. The table name and primary key columns are listed in the top of each box. The lines between the boxes illustrate the foreign key definitions.
Figure 2-2 Diagram of the videobiz database.