Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / docs / gsg / C / secondaryDelete.html
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 xmlns="" class="navheader">
16       <div class="libver">
17         <p>Library Version 11.2.5.3</p>
18       </div>
19       <table width="100%" summary="Navigation header">
20         <tr>
21           <th colspan="3" align="center">Deleting Secondary Database Records</th>
22         </tr>
23         <tr>
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>
27         </tr>
28       </table>
29       <hr />
30     </div>
31     <div class="sect1" lang="en" xml:lang="en">
32       <div class="titlepage">
33         <div>
34           <div>
35             <h2 class="title" style="clear: both"><a id="secondaryDelete"></a>Deleting Secondary Database Records</h2>
36           </div>
37         </div>
38       </div>
39       <p>
40         In general, you 
41              
42             <span>will</span> 
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.
46     </p>
47       <p>
48         However, as a convenience, you can delete 
49             
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 
53             
54             <span>secondary database</span>
55         records that reference the primary record.
56     </p>
57       <p>
58         You can use the 
59             
60             <code class="methodname">DB-&gt;del()</code>
61             
62         method to delete a secondary database record. 
63         
64         
65
66         <span>Note that if your
67             <span>secondary database</span> 
68              
69         contains duplicate records, then deleting a record from the set of
70         duplicates causes all of the duplicates to be deleted as well.
71         </span>
72
73     </p>
74       <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
75         <h3 class="title">Note</h3>
76         <p>
77       
78       <span>
79         You can delete a secondary database record using the previously
80         described mechanism
81       </span>
82       only if the primary database is opened for write access.
83       </p>
84       </div>
85       <p>For example:</p>
86       <a id="c_index7"></a>
87       <pre class="programlisting">#include &lt;db.h&gt;
88 #include &lt;string.h&gt;
89
90 ...
91
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 */
96
97 /* Primary */
98 ret = db_create(&amp;dbp, NULL, 0);
99 if (ret != 0) {
100   /* Error handling goes here */
101 }
102
103 /* Secondary */
104 ret = db_create(&amp;sdbp, NULL, 0);
105 if (ret != 0) {
106   /* Error handling goes here */
107 }
108
109 /* Usually we want to support duplicates for secondary databases */
110 ret = sdbp-&gt;set_flags(sdbp, DB_DUPSORT);
111 if (ret != 0) {
112   /* Error handling goes here */
113 }
114
115 /* open the primary database */
116 ret = dbp-&gt;open(dbp,        /* DB structure pointer */
117                 NULL,       /* Transaction pointer */
118                 "my_db.db", /* On-disk file that holds the database.
119                              * Required. */
120                 NULL,       /* Optional logical database name */
121                 DB_BTREE,   /* Database access method */
122                 0,          /* Open flags */
123                 0);         /* File mode (using defaults) */
124 if (ret != 0) {
125   /* Error handling goes here */
126 }
127
128 /* open the secondary database */
129 ret = sdbp-&gt;open(sdbp,          /* DB structure pointer */
130                  NULL,          /* Transaction pointer */
131                  "my_secdb.db", /* On-disk file that holds the database.
132                                  * Required. */
133                  NULL,          /* Optional logical database name */
134                  DB_BTREE,      /* Database access method */
135                  0,             /* Open flags */
136                  0);            /* File mode (using defaults) */
137 if (ret != 0) {
138   /* Error handling goes here */
139 }
140
141 /* Now associate the secondary to the primary */
142 dbp-&gt;associate(dbp,            /* Primary database */
143                NULL,           /* TXN id */
144                sdbp,           /* Secondary database */
145                get_sales_rep,  /* Callback used for key creation. */
146                0);             /* Flags */
147
148 /*
149  * Zero out the DBT before using it.
150  */
151 memset(&amp;key, 0, sizeof(DBT));
152
153 key.data = search_name;
154 key.size = strlen(search_name) + 1;
155
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.
160  */
161  sdbp-&gt;del(sdbp, NULL, &amp;key, 0); </pre>
162     </div>
163     <div class="navfooter">
164       <hr />
165       <table width="100%" summary="Navigation footer">
166         <tr>
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>
170           </td>
171           <td width="40%" align="right"> <a accesskey="n" href="secondaryCursor.html">Next</a></td>
172         </tr>
173         <tr>
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>
177           </td>
178           <td width="40%" align="right" valign="top"> 
179         
180         <span>Using Cursors with Secondary Databases</span>
181     </td>
182         </tr>
183       </table>
184     </div>
185   </body>
186 </html>