Open Source RDBMS - Seamless, Scalable, Stable and Free

English | 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
문서 정보
viewed 1342 times
번역 en
작성자
posted 작년
CUBRID
공헌자
마지막 수정시간 작년
변경 내역 보기
Share this article