2010-04-21 16 views

risposta

53

È possibile utilizzare le tabelle INFORMATION_SCHEMA per questo. Ad esempio, la tabella INFORMATION_SCHEMA TABLE_CONSTRAINTS.

Qualcosa del genere dovrebbe farlo:

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

Sembra che abbia esattamente quello di cui ho bisogno. Grazie! –

+1

Esiste un modo per elencare effettivamente il nome del campo della chiave esterna? – JoeTidee

5

La risposta attualmente accettato da RedFilter utente funziona bene se avete solo 1 database, ma non se si hanno molti.

Dopo aver inserito use information_schema; utilizzare questa query per ottenere le chiavi esterne per name_of_db:

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

Utilizzare questa query per ottenere le chiavi esterne per name_of_db salvati in un file scrivibile-mondo 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 '"'; 
0

SQL :

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

uscita:

+----------------------------+--------------+---------------------+ 
| 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   | 
+----------------------------+--------------+---------------------+ 
1

Query questo codice

select constraint_name, 
    table_schema, 
    table_name 
from information_schema.table_constraints 

Otterrete constraint_name, e filtrare il TABLE_SCHEMA che è l'elenco dei database.

Look at This

6

Questo è quello che io preferisco avere informazioni utili:

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' 
Problemi correlati