确定一个表是否在级联上有一个DELETE

时间:2022-09-19 03:41:50

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

我能知道一个数据库是否有带查询的级联删除吗?

2 个解决方案

#1


19  

Yes. Just query the INFORMATION_SCHEMA

是的。只是查询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

在手册中阅读更多关于REFERENTIAL_CONSTRAINTS表的内容。

#2


8  

You could use

您可以使用

SHOW CREATE TABLE `tablename`

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

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

#1


19  

Yes. Just query the INFORMATION_SCHEMA

是的。只是查询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

在手册中阅读更多关于REFERENTIAL_CONSTRAINTS表的内容。

#2


8  

You could use

您可以使用

SHOW CREATE TABLE `tablename`

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

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