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>Deleting Secondary Database Records</title>
7    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
9    <link rel="home" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="indexes.html" title="Chapter��5.��Secondary Databases" />
11    <link rel="previous" href="readSecondary.html" title="Reading Secondary Databases" />
12    <link rel="next" href="secondaryCursor.html" title="&#10;        &#10;        Using Cursors with Secondary Databases&#10;    " />
13  </head>
14  <body>
15    <div class="navheader">
16      <table width="100%" summary="Navigation header">
17        <tr>
18          <th colspan="3" align="center">Deleting Secondary Database Records</th>
19        </tr>
20        <tr>
21          <td width="20%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a>��</td>
22          <th width="60%" align="center">Chapter��5.��Secondary Databases</th>
23          <td width="20%" align="right">��<a accesskey="n" href="secondaryCursor.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="secondaryDelete"></a>Deleting Secondary Database Records</h2>
33          </div>
34        </div>
35        <div></div>
36      </div>
37      <p>
38        In general, you 
39             
40            <span>will</span> 
41        not modify a secondary database directly. In
42        order to modify a secondary database, you should modify the primary
43        database and simply allow DB to manage the secondary modifications for you.
44    </p>
45      <p>
46        However, as a convenience, you can delete 
47            
48            <span>secondary database</span>
49        records directly. Doing so causes the associated primary key/data pair to be deleted.
50        This in turn causes DB to delete all 
51            
52            <span>secondary database</span>
53        records that reference the primary record.
54    </p>
55      <p>
56        You can use the 
57            
58            
59            <tt class="methodname">Db::del()</tt>
60        method to delete a secondary database record. 
61        
62        
63
64        <span>Note that if your
65            <span>secondary database</span> 
66             
67        contains duplicate records, then deleting a record from the set of
68        duplicates causes all of the duplicates to be deleted as well.
69        </span>
70
71    </p>
72      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
73        <h3 class="title">Note</h3>
74        <p>
75      
76      <span>
77        You can delete a secondary database record using the previously
78        described mechanism
79      </span>
80      only if the primary database is opened for write access.
81      </p>
82      </div>
83      <p>For example:</p>
84      <a id="cxx_index7"></a>
85      <pre class="programlisting">#include &lt;db_cxx.h&gt;
86#include &lt;string.h&gt;
87
88...
89                                                                                                                                     
90Db my_database(NULL, 0); // Primary
91Db my_index(NULL, 0);    // Secondary
92
93// Open the primary
94my_database.open(NULL,       // Transaction pointer
95                 "my_db.db", // On-disk file that holds the database.
96                NULL,        // Optional logical database name
97                DB_BTREE,    // Database access method
98                DB_CREATE,   // Open flags
99                0);          // File mode (using defaults)
100
101// Setup the secondary to use sorted duplicates.
102// This is often desireable for secondary databases.
103my_index.set_flags(DB_DUPSORT);
104
105// Open the secondary
106my_index.open(NULL,              // Transaction pointer
107              "my_secondary.db", // On-disk file that holds the database.
108              NULL,              // Optional logical database name
109              DB_BTREE,          // Database access method
110              DB_CREATE,         // Open flags.
111              0);                // File mode (using defaults)
112
113
114// Now associate the primary and the secondary
115my_database.associate(NULL,          // Txn id
116                      &amp;my_index,     // Associated secondary database
117                      get_sales_rep, // Callback used for key extraction.
118                      0);            // Flags 
119
120// Name to delete
121char *search_name = "John Doe";
122
123// Get a search key
124Dbt key(search_name, strlen(search_name) + 1);
125                      
126// Now delete the secondary record. This causes the associated primary
127// record to be deleted. If any other secondary databases have secondary
128// records referring to the deleted primary record, then those secondary
129// records are also deleted.
130my_index.del(NULL, &amp;key, 0); </pre>
131    </div>
132    <div class="navfooter">
133      <hr />
134      <table width="100%" summary="Navigation footer">
135        <tr>
136          <td width="40%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a>��</td>
137          <td width="20%" align="center">
138            <a accesskey="u" href="indexes.html">Up</a>
139          </td>
140          <td width="40%" align="right">��<a accesskey="n" href="secondaryCursor.html">Next</a></td>
141        </tr>
142        <tr>
143          <td width="40%" align="left" valign="top">Reading Secondary Databases��</td>
144          <td width="20%" align="center">
145            <a accesskey="h" href="index.html">Home</a>
146          </td>
147          <td width="40%" align="right" valign="top">��
148        
149        Using Cursors with Secondary Databases
150    </td>
151        </tr>
152      </table>
153    </div>
154  </body>
155</html>
156