简体   繁体   中英

How do I see the ON DELETE and ON UPDATE for a foreign key in MySQL?

I'm using this query to find foreign key relationships:

SELECT *
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE `REFERENCED_TABLE_SCHEMA` = ? AND 
      `TABLE_NAME` = ? AND 
      `REFERENCED_TABLE_NAME` IS NOT NULL

It's giving me most of what I need, but the two fields I am missing are

  1. on update
  2. on delete

How do I find those properties on a foreign key constraint?

You can use the INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS table that has the following columns:

    CONSTRAINT_CATALOG      
    CONSTRAINT_SCHEMA        
    CONSTRAINT_NAME      
    UNIQUE_CONSTRAINT_CATALOG       
    UNIQUE_CONSTRAINT_SCHEMA         
    UNIQUE_CONSTRAINT_NAME       
    MATCH_OPTION         
    UPDATE_RULE      
    DELETE_RULE      
    TABLE_NAME       
    REFERENCED_TABLE_NAME

By joining on CONSTRAINT_SCHEMA and CONSTRAINT_NAME , you can get the UPDATE_RULE and DELETE_RULE .

Here , in the document.

You didn't say, but I assume you're looking for something you can access programmatically?

If not, I'm pretty sure SHOW CREATE TABLE ? will give you all the info you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM