information_schema.key_column_usage 学习
information_schema.key_column_usage 表可以查看索引列上的约束;
1、information_schema.key_column_usage 的常用列:
1、constraint_catalog :约束类型这个永远是def
2、constraint_schema :约束所在的数据库名
3、constraint_name :约束名
4、table_catalog :表类型这个永远是def
5、table_schema :表所在的数据库名
6、column_name :索引所在的列名
7、referenced_table_schema :被引用表所在的数据库名
8、referenced_table_name :被引用的表名
9、referenced_column_name :被引用表的列名
2、例子:
通过information_schema.key_column_usage表来查看外键引用关系
1、创建有主外键关系的表
create table teacher( id int not null auto_increment, name varchar(16) not null, primary key pk_teacher(id) ) engine=innodb default char set utf8; create table student( id int not null auto_increment, name varchar(16) not null, teacher_id int not null, constraint pk_student__id primary key(id), constraint fk_stuent__teacher_id foreign key(teacher_id) references teacher(id) ) engine=innodb default char set utf8;
2、查看索引列上的约束
select * from KEY_COLUMN_USAGE where table_schema='tempdb' \G *************************** 1. row *************************** CONSTRAINT_CATALOG: def CONSTRAINT_SCHEMA: tempdb CONSTRAINT_NAME: PRIMARY TABLE_CATALOG: def TABLE_SCHEMA: tempdb TABLE_NAME: student COLUMN_NAME: id ORDINAL_POSITION: 1 POSITION_IN_UNIQUE_CONSTRAINT: NULL REFERENCED_TABLE_SCHEMA: NULL REFERENCED_TABLE_NAME: NULL REFERENCED_COLUMN_NAME: NULL *************************** 2. row *************************** CONSTRAINT_CATALOG: def CONSTRAINT_SCHEMA: tempdb CONSTRAINT_NAME: fk_stuent__teacher_id TABLE_CATALOG: def TABLE_SCHEMA: tempdb TABLE_NAME: student COLUMN_NAME: teacher_id ORDINAL_POSITION: 1 POSITION_IN_UNIQUE_CONSTRAINT: 1 REFERENCED_TABLE_SCHEMA: tempdb REFERENCED_TABLE_NAME: teacher REFERENCED_COLUMN_NAME: id *************************** 3. row *************************** CONSTRAINT_CATALOG: def CONSTRAINT_SCHEMA: tempdb CONSTRAINT_NAME: PRIMARY TABLE_CATALOG: def TABLE_SCHEMA: tempdb TABLE_NAME: teacher COLUMN_NAME: id ORDINAL_POSITION: 1 POSITION_IN_UNIQUE_CONSTRAINT: NULL REFERENCED_TABLE_SCHEMA: NULL REFERENCED_TABLE_NAME: NULL REFERENCED_COLUMN_NAME: NULL