acknowledgments update
[openemr.git] / phpmyadmin / tbl_relation.php
blob20ee4823a2edc52b7b704799f4094c7b16b6a257
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Display table relations for viewing and editing
6 * includes phpMyAdmin relations and InnoDB relations
8 * @todo fix name handling: currently names with dots (.) are not properly handled
9 * for internal relations (but foreign keys relations are correct)
10 * @todo foreign key constraints require both fields being of equal type and size
11 * @todo check foreign fields to be from same type and size, all other makes no sense
12 * @todo add an link to create an index required for constraints,
13 * or an option to do automatically
14 * @todo if above todos are fullfilled we can add all fields meet requirements
15 * in the select dropdown
16 * @package PhpMyAdmin
19 /**
20 * Gets some core libraries
22 require_once 'libraries/common.inc.php';
23 require_once 'libraries/index.lib.php';
25 $response = PMA_Response::getInstance();
26 $header = $response->getHeader();
27 $scripts = $header->getScripts();
28 $scripts->addFile('tbl_relation.js');
29 $scripts->addFile('indexes.js');
31 /**
32 * Sets globals from $_POST
34 $post_params = array(
35 'destination',
36 'destination_foreign',
37 'display_field',
38 'fields_name',
39 'on_delete',
40 'on_update'
43 foreach ($post_params as $one_post_param) {
44 if (isset($_POST[$one_post_param])) {
45 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
49 /**
50 * Gets tables informations
52 require_once 'libraries/tbl_info.inc.php';
54 $options_array = array(
55 'CASCADE' => 'CASCADE',
56 'SET_NULL' => 'SET NULL',
57 'NO_ACTION' => 'NO ACTION',
58 'RESTRICT' => 'RESTRICT',
61 /**
62 * Gets the relation settings
64 $cfgRelation = PMA_getRelationsParam();
66 /**
67 * Updates
69 if ($cfgRelation['relwork']) {
70 $existrel = PMA_getForeigners($db, $table, '', 'internal');
72 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
73 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
75 if ($cfgRelation['displaywork']) {
76 $disp = PMA_getDisplayField($db, $table);
79 // will be used in the logic for internal relations and foreign keys:
80 $multi_edit_columns_name = isset($_REQUEST['fields_name'])
81 ? $_REQUEST['fields_name']
82 : null;
84 $html_output = '';
86 // u p d a t e s f o r I n t e r n a l r e l a t i o n s
87 if (isset($destination) && $cfgRelation['relwork']) {
89 foreach ($destination as $master_field_md5 => $foreign_string) {
90 $upd_query = false;
92 // Map the fieldname's md5 back to its real name
93 $master_field = $multi_edit_columns_name[$master_field_md5];
95 if (! empty($foreign_string)) {
96 $foreign_string = trim($foreign_string, '`');
97 list($foreign_db, $foreign_table, $foreign_field)
98 = explode('.', $foreign_string);
99 if (! isset($existrel[$master_field])) {
100 $upd_query = 'INSERT INTO '
101 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
102 . '.' . PMA_Util::backquote($cfgRelation['relation'])
103 . '(master_db, master_table, master_field, foreign_db,'
104 . ' foreign_table, foreign_field)'
105 . ' values('
106 . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
107 . '\'' . PMA_Util::sqlAddSlashes($table) . '\', '
108 . '\'' . PMA_Util::sqlAddSlashes($master_field) . '\', '
109 . '\'' . PMA_Util::sqlAddSlashes($foreign_db) . '\', '
110 . '\'' . PMA_Util::sqlAddSlashes($foreign_table) . '\','
111 . '\'' . PMA_Util::sqlAddSlashes($foreign_field) . '\')';
112 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
113 $upd_query = 'UPDATE '
114 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
115 . '.' . PMA_Util::backquote($cfgRelation['relation']) . ' SET'
116 . ' foreign_db = \''
117 . PMA_Util::sqlAddSlashes($foreign_db) . '\', '
118 . ' foreign_table = \''
119 . PMA_Util::sqlAddSlashes($foreign_table) . '\', '
120 . ' foreign_field = \''
121 . PMA_Util::sqlAddSlashes($foreign_field) . '\' '
122 . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
123 . ' AND master_table = \''
124 . PMA_Util::sqlAddSlashes($table) . '\''
125 . ' AND master_field = \''
126 . PMA_Util::sqlAddSlashes($master_field) . '\'';
127 } // end if... else....
128 } elseif (isset($existrel[$master_field])) {
129 $upd_query = 'DELETE FROM '
130 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
131 . '.' . PMA_Util::backquote($cfgRelation['relation'])
132 . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
133 . ' AND master_table = \'' . PMA_Util::sqlAddSlashes($table) . '\''
134 . ' AND master_field = \'' . PMA_Util::sqlAddSlashes($master_field)
135 . '\'';
136 } // end if... else....
137 if ($upd_query) {
138 PMA_queryAsControlUser($upd_query);
140 } // end while
141 } // end if (updates for internal relations)
143 // u p d a t e s f o r f o r e i g n k e y s
144 // (for now, one index name only; we keep the definitions if the
145 // foreign db is not the same)
147 if (isset($_REQUEST['destination_foreign'])) {
148 $display_query = '';
149 $seen_error = false;
150 foreach ($_REQUEST['destination_foreign'] as $master_field_md5 => $foreign_string) {
151 $create = false;
152 $drop = false;
154 // Map the fieldname's md5 back to it's real name
155 $master_field = $multi_edit_columns_name[$master_field_md5];
157 if (! empty($foreign_string)) {
158 list($foreign_db, $foreign_table, $foreign_field)
159 = PMA_backquoteSplit($foreign_string);
160 if (! isset($existrel_foreign[$master_field])) {
161 // no key defined for this field
162 $create = true;
163 } elseif (PMA_Util::backquote($existrel_foreign[$master_field]['foreign_db']) != $foreign_db
164 || PMA_Util::backquote($existrel_foreign[$master_field]['foreign_table']) != $foreign_table
165 || PMA_Util::backquote($existrel_foreign[$master_field]['foreign_field']) != $foreign_field
166 || $_REQUEST['constraint_name'][$master_field_md5] != $existrel_foreign[$master_field]['constraint']
167 || ($_REQUEST['on_delete'][$master_field_md5] != (! empty($existrel_foreign[$master_field]['on_delete']) ? $existrel_foreign[$master_field]['on_delete'] : 'RESTRICT'))
168 || ($_REQUEST['on_update'][$master_field_md5] != (! empty($existrel_foreign[$master_field]['on_update']) ? $existrel_foreign[$master_field]['on_update'] : 'RESTRICT'))
170 // another foreign key is already defined for this field
171 // or an option has been changed for ON DELETE or ON UPDATE
172 $drop = true;
173 $create = true;
174 } // end if... else....
175 } elseif (isset($existrel_foreign[$master_field])) {
176 $drop = true;
177 } // end if... else....
179 $tmp_error_drop = false;
180 if ($drop) {
181 $drop_query = PMA_getSQLToDropForeignKey(
182 $table, $existrel_foreign[$master_field]['constraint']
184 $display_query .= $drop_query . "\n";
185 PMA_DBI_try_query($drop_query);
186 $tmp_error_drop = PMA_DBI_getError();
188 if (! empty($tmp_error_drop)) {
189 $seen_error = true;
190 $html_output .= PMA_Util::mysqlDie(
191 $tmp_error_drop, $drop_query, false, '', false
193 continue;
196 $tmp_error_create = false;
197 if ($create) {
198 $create_query = PMA_getSQLToCreateForeignKey(
199 $table, $master_field, $foreign_db, $foreign_table, $foreign_field,
200 $_REQUEST['constraint_name'][$master_field_md5],
201 $options_array[$_REQUEST['on_delete'][$master_field_md5]],
202 $options_array[$_REQUEST['on_update'][$master_field_md5]]
205 $display_query .= $create_query . "\n";
206 PMA_DBI_try_query($create_query);
207 $tmp_error_create = PMA_DBI_getError();
208 if (! empty($tmp_error_create)) {
209 $seen_error = true;
211 if (substr($tmp_error_create, 1, 4) == '1005') {
212 $message = PMA_Message::error(
213 __('Error creating foreign key on %1$s (check data types)')
215 $message->addParam($master_field);
216 $message->display();
217 } else {
218 $html_output .= PMA_Util::mysqlDie(
219 $tmp_error_create, $create_query, false, '', false
222 $html_output .= PMA_Util::showMySQLDocu(
223 'manual_Table_types', 'InnoDB_foreign_key_constraints'
224 ) . "\n";
227 // this is an alteration and the old constraint has been dropped
228 // without creation of a new one
229 if ($drop && $create && empty($tmp_error_drop)
230 && ! empty($tmp_error_create)
232 // a rollback may be better here
233 $sql_query_recreate = '# Restoring the dropped constraint...' . "\n";
234 $sql_query_recreate .= PMA_getSQLToCreateForeignKey(
235 $table, $master_field,
236 PMA_Util::backquote($existrel_foreign[$master_field]['foreign_db']),
237 PMA_Util::backquote($existrel_foreign[$master_field]['foreign_table']),
238 PMA_Util::backquote($existrel_foreign[$master_field]['foreign_field']),
239 $existrel_foreign[$master_field]['constraint'],
240 $options_array[$existrel_foreign[$master_field]['on_delete']],
241 $options_array[$existrel_foreign[$master_field]['on_update']]
243 $display_query .= $sql_query_recreate . "\n";
244 PMA_DBI_try_query($sql_query_recreate);
247 } // end foreach
248 if (! empty($display_query) && ! $seen_error) {
249 $html_output .= PMA_Util::getMessage(
250 __('Your SQL query has been executed successfully'),
251 null, 'success'
254 } // end if isset($destination_foreign)
257 // U p d a t e s f o r d i s p l a y f i e l d
259 if ($cfgRelation['displaywork'] && isset($display_field)) {
260 $upd_query = false;
261 if ($disp) {
262 if ($display_field != '') {
263 $upd_query = 'UPDATE '
264 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
265 . '.' . PMA_Util::backquote($cfgRelation['table_info'])
266 . ' SET display_field = \''
267 . PMA_Util::sqlAddSlashes($display_field) . '\''
268 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
269 . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
270 } else {
271 $upd_query = 'DELETE FROM '
272 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
273 . '.' . PMA_Util::backquote($cfgRelation['table_info'])
274 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
275 . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
277 } elseif ($display_field != '') {
278 $upd_query = 'INSERT INTO '
279 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
280 . '.' . PMA_Util::backquote($cfgRelation['table_info'])
281 . '(db_name, table_name, display_field) VALUES('
282 . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
283 . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
284 . '\'' . PMA_Util::sqlAddSlashes($display_field) . '\')';
287 if ($upd_query) {
288 PMA_queryAsControlUser($upd_query);
290 } // end if
292 // If we did an update, refresh our data
293 if (isset($destination) && $cfgRelation['relwork']) {
294 $existrel = PMA_getForeigners($db, $table, '', 'internal');
296 if (isset($destination_foreign)
297 && PMA_Util::isForeignKeySupported($tbl_storage_engine)
299 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
302 if ($cfgRelation['displaywork']) {
303 $disp = PMA_getDisplayField($db, $table);
308 * Dialog
311 // common form
312 $html_output .= '<form method="post" action="tbl_relation.php">' . "\n"
313 . PMA_generate_common_hidden_inputs($db, $table);
316 // relations
318 if ($cfgRelation['relwork']
319 || PMA_Util::isForeignKeySupported($tbl_storage_engine)
321 // To choose relations we first need all tables names in current db
322 // and if the main table supports foreign keys
323 // we use SHOW TABLE STATUS because we need to find other tables of the
324 // same engine.
326 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
327 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db);
328 // [0] of the row is the name
329 // [1] is the type
330 } else {
331 $tab_query = 'SHOW TABLES FROM ' . PMA_Util::backquote($db);
332 // [0] of the row is the name
335 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
336 $selectboxall[] = '';
337 $selectboxall_foreign[] = '';
339 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
340 $current_table = new PMA_Table($curr_table[0], $db);
342 // explicitely ask for non-quoted list of indexed columns
343 $selectboxall = array_merge(
344 $selectboxall,
345 $current_table->getUniqueColumns($backquoted = false)
348 // if foreign keys are supported, collect all keys from other
349 // tables of the same engine
350 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)
351 && isset($curr_table[1])
352 && strtoupper($curr_table[1]) == $tbl_storage_engine
354 // explicitely ask for non-quoted list of indexed columns
355 // need to obtain backquoted values to support dots inside values
356 $selectboxall_foreign = array_merge(
357 $selectboxall_foreign,
358 $current_table->getIndexedColumns($backquoted = true)
361 } // end while over tables
362 } // end if
364 // Now find out the columns of our $table
365 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
366 $columns = PMA_DBI_get_columns($db, $table);
368 if (count($columns) > 0) {
370 foreach ($columns as $row) {
371 $save_row[] = $row;
374 $saved_row_cnt = count($save_row);
375 $html_output .= '<fieldset>'
376 . '<legend>' . __('Relations'). '</legend>'
377 . '<table>'
378 . '<tr><th>' . __('Column') . '</th>';
380 if ($cfgRelation['relwork']) {
381 $html_output .= '<th>' . __('Internal relation');
382 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
383 $html_output .= PMA_Util::showHint(
385 'An internal relation is not necessary when a corresponding'
386 . ' FOREIGN KEY relation exists.'
390 $html_output .= '</th>';
393 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
394 // this does not have to be translated, it's part of the MySQL syntax
395 $html_output .= '<th colspan="2">' . __('Foreign key constraint')
396 . ' (' . $tbl_storage_engine . ')';
397 $html_output .= '</th>';
399 $html_output .= '</tr>';
401 $odd_row = true;
402 for ($i = 0; $i < $saved_row_cnt; $i++) {
403 $myfield = $save_row[$i]['Field'];
404 // Use an md5 as array index to avoid having special characters
405 // in the name atttibure (see bug #1746964 )
406 $myfield_md5 = md5($myfield);
407 $myfield_html = htmlspecialchars($myfield);
409 $html_output .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
410 . '<td class="center">'
411 . '<strong>' . $myfield_html . '</strong>'
412 . '<input type="hidden" name="fields_name[' . $myfield_md5 . ']"'
413 . ' value="' . $myfield_html . '"/>'
414 . '</td>';
415 $odd_row = ! $odd_row;
417 if ($cfgRelation['relwork']) {
418 $html_output .= '<td><select name="destination[' . $myfield_md5 . ']">';
419 // PMA internal relations
420 if (isset($existrel[$myfield])) {
421 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
422 . $existrel[$myfield]['foreign_table'] . '.'
423 . $existrel[$myfield]['foreign_field'];
424 } else {
425 $foreign_field = false;
427 $seen_key = false;
428 foreach ($selectboxall as $value) {
429 $html_output .= '<option value="' . htmlspecialchars($value) . '"';
430 if ($foreign_field && $value == $foreign_field) {
431 $html_output .= ' selected="selected"';
432 $seen_key = true;
434 $html_output .= '>' . htmlspecialchars($value) . '</option>'. "\n";
435 } // end while
437 // if the link defined in relationtable points to a foreign field
438 // that is not a key in the foreign table, we show the link
439 // (will not be shown with an arrow)
440 if ($foreign_field && !$seen_key) {
441 $html_output .= '<option value="' . htmlspecialchars($foreign_field)
442 . '"'
443 . ' selected="selected">' . $foreign_field . '</option>'. "\n";
445 $html_output .= '</select>'
446 . '</td>';
447 } // end if (internal relations)
449 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
450 $html_output .= '<td>';
451 if (!empty($save_row[$i]['Key'])) {
452 $html_output .= '<span class="formelement">'
453 . '<select name="destination_foreign[' . $myfield_md5 . ']"'
454 . ' class="referenced_column_dropdown">';
455 if (isset($existrel_foreign[$myfield])) {
456 // need to PMA_Util::backquote to support a dot character inside
457 // an element
458 $foreign_field = PMA_Util::backquote(
459 $existrel_foreign[$myfield]['foreign_db']
461 . '.' . PMA_Util::backquote(
462 $existrel_foreign[$myfield]['foreign_table']
464 . '.' . PMA_Util::backquote(
465 $existrel_foreign[$myfield]['foreign_field']
467 } else {
468 $foreign_field = false;
471 $found_foreign_field = false;
472 foreach ($selectboxall_foreign as $value) {
473 $html_output .= '<option value="'
474 . htmlspecialchars($value) . '"';
475 if ($foreign_field && $value == $foreign_field) {
476 $html_output .= ' selected="selected"';
477 $found_foreign_field = true;
479 $html_output .= '>' . htmlspecialchars($value)
480 . '</option>'. "\n";
481 } // end while
483 // we did not find the foreign field in the tables of current db,
484 // must be defined in another db so show it to avoid erasing it
485 if (!$found_foreign_field && $foreign_field) {
486 $html_output .= '<option value="'
487 . htmlspecialchars($foreign_field) . '"'
488 . ' selected="selected"'
489 . '>' . $foreign_field . '</option>' . "\n";
491 $html_output .= '</select>'
492 . '</span>';
494 // For constraint name
495 $html_output .= '<span class="formelement">';
496 $constraint_name = isset($existrel_foreign[$myfield]['constraint'])
497 ? $existrel_foreign[$myfield]['constraint'] : '';
498 $html_output .= __('Constraint name');
499 $html_output .= '<input type="text" name="constraint_name['
500 . $myfield_md5 . ']"'
501 . ' value="' . $constraint_name . '"/>';
502 $html_output .= '</span>' . "\n";
504 $html_output .= '<span class="formelement">';
505 // For ON DELETE and ON UPDATE, the default action
506 // is RESTRICT as per MySQL doc; however, a SHOW CREATE TABLE
507 // won't display the clause if it's set as RESTRICT.
508 $on_delete = isset($existrel_foreign[$myfield]['on_delete'])
509 ? $existrel_foreign[$myfield]['on_delete'] : 'RESTRICT';
510 $html_output .= PMA_generateDropdown(
511 'ON DELETE',
512 'on_delete[' . $myfield_md5 . ']',
513 $options_array,
514 $on_delete
516 $html_output .= '</span>' . "\n";
518 $html_output .= '<span class="formelement">' . "\n";
519 $on_update = isset($existrel_foreign[$myfield]['on_update'])
520 ? $existrel_foreign[$myfield]['on_update'] : 'RESTRICT';
521 $html_output .= PMA_generateDropdown(
522 'ON UPDATE',
523 'on_update[' . $myfield_md5 . ']',
524 $options_array,
525 $on_update
527 $html_output .= '</span>' . "\n";
528 } else {
529 $html_output .= __('No index defined! Create one below');
530 } // end if (a key exists)
531 $html_output .= '</td>';
532 } // end if (InnoDB)
533 $html_output .= '</tr>';
534 } // end for
536 unset( $myfield, $myfield_md5, $myfield_html);
537 $html_output .= '</table>' . "\n"
538 . '</fieldset>' . "\n";
540 if ($cfgRelation['displaywork']) {
541 // Get "display_field" infos
542 $disp = PMA_getDisplayField($db, $table);
543 $html_output .= '<fieldset>'
544 . '<label>' . __('Choose column to display') . ': </label>'
545 . '<select name="display_field">'
546 . '<option value="">---</option>';
548 foreach ($save_row AS $row) {
549 $html_output .= '<option value="'
550 . htmlspecialchars($row['Field']) . '"';
551 if (isset($disp) && $row['Field'] == $disp) {
552 $html_output .= ' selected="selected"';
554 $html_output .= '>' . htmlspecialchars($row['Field'])
555 . '</option>'. "\n";
556 } // end while
558 $html_output .= '</select>'
559 . '</fieldset>';
560 } // end if (displayworks)
562 $html_output .= '<fieldset class="tblFooters">'
563 . '<input type="submit" value="' . __('Save') . '" />'
564 . '</fieldset>'
565 . '</form>';
566 } // end if (we have columns in this table)
568 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
569 $html_output .= '<div id="index_div" class="ajax" >'
570 . PMA_getHtmlForDisplayIndexes();
572 // Render HTML output
573 PMA_Response::getInstance()->addHTML($html_output);
576 * Generate dropdown choices
578 * @param string $dropdown_question Message to display
579 * @param string $select_name Name of the <select> field
580 * @param array $choices Choices for dropdown
581 * @param string $selected_value Selected value
583 * @return string The html code for existing value (for selected)
585 * @access public
587 function PMA_generateDropdown(
588 $dropdown_question, $select_name, $choices, $selected_value
590 $html_output = htmlspecialchars($dropdown_question) . '&nbsp;&nbsp;'
591 . '<select name="' . htmlspecialchars($select_name) . '">' . "\n";
593 foreach ($choices as $one_value => $one_label) {
594 $html_output .= '<option value="' . htmlspecialchars($one_value) . '"';
595 if ($selected_value == $one_value) {
596 $html_output .= ' selected="selected" ';
598 $html_output .= '>' . htmlspecialchars($one_label) . '</option>' . "\n";
600 $html_output .= '</select>' . "\n";
602 return $html_output;
606 * Split a string on backquote pairs
608 * @param string $text original string
610 * @return array containing the elements (and their surrounding backquotes)
612 * @access public
614 function PMA_backquoteSplit($text)
616 $elements = array();
617 $final_pos = strlen($text) - 1;
618 $pos = 0;
619 while ($pos <= $final_pos) {
620 $first_backquote = strpos($text, '`', $pos);
621 $second_backquote = strpos($text, '`', $first_backquote + 1);
622 // after the second one, there might be another one which means
623 // this is an escaped backquote
624 if ($second_backquote < $final_pos && '`' == $text[$second_backquote + 1]) {
625 $second_backquote = strpos($text, '`', $second_backquote + 2);
627 if (false === $first_backquote || false === $second_backquote) {
628 break;
630 $elements[] = substr(
631 $text, $first_backquote, $second_backquote - $first_backquote + 1
633 $pos = $second_backquote + 1;
635 return($elements);
639 * Returns the DROP query for a foreign key constraint
641 * @param string $table table of the foreign key
642 * @param string $fk foreign key name
644 * @return string DROP query for the foreign key constraint
646 function PMA_getSQLToDropForeignKey($table, $fk)
648 return 'ALTER TABLE ' . PMA_Util::backquote($table)
649 . ' DROP FOREIGN KEY ' . PMA_Util::backquote($fk) . ';';
653 * Returns the SQL query for foreign key constraint creation
655 * @param string $table table name
656 * @param string $field field name
657 * @param string $foreignDb back-quoted foreign database name
658 * @param string $foreignTable back-quoted foreign table name
659 * @param string $foreignField back-quoted foreign field name
660 * @param string $name name of the constraint
661 * @param string $onDelete on delete action
662 * @param string $onUpdate on update action
664 * @return string SQL query for foreign key constraint creation
666 function PMA_getSQLToCreateForeignKey($table, $field, $foreignDb, $foreignTable,
667 $foreignField, $name = null, $onDelete = null, $onUpdate = null
669 $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD ';
670 // if user entered a constraint name
671 if (! empty($name)) {
672 $sql_query .= ' CONSTRAINT ' . PMA_Util::backquote($name);
675 $sql_query .= ' FOREIGN KEY (' . PMA_Util::backquote($field) . ')'
676 . ' REFERENCES ' . $foreignDb . '.' . $foreignTable
677 . '(' . $foreignField . ')';
679 if (! empty($onDelete)) {
680 $sql_query .= ' ON DELETE ' . $onDelete;
682 if (! empty($onUpdate)) {
683 $sql_query .= ' ON UPDATE ' . $onUpdate;
685 $sql_query .= ';';
687 return $sql_query;