22

我可以知道数据库是否DELETE ON CASCADE有查询吗?

4

2 回答 2

32

是的。只需查询INFORMATION_SCHEMA

SELECT * FROM information_schema.REFERENTIAL_CONSTRAINTS

或者更具体地说

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

阅读手册中有关REFERENTIAL_CONSTRAINTS表格​​的更多信息

于 2012-10-30T08:14:34.377 回答
14

你可以使用

SHOW CREATE TABLE `tablename`

获取表的完整定义。这包括任何外键约束。

于 2012-10-30T08:14:27.707 回答