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 58 <code class="methodname">Db::del()</code> 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="cxx_index7"></a> 84 <pre class="programlisting">#include <db_cxx.h> 85#include <string.h> 86 87... 88 89Db my_database(NULL, 0); // Primary 90Db my_index(NULL, 0); // Secondary 91 92// Open the primary 93my_database.open(NULL, // Transaction pointer 94 "my_db.db", // On-disk file that holds the database. 95 NULL, // Optional logical database name 96 DB_BTREE, // Database access method 97 DB_CREATE, // Open flags 98 0); // File mode (using defaults) 99 100// Setup the secondary to use sorted duplicates. 101// This is often desireable for secondary databases. 102my_index.set_flags(DB_DUPSORT); 103 104// Open the secondary 105my_index.open(NULL, // Transaction pointer 106 "my_secondary.db", // On-disk file that holds the database. 107 NULL, // Optional logical database name 108 DB_BTREE, // Database access method 109 DB_CREATE, // Open flags. 110 0); // File mode (using defaults) 111 112 113// Now associate the primary and the secondary 114my_database.associate(NULL, // Txn id 115 &my_index, // Associated secondary database 116 get_sales_rep, // Callback used for key extraction. 117 0); // Flags 118 119// Name to delete 120char *search_name = "John Doe"; 121 122// Get a search key 123Dbt key(search_name, strlen(search_name) + 1); 124 125// Now delete the secondary record. This causes the associated primary 126// record to be deleted. If any other secondary databases have secondary 127// records referring to the deleted primary record, then those secondary 128// records are also deleted. 129my_index.del(NULL, &key, 0); </pre> 130 </div> 131 <div class="navfooter"> 132 <hr /> 133 <table width="100%" summary="Navigation footer"> 134 <tr> 135 <td width="40%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a>��</td> 136 <td width="20%" align="center"> 137 <a accesskey="u" href="indexes.html">Up</a> 138 </td> 139 <td width="40%" align="right">��<a accesskey="n" href="secondaryCursor.html">Next</a></td> 140 </tr> 141 <tr> 142 <td width="40%" align="left" valign="top">Reading Secondary Databases��</td> 143 <td width="20%" align="center"> 144 <a accesskey="h" href="index.html">Home</a> 145 </td> 146 <td width="40%" align="right" valign="top">�� 147 148 <span>Using Cursors with Secondary Databases</span> 149 </td> 150 </tr> 151 </table> 152 </div> 153 </body> 154</html> 155