使用特定外键选项查询所有约束

时间:2015-04-02 19:20:47

标签: mysql sql database

我在MySql中有几个带外键的表。例如:

CREATE TABLE rooms (
  room_no int(11) NOT NULL AUTO_INCREMENT,
  room_name varchar(255) NOT NULL,
  building_no int(11) NOT NULL,
  PRIMARY KEY (room_no),
  KEY building_no (building_no),
CONSTRAINT rooms_ibfk_1 
  FOREIGN KEY (building_no) 
  REFERENCES buildings (building_no) 
  ON DELETE CASCADE
) ENGINE=InnoDB;

我想查询具有foriegn键约束的架构中的每个表,并知道其他选项。 我尝试了以下查询:

select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE;

select *
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'FOREIGN KEY';

不幸的是,他们没有给我删除或更新操作。

1 个答案:

答案 0 :(得分:0)

如果您的表是INNODB(我假设它们是外键),请查看INFORMATION_SCHEMA.SYS_FOREIGN。在文档中,type字段为您提供ON DELETE / ON UPDATE操作:

  

一组位标志,包含有关外键列的信息,ORed在一起。 1 = ON DELETE CASCADE,2 = ON UPDATE SET NULL,4 = ON UPDATE CASCADE,8 = ON UPDATE SET NULL,16 = ON DELETE NO ACTION,32 = ON UPDATE NO ACTION。

您可以FOR_NAMEREF_NAME进行查询。