Getting Foreign Keys Using SHOW CREATE TABLE

Since CUBRID 9.0, CUBRID supports SHOW CREATE TABLE statement. This command allows you to see all the table columns as well as all the constraints such as primary key or foreign key. Below you can see the SHOW CREATE TABLE result ran on the "game" table from demo database:

From the above output, you can see that pk_game_host_year_event_code_athlete_code is a primary key on host_year, event_code and athlete_code fields, while fk_game_event_code and fk_game_athlete_code are foreign keys referencing the event_code field in event table and athlete_code in athlete table respectively.

Getting Primary Keys Using db_index and SHOW INDEX

Another approach that works regardless of CUBRID version is described in the following section. Use the command below.

There are 2 foreign keys: fk_game_athlete_code. To find the columns constrained by a given foreign (or primary) key, run the SHOW INDEX command and look for the Key_name that corresponds to the above index_name.

As you can see, the 2 constrained columns are athlete_code and event_code. Currently, there is no way to obtain the referenced table name in SQL apart from using "SHOW CREATE TABLE" statement.