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��10.��Secondary Databases" />
11    <link rel="previous" href="readSecondary.html" title="Reading Secondary Databases" />
12    <link rel="next" href="secondaryCursor.html" title="&#10;        Using Secondary Cursors&#10;        &#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��10.��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            <tt class="classname">SecondaryDatabase</tt>
48            
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            <tt class="classname">SecondaryDatabase</tt>
52            
53        records that reference the primary record.
54    </p>
55      <p>
56        You can use the 
57            <tt class="methodname">SecondaryDatabase.delete()</tt>
58            
59            
60        method to delete a secondary database record. 
61        
62        
63
64        <span>Note that if your
65             
66            <tt class="classname">SecondaryDatabase</tt> 
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      <span><tt class="methodname">SecondaryDatabase.delete()</tt> causes the
76      previously described delete operations to occur
77      </span>
78      
79      only if the primary database is opened for write access.
80      </p>
81      </div>
82      <p>For example:</p>
83      <a id="java_index7"></a>
84      <pre class="programlisting">package db.GettingStarted;
85
86import com.sleepycat.db.DatabaseEntry;
87import com.sleepycat.db.DatabaseException;
88import com.sleepycat.db.OperationStatus;
89import com.sleepycat.db.SecondaryDatabase;
90
91...
92try {
93    SecondaryDatabase mySecondaryDatabase = null;
94    // Omitting all database opens
95    ...
96
97    String searchName = "John Doe";
98    DatabaseEntry searchKey = 
99        new DatabaseEntry(searchName.getBytes("UTF-8"));
100
101    // Delete the first secondary record that uses "John Doe" as
102    // a key. This causes the primary record referenced by this secondary
103    // record to be deleted.
104    OperationStatus retVal = mySecondaryDatabase.delete(null, searchKey);
105} catch (Exception e) {
106    // Exception handling goes here
107}</pre>
108    </div>
109    <div class="navfooter">
110      <hr />
111      <table width="100%" summary="Navigation footer">
112        <tr>
113          <td width="40%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a>��</td>
114          <td width="20%" align="center">
115            <a accesskey="u" href="indexes.html">Up</a>
116          </td>
117          <td width="40%" align="right">��<a accesskey="n" href="secondaryCursor.html">Next</a></td>
118        </tr>
119        <tr>
120          <td width="40%" align="left" valign="top">Reading Secondary Databases��</td>
121          <td width="20%" align="center">
122            <a accesskey="h" href="index.html">Home</a>
123          </td>
124          <td width="40%" align="right" valign="top">��
125        Using Secondary Cursors
126        
127    </td>
128        </tr>
129      </table>
130    </div>
131  </body>
132</html>
133