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>DB->associate_foreign()</title>
7 <link rel="stylesheet" href="apiReference.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB C API Reference" />
10 <link rel="up" href="db.html" title="Chapter 2. The DB Handle" />
11 <link rel="prev" href="dbassociate.html" title="DB->associate()" />
12 <link rel="next" href="dbclose.html" title="DB->close()" />
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">DB->associate_foreign()</th>
24 <td width="20%" align="left"><a accesskey="p" href="dbassociate.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 2.
28 <td width="20%" align="right"> <a accesskey="n" href="dbclose.html">Next</a></td>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
37 <h2 class="title" style="clear: both"><a id="dbassociate_foreign"></a>DB->associate_foreign()</h2>
41 <pre class="programlisting">#include <db.h>
44 DB->associate_foreign(DB *foreign, DB *secondary,,
45 int (*callback)(DB *secondary,
46 const DBT *key, DBT *data, const DBT *foreignkey, int *changed),
47 u_int32_t flags); </pre>
49 The <code class="methodname">DB->associate_foreign()</code> function is used to declare one database a
50 foreign constraint for a secondary database. The
51 <a class="link" href="db.html" title="Chapter 2. The DB Handle">DB</a> handle that you call the
52 <code class="methodname">associate_foreign()</code> method from is the foreign
56 After a foreign database has been "associated" with a secondary
57 database, all keys inserted into the secondary must exist in the
58 foreign database. Attempting to add a record with a foreign key
59 that does not exist in the foreign database will cause the put
60 method to fail and return <code class="literal">DB_FOREIGN_CONFLICT</code>.
63 Deletions in the foreign database affect the secondary in a manner
64 defined by the flags parameter. See
65 <a href="../../programmer_reference/am_foreign.html" class="olink">Foreign Indices</a>
66 in the <em class="citetitle">Berkeley DB Programmer's Reference Guide</em> for more information.
69 The <code class="methodname">DB->associate_foreign()</code> <span>
71 method returns a non-zero error value on failure and 0 on success.
76 <div class="sect2" lang="en" xml:lang="en">
77 <div class="titlepage">
80 <h3 class="title"><a id="idp57190936"></a>Parameters</h3>
84 <div class="sect3" lang="en" xml:lang="en">
85 <div class="titlepage">
88 <h4 class="title"><a id="idp57303592"></a>foreign</h4>
93 The <span class="bold"><strong>foreign</strong></span> parameter should be a
94 database handle for the foreign database.
97 <div class="sect3" lang="en" xml:lang="en">
98 <div class="titlepage">
101 <h4 class="title"><a id="idp57310816"></a>secondary</h4>
106 The <span class="bold"><strong>secondary</strong></span> parameter should be an
107 open database handle of a database that contains a secondary index who's
108 keys also exist in the <span class="bold"><strong>foreign</strong></span> database.
111 <div class="sect3" lang="en" xml:lang="en">
112 <div class="titlepage">
115 <h4 class="title"><a id="idp57273032"></a>callback</h4>
120 The <span class="bold"><strong>callback</strong></span> parameter is a callback
121 function that nullifies the foreign key portion of a data <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>.
124 The callback parameter must be NULL if either DB_FOREIGN_ABORT or DB_FOREIGN_CASCADE is set.
127 The callback takes four arguments:
129 <div class="itemizedlist">
133 <code class="literal">secondary</code>
136 The <span class="bold"><strong>secondary</strong></span> parameter is the
137 database handle for the secondary.
142 <code class="literal">key</code>
145 The <span class="bold"><strong>key</strong></span> parameter is a
146 <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the primary key.
151 <code class="literal">data</code>
154 The <span class="bold"><strong>data</strong></span> parameter is a
155 <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the primary data
161 <code class="literal">foreignkey</code>
164 The <span class="bold"><strong>foreignkey</strong></span> parameter is a
165 <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the foreign
166 key which is being deleted.
171 <code class="literal">changed</code>
174 The <span class="bold"><strong>changed</strong></span> parameter is a
175 pointer to a boolean value, indicated whether <span class="bold"><strong>data</strong></span>
181 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
182 <h3 class="title">Note</h3>
184 Berkeley DB is not re-entrant. Callback functions should not
185 attempt to make library calls (for example, to release locks or
186 close open handles). Re-entering Berkeley DB is not guaranteed
187 to work correctly, and the results are undefined.
191 <div class="sect3" lang="en" xml:lang="en">
192 <div class="titlepage">
195 <h4 class="title"><a id="idp57346048"></a>flags</h4>
200 The <span class="bold"><strong>flags</strong></span> parameter must be set to
201 one of the following values:
203 <div class="itemizedlist">
206 <p><a id="associate_foreign_DB_FOREIGN_ABORT"></a>
207 <code class="literal">DB_FOREIGN_ABORT</code>
210 Abort the deletion of a key in the foreign database and return DB_FOREIGN_CONFLICT
211 if that key exists in the secondary database. The deletion should be protected by
212 a transaction to ensure database integrity after the aborted delete.
216 <p><a id="associate_foreign_DB_FOREIGN_CASCADE"></a>
217 <code class="literal">DB_FOREIGN_CASCADE</code>
220 The deletion of a key in the foreign database will also delete that key from the
221 secondary database (and the corresponding entry in the secondary's primary database.)
225 <p><a id="associate_foreign_DB_FOREIGN_NULLIFY"></a>
226 <code class="literal">DB_FOREIGN_NULLIFY</code>
229 The deletion of a key in the foreign database will call the nullification function
230 passed to associate_foreign and update the secondary database with the changed data.
237 <div class="sect2" lang="en" xml:lang="en">
238 <div class="titlepage">
241 <h3 class="title"><a id="idp57219832"></a>Errors</h3>
246 The <code class="methodname">DB->associate_foreign()</code> <span>
248 method may fail and return one of the following non-zero errors:
253 <div class="sect3" lang="en" xml:lang="en">
254 <div class="titlepage">
257 <h4 class="title"><a id="idp57355048"></a> DB_REP_HANDLE_DEAD</h4>
262 When a client synchronizes with the master, it is possible for committed
263 transactions to be rolled back. This invalidates all the database and cursor
264 handles opened in the replication environment. Once this occurs, an attempt to use
267 return <code class="literal">DB_REP_HANDLE_DEAD</code>.
268 The application will need to discard the handle and open a new one in order to
272 <div class="sect3" lang="en" xml:lang="en">
273 <div class="titlepage">
276 <h4 class="title"><a id="idp57346112"></a>DB_REP_LOCKOUT</h4>
281 The operation was blocked by client/master synchronization.
284 <div class="sect3" lang="en" xml:lang="en">
285 <div class="titlepage">
288 <h4 class="title"><a id="idp57331896"></a>EINVAL</h4>
293 If the foreign database handle is a secondary index; the foreign
294 database handle has been configured to allow duplicates; the foreign
295 database handle is a renumbering recno database; callback is
296 configured and DB_FOREIGN_NULLIFY is not; DB_FOREIGN_NULLIFY is
297 configured and callback is not.
301 <div class="sect2" lang="en" xml:lang="en">
302 <div class="titlepage">
305 <h3 class="title"><a id="idp57307960"></a>Class</h3>
310 <a class="link" href="db.html" title="Chapter 2. The DB Handle">DB</a>
313 <div class="sect2" lang="en" xml:lang="en">
314 <div class="titlepage">
317 <h3 class="title"><a id="idp57363848"></a>See Also</h3>
322 <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a>
326 <div class="navfooter">
328 <table width="100%" summary="Navigation footer">
330 <td width="40%" align="left"><a accesskey="p" href="dbassociate.html">Prev</a> </td>
331 <td width="20%" align="center">
332 <a accesskey="u" href="db.html">Up</a>
334 <td width="40%" align="right"> <a accesskey="n" href="dbclose.html">Next</a></td>
337 <td width="40%" align="left" valign="top">DB->associate() </td>
338 <td width="20%" align="center">
339 <a accesskey="h" href="index.html">Home</a>
341 <td width="40%" align="right" valign="top"> DB->close()</td>