1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6    <title>Berkeley DB Concepts</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9    <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="introduction.html" title="Chapter��1.��Introduction to Berkeley DB" />
11    <link rel="prev" href="introduction.html" title="Chapter��1.��Introduction to Berkeley DB" />
12    <link rel="next" href="accessmethods.html" title="Access Methods" />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Berkeley DB Concepts</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="introduction.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��1.��Introduction to Berkeley DB </th>
23          <td width="20%" align="right">��<a accesskey="n" href="accessmethods.html">Next</a></td>
24        </tr>
25      </table>
26      <hr />
27    </div>
28    <div class="sect1" lang="en" xml:lang="en">
29      <div class="titlepage">
30        <div>
31          <div>
32            <h2 class="title" style="clear: both"><a id="concepts"></a>Berkeley DB Concepts</h2>
33          </div>
34        </div>
35      </div>
36      <p>
37        Before continuing, it is useful to describe some of the larger concepts
38        that you will encounter when building a DB application.
39    </p>
40      <p>
41        Conceptually, DB databases contain <span class="emphasis"><em>records</em></span>.
42        Logically each record represents a single entry in the database. 
43        Each such record contains two pieces of information: a key and a data.
44        This manual will on occasion describe a <span class="emphasis"><em>a record's
45        key</em></span> or a <span class="emphasis"><em>record's data</em></span> when it is
46        necessary to speak to one or the other portion of a database
47        record.
48    </p>
49      <p>
50        Because of the key/data pairing used for DB databases, they are
51        sometimes thought of as a two-column table.  However, data (and
52        sometimes keys, depending on the access method) can hold arbitrarily
53        complex data. Frequently, C structures and other such mechanisms are
54        stored in the record. This effectively turns a 2-column table
55        into a table with <span class="emphasis"><em>n</em></span> columns, where
56        <span class="emphasis"><em>n-1</em></span> of those columns are provided by the structure's
57        fields.
58    </p>
59      <p>
60        Note that a DB database is very much like a table in a relational
61        database system in that most DB applications use more than one 
62        database (just as most relational databases use more than one table). 
63    </p>
64      <p>
65        Unlike relational systems, however, a DB database contains a single 
66        collection of records organized according to a given access method 
67        (BTree, Queue, Hash, and so forth). In a relational database system,
68        the underlying access method is generally hidden from you. 
69     </p>
70      <p>
71        In any case, frequently DB
72        applications are designed so that a single database stores a specific 
73        type of data (just as in a relational database system, a single table
74        holds entries containing a specific set of fields). Because most applications 
75        are required to manage multiple kinds of data, a DB application will 
76        often use multiple databases.
77    </p>
78      <p>
79        For example, consider an accounting application. This kind of an
80        application may manage data based on bank accounts, checking
81        accounts, stocks, bonds, loans, and so forth. An accounting application
82        will also have to manage information about people, banking institutions,
83        customer accounts, and so on. In a traditional relational database, all
84        of these different kinds of information would be stored and managed
85        using a (probably very) complex series of tables. In a DB
86        application, all of this information would instead be divided out and 
87        managed using multiple databases.
88    </p>
89      <p>
90        DB applications can efficiently use multiple databases using an
91        optional mechanism called an <span class="emphasis"><em>environment</em></span>.
92        For more information, see <a class="xref" href="environments.html" title="Environments">Environments</a>.
93     </p>
94      <p>
95        You interact with most DB APIs using special structures that
96        contain pointers to functions. These callbacks are
97        called <span class="emphasis"><em>methods</em></span> because they look so much like a
98        method on a C++ class. The variable that you use to access these
99        methods is often referred to as a
100        <span class="emphasis"><em>handle</em></span>. For example, to use a database you will
101        obtain a handle to that database.
102     </p>
103      <p>
104        Retrieving a record from a database is sometimes called
105        <span class="emphasis"><em>getting the record</em></span> because the method that you use
106        to retrieve the records is called <code class="methodname">get()</code>.
107        Similarly, storing database records is sometimes called
108        <span class="emphasis"><em>putting the record</em></span> because you use the
109        <code class="methodname">put()</code> method to do this.
110     </p>
111      <p>
112        When you store, or put, a record to a database using its handle, the
113        record is stored according to whatever sort order is in use by the
114        database. Sorting is mostly performed based on the key, but sometimes
115        the data is considered too. If you put a record using a key that already
116        exists in the database, then the existing record is replaced with the
117        new data.  However, if the database supports
118        duplicate records (that is, records with identical keys but
119        different data), then that new record is stored as a duplicate record and
120        any existing records are not overwritten.
121     </p>
122      <p>
123        If a database supports duplicate records, then you can use a database
124        handle to retrieve only the first record in a set of duplicate records.
125     </p>
126      <p>
127        In addition to using a database handle, you can also read and write data using a
128        special mechanism called a <span class="emphasis"><em>cursor</em></span>. Cursors are
129        essentially iterators that you can use to walk over the records in a
130        database. You can use cursors to iterate over a database from the first
131        record to the last, and from the last to the first. You can also use
132        cursors to seek to a record. In the event that a database supports
133        duplicate records, cursors are the only way you can access all the
134        records in a set of duplicates.
135     </p>
136      <p>
137        Finally, DB provides a special kind of a database called a
138        <span class="emphasis"><em>secondary database</em></span>. Secondary databases serve as an
139        index into normal databases (called primary database to distinguish them
140        from secondaries). Secondary databases are interesting because DB
141        records can hold complex data types, but seeking to a given record is
142        performed only based on that record's key. If you wanted to be able to
143        seek to a record based on some piece of information that is not the key,
144        then you enable this through the use of secondary databases.
145     </p>
146    </div>
147    <div class="navfooter">
148      <hr />
149      <table width="100%" summary="Navigation footer">
150        <tr>
151          <td width="40%" align="left"><a accesskey="p" href="introduction.html">Prev</a>��</td>
152          <td width="20%" align="center">
153            <a accesskey="u" href="introduction.html">Up</a>
154          </td>
155          <td width="40%" align="right">��<a accesskey="n" href="accessmethods.html">Next</a></td>
156        </tr>
157        <tr>
158          <td width="40%" align="left" valign="top">Chapter��1.��Introduction to Berkeley DB ��</td>
159          <td width="20%" align="center">
160            <a accesskey="h" href="index.html">Home</a>
161          </td>
162          <td width="40%" align="right" valign="top">��Access Methods</td>
163        </tr>
164      </table>
165    </div>
166  </body>
167</html>
168