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.73.2" />
9    <link rel="start" href="index.html" title="Getting Started with Berkeley DB" />
10    <link rel="up" href="indexes.html" title="Chapter��5.��Secondary Databases" />
11    <link rel="prev" href="readSecondary.html" title="Reading Secondary Databases" />
12    <link rel="next" href="secondaryCursor.html" title="Using Cursors with Secondary Databases" />
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>
36      <p>
37        In general, you 
38             
39            <span>will</span> 
40        not modify a secondary database directly. In
41        order to modify a secondary database, you should modify the primary
42        database and simply allow DB to manage the secondary modifications for you.
43    </p>
44      <p>
45        However, as a convenience, you can delete 
46            
47            <span>secondary database</span>
48        records directly. Doing so causes the associated primary key/data pair to be deleted.
49        This in turn causes DB to delete all 
50            
51            <span>secondary database</span>
52        records that reference the primary record.
53    </p>
54      <p>
55        You can use the 
56            
57            <code class="methodname">DB-&gt;del()</code>
58            
59        method to delete a secondary database record. 
60        
61        
62
63        <span>Note that if your
64            <span>secondary database</span> 
65             
66        contains duplicate records, then deleting a record from the set of
67        duplicates causes all of the duplicates to be deleted as well.
68        </span>
69
70    </p>
71      <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
72        <h3 class="title">Note</h3>
73        <p>
74      
75      <span>
76        You can delete a secondary database record using the previously
77        described mechanism
78      </span>
79      only if the primary database is opened for write access.
80      </p>
81      </div>
82      <p>For example:</p>
83      <a id="c_index7"></a>
84      <pre class="programlisting">#include &lt;db.h&gt;
85#include &lt;string.h&gt;
86                                                                                                                                     
87...
88                                                                                                                                     
89DB *dbp, *sdbp;    /* Primary and secondary DB handles */
90DBT key;           /* DBTs used for the delete */
91int ret;           /* Function return value */
92char *search_name = "John Doe"; /* Name to delete */
93
94/* Primary */
95ret = db_create(&amp;dbp, NULL, 0);
96if (ret != 0) {
97  /* Error handling goes here */
98}
99                                                                                                                                     
100/* Secondary */
101ret = db_create(&amp;sdbp, NULL, 0);
102if (ret != 0) {
103  /* Error handling goes here */
104}
105
106/* Usually we want to support duplicates for secondary databases */
107ret = sdbp-&gt;set_flags(sdbp, DB_DUPSORT);
108if (ret != 0) {
109  /* Error handling goes here */
110}
111
112/* open the primary database */
113ret = dbp-&gt;open(dbp,        /* DB structure pointer */
114                NULL,       /* Transaction pointer */
115                "my_db.db", /* On-disk file that holds the database.
116                             * Required. */
117                NULL,       /* Optional logical database name */
118                DB_BTREE,   /* Database access method */
119                0,          /* Open flags */
120                0);         /* File mode (using defaults) */
121if (ret != 0) {
122  /* Error handling goes here */
123}
124
125/* open the secondary database */
126ret = sdbp-&gt;open(sdbp,          /* DB structure pointer */
127                 NULL,          /* Transaction pointer */
128                 "my_secdb.db", /* On-disk file that holds the database.
129                                 * Required. */
130                 NULL,          /* Optional logical database name */
131                 DB_BTREE,      /* Database access method */
132                 0,             /* Open flags */
133                 0);            /* File mode (using defaults) */
134if (ret != 0) {
135  /* Error handling goes here */
136}
137
138/* Now associate the secondary to the primary */
139dbp-&gt;associate(dbp,            /* Primary database */
140               NULL,           /* TXN id */
141               sdbp,           /* Secondary database */
142               get_sales_rep,  /* Callback used for key creation. */
143               0);             /* Flags */
144
145/*
146 * Zero out the DBT before using it.
147 */
148memset(&amp;key, 0, sizeof(DBT));
149
150key.data = search_name;
151key.size = strlen(search_name) + 1;
152
153/* Now delete the secondary record. This causes the associated primary
154 * record to be deleted. If any other secondary databases have secondary
155 * records referring to the deleted primary record, then those secondary
156 * records are also deleted.
157 */
158 sdbp-&gt;del(sdbp, NULL, &amp;key, 0); </pre>
159    </div>
160    <div class="navfooter">
161      <hr />
162      <table width="100%" summary="Navigation footer">
163        <tr>
164          <td width="40%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a>��</td>
165          <td width="20%" align="center">
166            <a accesskey="u" href="indexes.html">Up</a>
167          </td>
168          <td width="40%" align="right">��<a accesskey="n" href="secondaryCursor.html">Next</a></td>
169        </tr>
170        <tr>
171          <td width="40%" align="left" valign="top">Reading Secondary Databases��</td>
172          <td width="20%" align="center">
173            <a accesskey="h" href="index.html">Home</a>
174          </td>
175          <td width="40%" align="right" valign="top">��
176        
177        <span>Using Cursors with Secondary Databases</span>
178    </td>
179        </tr>
180      </table>
181    </div>
182  </body>
183</html>
184