Get foreign keys for database table

15 ビュー (過去 30 日間)
K K
K K 2018 年 5 月 30 日
回答済み: Piyush Kumar 2024 年 10 月 29 日 4:19
Hi,
I have some code that I use with a mysql database backend that uses the importedkeys function. I see that function is marked as "to be removed". This code will obviously break when that function is removed. What is the replacement for it? I don't see anything obvious when reading through the DB Toolbox function list
Thanks,
Kristo
  3 件のコメント
Guillaume
Guillaume 2018 年 5 月 30 日
I'm not familiar with mysql but I assume that the tables relationships are stored in a system table. So you should be able to query that table whatever its name is.
K K
K K 2018 年 6 月 6 日
So it sounds like this is not possible going forward?

サインインしてコメントする。

回答 (1 件)

Piyush Kumar
Piyush Kumar 2024 年 10 月 29 日 4:19
Hi,
To get foreign keys for a table in a MySQL database, you can query the INFORMATION_SCHEMA.KEY_COLUMN_USAGE table. Here's the SQL query you can use:
SELECT
TABLE_NAME,
COLUMN_NAME,
CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = 'database_name' AND
TABLE_NAME = 'table_name' AND
REFERENCED_TABLE_NAME IS NOT NULL;
To achieve the same using a MATLAB script,
  • Establish a connection using database function.
  • Execute the above sql query and get the results using fetch function.
  • Close the connection at last using close function.

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by