4

我很想知道是否有办法告诉哪些列已使用 mysql 设置为 CASCADE。做 DESC 表;似乎没有产生任何明显的东西。有什么建议么?

4

2 回答 2

4

从 MySQL 5.1.10 开始,您可以通过加入以下方式从INFORMATION_SCHEMA中获取此信息:REFERENTIAL_CONSTRAINTSKEY_COLUMN_USAGE

SELECT kcu.COLUMN_NAME
FROM   INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
  JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
 USING (CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME)
WHERE  'CASCADE' IN (rc.DELETE_RULE, rc.UPDATE_RULE)
   AND kcu.TABLE_SCHEMA = DATABASE()
   AND kcu.TABLE_NAME = 'my_table'
于 2012-08-31T05:39:11.900 回答
2

您可以使用:

show create table tableName;

这将输出一个创建表,其中包含对级联删除的引用。

从文档中获取的示例:

mysql> SHOW CREATE TABLE ibtest11c\G
*************************** 1. row ***************************
       Table: ibtest11c
Create Table: CREATE TABLE `ibtest11c` (
  `A` int(11) NOT NULL auto_increment,
  `D` int(11) NOT NULL default '0',
  `B` varchar(200) NOT NULL default '',
  `C` varchar(175) default NULL,
  PRIMARY KEY  (`A`,`D`,`B`),
  KEY `B` (`B`,`C`),
  KEY `C` (`C`),
  CONSTRAINT `0_38775` FOREIGN KEY (`A`, `D`)
REFERENCES `ibtest11a` (`A`, `D`)
ON DELETE CASCADE ON UPDATE CASCADE,    // <--- This is what you are looking for
  CONSTRAINT `0_38776` FOREIGN KEY (`B`, `C`)
REFERENCES `ibtest11a` (`B`, `C`)       // <--- This is what you are looking for
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB CHARSET=latin1
1 row in set (0.01 sec)
于 2012-08-31T05:35:39.360 回答