Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to find foreign keys

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

like image 358
André Moreira Avatar asked Oct 14 '11 09:10

André Moreira


People also ask

How do I find a foreign key in SQL?

Using SQL Server Management Studio Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

How do I find a foreign key in a database?

select * from sys.foreign_keys If you want to search foreign keys created on a SQL table, you can use following SELECT command querying sys. foreign_keys system view on parent_object_id column value. On the other hand, if SQL programmer is dealing with SQL tables referring to a specific table, then sys.

How can I find foreign key name?

To see foreign key relationships of a table: SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA. KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = 'db_name' AND REFERENCED_TABLE_NAME = 'table_name';


2 Answers

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

SELECT *
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
like image 122
rukya Avatar answered Oct 24 '22 17:10

rukya


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....

like image 22
Karthik Avatar answered Oct 24 '22 16:10

Karthik