使用Perl/DBI/MySQL/InnoDB查找外键信息
我想以编程方式查找 MySQL 数据库中特定 InnoDB 表的外键。
我正在使用 Perl,并且偶然发现了 $dbh->foreign_key_info
。我刚刚尝试使用它,但似乎有点错误。
它不会返回 ON DELETE 和 ON UPDATE 信息,即使它暗示它可以。它还返回常规索引。
感谢您的任何帮助。
use strict;
use warnings;
use DBI;
use Data::Dumper;
my $dbh = DBI->connect("DBI:mysql:database=db;host=localhost", "user", "password");
my $sth = $dbh->foreign_key_info(undef, undef, undef, undef, undef, "table_name");
print Dumper $sth->fetchall_hashref("FK_NAME");
和输出:
$VAR1 = {
'some_table_ibfk_3' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => 'db',
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => 'some_other_table',
'FKCOLUMN_NAME' => 'some_other_table_id',
'FK_NAME' => 'some_table_ibfk_3',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => 'id',
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
},
'user_id_2' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => undef,
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => undef,
'FKCOLUMN_NAME' => 'some_other_table_id',
'FK_NAME' => 'user_id_2',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => undef,
'KEY_SEQ' => '2',
'UPDATE_RULE' => undef
},
'PRIMARY' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => undef,
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => undef,
'FKCOLUMN_NAME' => 'id',
'FK_NAME' => 'PRIMARY',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => undef,
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
},
'some_table_ibfk_1' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => 'db',
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => 'user_bk2',
'FKCOLUMN_NAME' => 'user_id',
'FK_NAME' => 'some_table_ibfk_1',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => 'id',
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
},
'some_table_ibfk_2' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => 'db',
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => 'user_bk2',
'FKCOLUMN_NAME' => 'coach_id',
'FK_NAME' => 'some_table_ibfk_2',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => 'id',
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
}
};
I want to programmatically find the the foreign keys on a particular InnoDB table in my MySQL database.
I'm using Perl, and I stumbled across $dbh->foreign_key_info
. I've just tried using it but it seems a bit faulty.
It doesn't return the ON DELETE, and ON UPDATE information, even though it implies it can. And it's also returning regular indexes.
Thanks for any help.
use strict;
use warnings;
use DBI;
use Data::Dumper;
my $dbh = DBI->connect("DBI:mysql:database=db;host=localhost", "user", "password");
my $sth = $dbh->foreign_key_info(undef, undef, undef, undef, undef, "table_name");
print Dumper $sth->fetchall_hashref("FK_NAME");
And the output:
$VAR1 = {
'some_table_ibfk_3' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => 'db',
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => 'some_other_table',
'FKCOLUMN_NAME' => 'some_other_table_id',
'FK_NAME' => 'some_table_ibfk_3',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => 'id',
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
},
'user_id_2' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => undef,
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => undef,
'FKCOLUMN_NAME' => 'some_other_table_id',
'FK_NAME' => 'user_id_2',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => undef,
'KEY_SEQ' => '2',
'UPDATE_RULE' => undef
},
'PRIMARY' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => undef,
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => undef,
'FKCOLUMN_NAME' => 'id',
'FK_NAME' => 'PRIMARY',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => undef,
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
},
'some_table_ibfk_1' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => 'db',
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => 'user_bk2',
'FKCOLUMN_NAME' => 'user_id',
'FK_NAME' => 'some_table_ibfk_1',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => 'id',
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
},
'some_table_ibfk_2' => {
'PK_NAME' => undef,
'DEFERABILITY' => undef,
'FKTABLE_CAT' => undef,
'PKTABLE_SCHEM' => 'db',
'UNIQUE_OR_PRIMARY' => undef,
'PKTABLE_CAT' => undef,
'FKTABLE_NAME' => 'some_table',
'FKTABLE_SCHEM' => 'db',
'PKTABLE_NAME' => 'user_bk2',
'FKCOLUMN_NAME' => 'coach_id',
'FK_NAME' => 'some_table_ibfk_2',
'DELETE_RULE' => undef,
'PKCOLUMN_NAME' => 'id',
'KEY_SEQ' => '1',
'UPDATE_RULE' => undef
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 mysql 驱动程序可能还不支持它。通过调试进行快速检查,看起来输入了以下 sql 语句:
请注意,UPDATE_RULE 和 DELETE_RULE 列都设置为 NULL。
It looks like it may not be supported by the mysql driver yet. Doing a quick check through debug, it looks like the following sql statement gets entered:
Notice that the UPDATE_RULE and DELETE_RULE columns are both set to NULL.