Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Display foreign keys and their relationship in CUBRID PHP


In order to obtain foreign key (FK) information from CUBRID database in PHP, you can use cubrid_schema() function which has the following syntax.

array cubrid_schema ( resource $conn_identifier , int $schema_type [, string $class_name [, string $attr_name ]] )

To retrieve FK info for a particular table you can pass CUBRID_SCH_IMPORTED_KEYS constant to int $schema_type. The following is the example which outputs the foreign key information for a table "game" in "demodb" database.

<?php
$conn = cubrid_connect("localhost", 33000, "demodb");

// run this on demodb database
$fks = cubrid_schema($conn, CUBRID_SCH_IMPORTED_KEYS, "game");
// $fks is now an array()

function getReferentialAction($value)
{
	switch ($value)
	{
		case 0: return 'CASCADE';
		case 1: return 'RESTRICT';
		case 2: return 'NO ACTION';
		default: return 'SET NULL';  // case 3:
	}
}

$FK_SCHEMA = '';

foreach ($fks as $fk)
{
	$FK_SCHEMA .= ($FK_SCHEMA == '' ? '' : ', ')
		. 'FOREIGN KEY ("' . $fk['FKCOLUMN_NAME'] . '") REFERENCES "'
                . $fk['PKTABLE_NAME'] . '"("' . $fk['PKCOLUMN_NAME'] . '") ON DELETE '
                . getReferentialAction($fk['DELETE_RULE'])
                . ' ON UPDATE ' . getReferentialAction($fk['UPDATE_RULE']);
}

echo $FK_SCHEMA;

cubrid_disconnect($conn);
?>

As a result you will see the output similar to:

FOREIGN KEY ("athlete_code") REFERENCES "athlete"("code") ON DELETE RESTRICT ON UPDATE RESTRICT, FOREIGN KEY ("event_code") REFERENCES "event"("code") ON DELETE RESTRICT ON UPDATE RESTRICT

comments powered by Disqus
Page info
viewed 1753 times
translations en
Author
posted 2 years ago by
CUBRID
Contributors
updated 2 years ago by
View revisions
Share this article