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">
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" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Deleting Secondary Database Records</th>
24 <td width="20%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 5. Secondary Databases</th>
26 <td width="20%" align="right"> <a accesskey="n" href="secondaryCursor.html">Next</a></td>
31 <div class="sect1" lang="en" xml:lang="en">
32 <div class="titlepage">
35 <h2 class="title" style="clear: both"><a id="secondaryDelete"></a>Deleting Secondary Database Records</h2>
43 not modify a secondary database directly. In
44 order to modify a secondary database, you should modify the primary
45 database and simply allow DB to manage the secondary modifications for you.
48 However, as a convenience, you can delete
50 <span>secondary database</span>
51 records directly. Doing so causes the associated primary key/data pair to be deleted.
52 This in turn causes DB to delete all
54 <span>secondary database</span>
55 records that reference the primary record.
60 <code class="methodname">DB->del()</code>
62 method to delete a secondary database record.
66 <span>Note that if your
67 <span>secondary database</span>
69 contains duplicate records, then deleting a record from the set of
70 duplicates causes all of the duplicates to be deleted as well.
74 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
75 <h3 class="title">Note</h3>
79 You can delete a secondary database record using the previously
82 only if the primary database is opened for write access.
87 <pre class="programlisting">#include <db.h>
88 #include <string.h>
92 DB *dbp, *sdbp; /* Primary and secondary DB handles */
93 DBT key; /* DBTs used for the delete */
94 int ret; /* Function return value */
95 char *search_name = "John Doe"; /* Name to delete */
98 ret = db_create(&dbp, NULL, 0);
100 /* Error handling goes here */
104 ret = db_create(&sdbp, NULL, 0);
106 /* Error handling goes here */
109 /* Usually we want to support duplicates for secondary databases */
110 ret = sdbp->set_flags(sdbp, DB_DUPSORT);
112 /* Error handling goes here */
115 /* open the primary database */
116 ret = dbp->open(dbp, /* DB structure pointer */
117 NULL, /* Transaction pointer */
118 "my_db.db", /* On-disk file that holds the database.
120 NULL, /* Optional logical database name */
121 DB_BTREE, /* Database access method */
123 0); /* File mode (using defaults) */
125 /* Error handling goes here */
128 /* open the secondary database */
129 ret = sdbp->open(sdbp, /* DB structure pointer */
130 NULL, /* Transaction pointer */
131 "my_secdb.db", /* On-disk file that holds the database.
133 NULL, /* Optional logical database name */
134 DB_BTREE, /* Database access method */
136 0); /* File mode (using defaults) */
138 /* Error handling goes here */
141 /* Now associate the secondary to the primary */
142 dbp->associate(dbp, /* Primary database */
144 sdbp, /* Secondary database */
145 get_sales_rep, /* Callback used for key creation. */
149 * Zero out the DBT before using it.
151 memset(&key, 0, sizeof(DBT));
153 key.data = search_name;
154 key.size = strlen(search_name) + 1;
156 /* Now delete the secondary record. This causes the associated primary
157 * record to be deleted. If any other secondary databases have secondary
158 * records referring to the deleted primary record, then those secondary
159 * records are also deleted.
161 sdbp->del(sdbp, NULL, &key, 0); </pre>
163 <div class="navfooter">
165 <table width="100%" summary="Navigation footer">
167 <td width="40%" align="left"><a accesskey="p" href="readSecondary.html">Prev</a> </td>
168 <td width="20%" align="center">
169 <a accesskey="u" href="indexes.html">Up</a>
171 <td width="40%" align="right"> <a accesskey="n" href="secondaryCursor.html">Next</a></td>
174 <td width="40%" align="left" valign="top">Reading Secondary Databases </td>
175 <td width="20%" align="center">
176 <a accesskey="h" href="index.html">Home</a>
178 <td width="40%" align="right" valign="top">
180 <span>Using Cursors with Secondary Databases</span>