简体   繁体   English

如何检查是否创建了外键

[英]How to check if a foreign key is created

I have created a foreign key with NaviCat (a MySQL application), but the instant I create it, it disappears from the foreign key list and a new Index get added. 我已经使用NaviCat(一个MySQL应用程序)创建了一个外键,但是创建它的那一刻,它就从外键列表中消失了,并添加了一个新的索引。 Does that mean something went wrong or is that normal to happen? 这是否意味着出了点问题或正常发生?

I have tried using the information_schema How to check if a column is already a foreign key? 我已尝试使用information_schema 如何检查列是否已为外键? but that resulted in Unknown table 'REFERENTIAL_CONSTRAINTS' in information_schema . 但这导致Unknown table 'REFERENTIAL_CONSTRAINTS' in information_schema中的Unknown table 'REFERENTIAL_CONSTRAINTS' in information_schema Is it possible that query is for MsSQL and is different with MySQL? 查询是否可能针对MsSQL,并且与MySQL不同?

  1. The table you are probably creating the foreign key is MyISAM. 您可能正在创建外键的表是MyISAM。 Go to the Table Design view and go to the Options tab to change the table Engine to InnoDB 转到“表设计”视图,然后转到“选项”选项卡,将表引擎更改为InnoDB

  2. You can change all your tables to InnoDB following the steps at http://kevin.vanzonneveld.net/techblog/article/convert_all_tables_to_innodb_in_one_go/ 您可以按照http://kevin.vanzonneveld.net/techblog/article/convert_all_tables_to_innodb_in_one_go/中的步骤将所有表更改为InnoDB。

  3. Add default-storage-engine=innodb in the [mysqld] section of your MySQL configuration file (usually my.cnf) from http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se在MySQL配置文件(通常是my.cnf)的[mysqld]部分中添加default-storage-engine = innodb 。 html

There are no flaws after all from MySQL 5.5 InnoDB is the default storage engine. 毕竟MySQL 5.5没有缺陷,InnoDB是默认的存储引擎。

execute 执行

SHOW CREATE TABLE myTable;

and then check for 然后检查

ENGINE = InnoDB

if this is true, you can use foreign keys. 如果是这样,则可以使用外键。 check if it contains something like this: 检查它是否包含以下内容:

FOREIGN KEY (customer_id)  REFERENCES customer(id)

hope that helps ! 希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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