简体   繁体   中英

Checking foreignkey constraints MySQL

I need to check foreign key constraints on a schemas. I need the table name, the column name, the referenced schema, the referenced table and the referenced column. Google search tells me that I could do this using information_schema. I have some doubts regarding this approach.

  1. Is the information schema reliable? I have too many large databases containing too many tables and any errors would be costly
  2. Considering information_schema is a system database and contains info about all databases, any query will take considerably longer. Right?

Is there an alternate method to check foreign key constraints? Using SHOW CREATE TABLE and comparing queries does not seem to be a very good idea.

My application will be coded in C# (Connector:ODBC). I know there is a .NET MySQL connector which provides Foreign Key Columns collection for the getschema method (http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connecting-connection-string.html#connector-net-programming-getschema). Does anyone know if it uses information_schema too?

I could really use some help here.

Regards,

Is the information schema reliable?

If it isn't, neither is your database. (Tip: it isn't ).

Considering information_schema is a system database and contains info about all databases, any query will take considerably longer. Right?

That depends on how you query them. Try show indexes to see the available indexes.

Is there an alternate method to check foreign key constraints? Using SHOW CREATE TABLE and comparing queries does not seem to be a very good idea.

In theory, show create table queries the information_schema in a slightly different way.

In practice, and quoting the second link further up, some of the information returned between the two might be inconsistent:

create table rolando (num int not null, primary key (num) using hash);

mysql> show create table rolando\G
  (...)
  PRIMARY KEY (`num`) USING HASH

mysql> show indexes from rolando;
(...) | Index_type | (...)
(...) | BTREE      | (...)

Lastly, see:

Is querying the MySQL information_schema database a good way to find related tables?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM