| 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>Foreign key indexes</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="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="am.html" title="Chapter 3. Access Method Operations" /> |
| 11 | <link rel="prev" href="am_second.html" title="Secondary indexes" /> |
| 12 | <link rel="next" href="am_cursor.html" title="Cursor operations" /> |
| 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">Foreign key indexes</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_second.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 3. |
| 26 | Access Method Operations |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="am_cursor.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="am_foreign"></a>Foreign key indexes</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p> |
| 42 | Foreign keys are used to ensure a level of consistency between |
| 43 | two different databases in terms of the keys that the databases use. |
| 44 | In a foreign key relationship, one database is the |
| 45 | <span class="emphasis"><em>constrained</em></span> database. This database is actually a |
| 46 | secondary database which is associated with a primary database. The |
| 47 | other database in this relationship is the |
| 48 | <span class="emphasis"><em>foreign key</em></span> database. Once this relationship has |
| 49 | been established between a constrained database and a foreign key |
| 50 | database, then: |
| 51 | </p> |
| 52 | <div class="orderedlist"> |
| 53 | <ol type="1"> |
| 54 | <li> |
| 55 | <p> |
| 56 | Key/data items cannot be added to the |
| 57 | constrained database unless that same key |
| 58 | already exists in the foreign key |
| 59 | database. |
| 60 | </p> |
| 61 | </li> |
| 62 | <li> |
| 63 | <p> |
| 64 | A key/data pair cannot be deleted from the foreign |
| 65 | key database unless some action is also taken to |
| 66 | keep the constrained database consistent with |
| 67 | the foreign key database. |
| 68 | </p> |
| 69 | </li> |
| 70 | </ol> |
| 71 | </div> |
| 72 | <p> |
| 73 | Because the constrained database is a secondary database, by ensuring |
| 74 | it's consistency with a foreign key database you are actually ensuring |
| 75 | that a primary database (the one to which the secondary database is |
| 76 | associated) is consistent with the foreign key database. |
| 77 | </p> |
| 78 | <p> |
| 79 | Deletions of keys in the foreign key database affect the constrained database |
| 80 | in one of three ways, as specified by the application: |
| 81 | </p> |
| 82 | <div class="itemizedlist"> |
| 83 | <ul type="disc"> |
| 84 | <li> |
| 85 | <p> |
| 86 | <code class="literal">Abort</code> |
| 87 | </p> |
| 88 | <p> |
| 89 | The deletion of a record from the foreign database will not proceed if that key |
| 90 | exists in the constrained primary database. Transactions must be used to prevent |
| 91 | the aborted delete from corrupting either of the databases. |
| 92 | </p> |
| 93 | </li> |
| 94 | <li> |
| 95 | <p> |
| 96 | <code class="literal">Cascade</code> |
| 97 | </p> |
| 98 | <p> |
| 99 | The deletion of a record from the foreign database will also cause any records |
| 100 | in the constrained primary database that use that key to also be |
| 101 | automatically deleted. |
| 102 | </p> |
| 103 | </li> |
| 104 | <li> |
| 105 | <p> |
| 106 | <code class="literal">Nullify</code> |
| 107 | </p> |
| 108 | <p> |
| 109 | The deletion of a record from the foreign database will cause a user specified |
| 110 | callback function to be called, in order to alter or nullify any records |
| 111 | using that key in the constrained primary database. |
| 112 | </p> |
| 113 | </li> |
| 114 | </ul> |
| 115 | </div> |
| 116 | <p> |
| 117 | Note that it is possible to delete a key from the constrained database, |
| 118 | but not from the foreign key database. For this reason, if you want the |
| 119 | keys used in both databases to be 100% accurate, then you will have to |
| 120 | write code to ensure that when a key is removed from the constrained |
| 121 | database, it is also removed from the foreign key database. |
| 122 | </p> |
| 123 | <p> |
| 124 | As an example of how foreign key indexes might be used, consider a database |
| 125 | of customer information and a database of order information. A typical |
| 126 | customer database would use a customer ID as the key and those keys |
| 127 | would also appear in the order database. To ensure an order is not |
| 128 | booked for a non-existent customer, the customer database can be |
| 129 | associated with the order database as a foreign index. |
| 130 | </p> |
| 131 | <p> |
| 132 | In order to do this, you create a secondary index of the order |
| 133 | database, which uses customer IDs as the key for its key/data pairs. |
| 134 | This secondary index is, then, the constrained database. But because |
| 135 | the secondary index is constrained, so too is the order database |
| 136 | because the contents of the secondary index are programmatically tied |
| 137 | to the contents of the order database. |
| 138 | </p> |
| 139 | <p> |
| 140 | The customer database, then, is the foreign key database. It is |
| 141 | associated to the order database's secondary index using the |
| 142 | <a href="../api_reference/C/dbassociate_foreign.html" class="olink">DB->associate_foreign()</a> method. In this way, |
| 143 | an order cannot be added to the order database unless the customer ID |
| 144 | already exists in the customer database. |
| 145 | </p> |
| 146 | <p> |
| 147 | Note that this relationship can also be configured to delete any |
| 148 | outstanding orders for a customer when that customer is deleted from |
| 149 | the customer database. |
| 150 | </p> |
| 151 | <p>In SQL, this would be done by executing something like the following:</p> |
| 152 | <pre class="programlisting">CREATE TABLE customers(cust_id CHAR(4) NOT NULL, |
| 153 | lastname CHAR(15), firstname CHAR(15), PRIMARY KEY(cust_id)); |
| 154 | CREATE TABLE orders(order_id CHAR(4) NOT NULL, order_num int NOT NULL, |
| 155 | cust_id CHAR(4), PRIMARY KEY (order_id), |
| 156 | FOREIGN KEY (cust_id) REFERENCES customers(cust_id) |
| 157 | ON DELETE CASCADE); |
| 158 | </pre> |
| 159 | <p>In Berkeley DB, this would work as follows:</p> |
| 160 | <a id="prog_am18"></a> |
| 161 | <pre class="programlisting">struct customer { |
| 162 | char cust_id[4]; |
| 163 | char last_name[15]; |
| 164 | char first_name[15]; |
| 165 | }; |
| 166 | struct order { |
| 167 | char order_id[4]; |
| 168 | int order_number; |
| 169 | char cust_id[4]; |
| 170 | }; |
| 171 | |
| 172 | .... |
| 173 | |
| 174 | void |
| 175 | foreign() |
| 176 | { |
| 177 | DB *dbp, *sdbp, *fdbp; |
| 178 | int ret; |
| 179 | |
| 180 | /* Open/create order database */ |
| 181 | if ((ret = db_create(&dbp, dbenv, 0)) != 0) |
| 182 | handle_error(ret); |
| 183 | if ((ret = dbp->open(dbp, NULL, |
| 184 | "orders.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0) |
| 185 | handle_error(ret); |
| 186 | |
| 187 | /* |
| 188 | * Open/create secondary index on customer id. Note that it |
| 189 | * supports duplicates because a customer may have multiple |
| 190 | * orders. |
| 191 | */ |
| 192 | if ((ret = db_create(&sdbp, dbenv, 0)) != 0) |
| 193 | handle_error(ret); |
| 194 | if ((ret = sdbp->set_flags(sdbp, DB_DUP | DB_DUPSORT)) != 0) |
| 195 | handle_error(ret); |
| 196 | if ((ret = sdbp->open(sdbp, NULL, "orders_cust_ids.db", |
| 197 | NULL, DB_BTREE, DB_CREATE, 0600)) != 0) |
| 198 | handle_error(ret); |
| 199 | |
| 200 | /* Associate the secondary with the primary. */ |
| 201 | if ((ret = dbp->associate(dbp, NULL, sdbp, getcustid, 0)) != 0) |
| 202 | handle_error(ret); |
| 203 | |
| 204 | /* Open/create customer database */ |
| 205 | if ((ret = db_create(&fdbp, dbenv, 0)) != 0) |
| 206 | handle_error(ret); |
| 207 | if ((ret = fdbp->open(fdbp, NULL, |
| 208 | "customers.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0) |
| 209 | handle_error(ret); |
| 210 | |
| 211 | /* Associate the foreign with the secondary. */ |
| 212 | if ((ret = fdbp->associate_foreign( |
| 213 | fdbp, sdbp, NULL, DB_FOREIGN_CASCADE)) != 0) |
| 214 | handle_error(ret); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * getcustid -- extracts a secondary key (the customer id) from a primary |
| 220 | * key/data pair |
| 221 | */ |
| 222 | int |
| 223 | getcustid(secondary, pkey, pdata, skey) |
| 224 | DB *secondary; |
| 225 | const DBT *pkey, *pdata; |
| 226 | DBT *skey; |
| 227 | { |
| 228 | /* |
| 229 | * Since the secondary key is a simple structure member of the |
| 230 | * record, we don't have to do anything fancy to return it. If |
| 231 | * we have composite keys that need to be constructed from the |
| 232 | * record, rather than simply pointing into it, then the user's |
| 233 | * function might need to allocate space and copy data. In |
| 234 | * this case, the DB_DBT_APPMALLOC flag should be set in the |
| 235 | * secondary key DBT. |
| 236 | */ |
| 237 | memset(skey, 0, sizeof(DBT)); |
| 238 | skey->data = ((struct order *)pdata->data)->cust_id; |
| 239 | skey->size = 4; |
| 240 | return (0); |
| 241 | }</pre> |
| 242 | </div> |
| 243 | <div class="navfooter"> |
| 244 | <hr /> |
| 245 | <table width="100%" summary="Navigation footer"> |
| 246 | <tr> |
| 247 | <td width="40%" align="left"><a accesskey="p" href="am_second.html">Prev</a> </td> |
| 248 | <td width="20%" align="center"> |
| 249 | <a accesskey="u" href="am.html">Up</a> |
| 250 | </td> |
| 251 | <td width="40%" align="right"> <a accesskey="n" href="am_cursor.html">Next</a></td> |
| 252 | </tr> |
| 253 | <tr> |
| 254 | <td width="40%" align="left" valign="top">Secondary indexes </td> |
| 255 | <td width="20%" align="center"> |
| 256 | <a accesskey="h" href="index.html">Home</a> |
| 257 | </td> |
| 258 | <td width="40%" align="right" valign="top"> Cursor operations</td> |
| 259 | </tr> |
| 260 | </table> |
| 261 | </div> |
| 262 | </body> |
| 263 | </html> |