Figure out if a table has a DELETE on CASCADE

与世无争的帅哥 提交于 2019-12-06 17:14:12

问题


Can I know if a database have DELETE ON CASCADE with a query?


回答1:


Yes. Just query the INFORMATION_SCHEMA

SELECT * FROM information_schema.REFERENTIAL_CONSTRAINTS

Or more specifically

-- This query will list all constraints, their delete rule, 
-- the constraint table/column list, and the referenced table
SELECT 
  r.CONSTRAINT_NAME,
  r.DELETE_RULE, 
  r.TABLE_NAME,
  GROUP_CONCAT(k.COLUMN_NAME SEPARATOR ', ') AS `constraint columns`,
  r.REFERENCED_TABLE_NAME
FROM information_schema.REFERENTIAL_CONSTRAINTS r
  JOIN information_schema.KEY_COLUMN_USAGE k
  USING (CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME)
-- using MySQL's GROUP BY clause. In other DB's more columns would need to be
-- specified!
GROUP BY r.CONSTRAINT_CATALOG,
         r.CONSTRAINT_SCHEMA,
         r.CONSTRAINT_NAME

Read more about the REFERENTIAL_CONSTRAINTS table in the manual




回答2:


You could use

SHOW CREATE TABLE `tablename`

To get the entire definition of the table. This includes any foreign key constraints.



来源:https://stackoverflow.com/questions/13134913/figure-out-if-a-table-has-a-delete-on-cascade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!