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            <tt class="methodname">DB-&gt;del()</tt>
59            
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="c_index7"></a>
85      <pre class="programlisting">#include &lt;db.h&gt;
86#include &lt;string.h&gt;
87                                                                                                                                     
88...
89                                                                                                                                     
90DB *dbp, *sdbp;    /* Primary and secondary DB handles */
91DBT key;           /* DBTs used for the delete */
92int ret;           /* Function return value */
93char *search_name = "John Doe"; /* Name to delete */
94
95/* Primary */
96ret = db_create(&amp;dbp, NULL, 0);
97if (ret != 0) {
98  /* Error handling goes here */
99}
100                                                                                                                                     
101/* Secondary */
102ret = db_create(&amp;sdbp, NULL, 0);
103if (ret != 0) {
104  /* Error handling goes here */
105}
106
107/* Usually we want to support duplicates for secondary databases */
108ret = sdbp-&gt;set_flags(sdbp, DB_DUPSORT);
109if (ret != 0) {
110  /* Error handling goes here */
111}
112
113/* open the primary database */
114ret = dbp-&gt;open(dbp,        /* DB structure pointer */
115                NULL,       /* Transaction pointer */
116                "my_db.db", /* On-disk file that holds the database.
117                             * Required. */
118                NULL,       /* Optional logical database name */
119                DB_BTREE,   /* Database access method */
120                0,          /* Open flags */
121                0);         /* File mode (using defaults) */
122if (ret != 0) {
123  /* Error handling goes here */
124}
125
126/* open the secondary database */
127ret = sdbp-&gt;open(sdbp,          /* DB structure pointer */
128                 NULL,          /* Transaction pointer */
129                 "my_secdb.db", /* On-disk file that holds the database.
130                                 * Required. */
131                 NULL,          /* Optional logical database name */
132                 DB_BTREE,      /* Database access method */
133                 0,             /* Open flags */
134                 0);            /* File mode (using defaults) */
135if (ret != 0) {
136  /* Error handling goes here */
137}
138
139/* Now associate the secondary to the primary */
140dbp-&gt;associate(dbp,            /* Primary database */
141               NULL,           /* TXN id */
142               sdbp,           /* Secondary database */
143               get_sales_rep,  /* Callback used for key creation. */
144               0);             /* Flags */
145
146/*
147 * Zero out the DBT before using it.
148 */
149memset(&amp;key, 0, sizeof(DBT));
150
151key.data = search_name;
152key.size = strlen(search_name) + 1;
153
154/* Now delete the secondary record. This causes the associated primary
155 * record to be deleted. If any other secondary databases have secondary
156 * records referring to the deleted primary record, then those secondary
157 * records are also deleted.
158 */
159 sdbp-&gt;del(sdbp, NULL, &amp;key, 0); </pre>
160    </div>
161    <div class="navfooter">
162      <hr />
163      <table width="100%" summary="Navigation footer">
164        <tr>
165          <td width="40%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a> </td>
166          <td width="20%" align="center">
167            <a accesskey="u" href="indexes.html">Up</a>
168          </td>
169          <td width="40%" align="right"> <a accesskey="n" href="secondaryCursor.html">Next</a></td>
170        </tr>
171        <tr>
172          <td width="40%" align="left" valign="top">Reading Secondary Databases </td>
173          <td width="20%" align="center">
174            <a accesskey="h" href="index.html">Home</a>
175          </td>
176          <td width="40%" align="right" valign="top"177        
178        Using Cursors with Secondary Databases
179    </td>
180        </tr>
181      </table>
182    </div>
183  </body>
184</html>
185