查询查找外键

发布于 2024-12-09 23:51:32 字数 1026 浏览 0 评论 0原文

我有一个数据库,需要删除一些外键,但我事先不知道外键是否仍然存在。

我发现了一些存储过程(http://forums.mysql.com/read .php?97,218825,247526) 可以解决这个问题,但我不想为此创建存储过程。

我尝试在存储过程中使用查询,但使用“IF EXISTS (SELECT NULL FROM etc.. etc...”时出现错误。

我只能在存储过程中使用 IF EXISTS 吗?


现在,我唯一可以运行的是

SELECT * FROM information_schema.TABLE_CONSTRAINTS 
WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' 
AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';

,我也尝试过这个

IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = parm_key_name) THEN
(...) do something (...)
END IF; 

,但我得到一个 你的 SQL 语法有错误;请检查与你的 MySQL 服务器版本相对应的手册,以获取附近使用的正确语法。 'IF' 在第 1 行

我已经在论坛中寻找带有简单查询的示例,我无法理解为什么这不起作用

注意:编辑以更正损坏的链接。

I have a database where I need to drop some foreign keys, but I don't know beforehand whether the foreign keys still exist.

I've found some stored procedures (http://forums.mysql.com/read.php?97,218825,247526) that does the trick, but I don't want to create a stored procedure for this.

I've tried to use the query inside the stored procedure, but I get an error using "IF EXISTS (SELECT NULL FROM etc.. etc...

Can I only use IF EXISTS in stored procedures?


right now, the only thing I can run is

SELECT * FROM information_schema.TABLE_CONSTRAINTS 
WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' 
AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';

and I've tried this too

IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = parm_key_name) THEN
(...) do something (...)
END IF; 

but I get a You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF' at line 1

I've looked for examples in forums with simple queries and I can't make sense of why this isn't working.

NOTE: Edit to correct broken link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-12-16 23:51:32

您需要连接到信息方案,您可以在此表中找到有关主键和外键的所有信息

SELECT * FROM information_schema.TABLE_CONSTRAINTS T;

您需要成为ROOT< /code> 用户访问 information_schema

使用这个表你可以找到表、数据库以及它是否有外键。

如果您不想使用 IF EXIST 和存储过程,希望这会有所帮助。但我确信您可以使用 IF EXIST 可用于非存储过程查询......

You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table

SELECT * FROM information_schema.TABLE_CONSTRAINTS T;

you need to be a ROOT user to access the information_schema.

USING this table you can find the table, db and whether it has foreign key.

Hope this helps if you dont wanna use IF EXIST and Stored Procedure. But I am Sure you can use IF EXIST can be used for non stored procedure queries....

伏妖词 2024-12-16 23:51:32

为什么你不使用表“INFORMATION_SCHEMA”呢?

SELECT *
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'

Why don't You use the table "INFORMATION_SCHEMA" to this?

SELECT *
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
债姬 2024-12-16 23:51:32

需要连接到Information schema,就可以在这张表中找到主键和外键的所有信息

 select
        concat(table_name, '.', column_name) as 'foreign key',  
        concat(referenced_table_name, '.', referenced_column_name) as 'references'
    from
        information_schema.key_column_usage
    where
        referenced_table_name is not null;

帮助:请参阅此链接 list-foreign-keys-in-mysql

You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table

 select
        concat(table_name, '.', column_name) as 'foreign key',  
        concat(referenced_table_name, '.', referenced_column_name) as 'references'
    from
        information_schema.key_column_usage
    where
        referenced_table_name is not null;

HELP: see this link list-foreign-keys-in-mysql

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文