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