Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 723 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
View all foreign key constraints for entire MySQL database

#1
I have a large database with over 150 tables that I've recently been handed. I'm just wondering if there is an easy way to view all foreign key constraints for the entire DB instead of on a per-table basis.
Reply

#2
You can use the [`INFORMATION_SCHEMA`][1] tables for this. For example, the [`INFORMATION_SCHEMA TABLE_CONSTRAINTS`][2] table.

Something like this should do it:

select *
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'FOREIGN KEY'


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
**SQL:**

select constraint_name,
table_schema,
table_name
from information_schema.table_constraints
where constraint_schema = 'astdb'

**Output:**

+----------------------------+--------------+---------------------+
| constraint_name | table_schema | table_name |
+----------------------------+--------------+---------------------+
| PRIMARY | astdb | asset_category |
| PRIMARY | astdb | asset_type |
| PRIMARY | astdb | asset_valuation |
| PRIMARY | astdb | assets |
| PRIMARY | astdb | com_mst |
| PRIMARY | astdb | com_typ |
| PRIMARY | astdb | ref_company_type |
| PRIMARY | astdb | supplier |
| PRIMARY | astdb | third_party_company |
| third_party_company_ibfk_1 | astdb | third_party_company |
| PRIMARY | astdb | user |
| PRIMARY | astdb | user_role |
+----------------------------+--------------+---------------------+


Reply

#4
Query this code


select constraint_name,
table_schema,
table_name
from information_schema.table_constraints

You will get constraint_name, and filter the table_schema which is the list of `database `.

[Look at This][1]


[1]:

[To see links please register here]

Reply

#5
This is what I prefer to get useful informations:

SELECT CONSTRAINT_NAME,
UNIQUE_CONSTRAINT_NAME,
MATCH_OPTION,
UPDATE_RULE,
DELETE_RULE,
TABLE_NAME,
REFERENCED_TABLE_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = 'your_database_name'
Reply

#6
The currently accepted answer by user RedFilter will work fine if you have just 1 database, but not if you have many.

After entering `use information_schema;` use this query to get foreign keys for `name_of_db`:

select * from `table_constraints` where `table_schema` like `name_of_db` and `constraint_type` = 'FOREIGN KEY'

Use this query to get foreign keys for `name_of_db` saved to world-writeable file `output_filepath_and_name`:


select * from `table_constraints` where `table_schema` like "name_of_db" and `constraint_type` = 'FOREIGN KEY' into outfile "output_filepath_and_name" FIELDS TERMINATED BY ',' ENCLOSED BY '"';
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through