查询查找外键
我有一个数据库,需要删除一些外键,但我事先不知道外键是否仍然存在。
我发现了一些存储过程(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要连接到信息方案,您可以在此表中找到有关主键和外键的所有信息
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 theinformation_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 useIF EXIST
can be used for non stored procedure queries....为什么你不使用表“INFORMATION_SCHEMA”呢?
Why don't You use the table "INFORMATION_SCHEMA" to this?
需要连接到Information schema,就可以在这张表中找到主键和外键的所有信息
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