mysql主外键依赖关系

查看主外键的依赖关系:
在oracle中可以通过 DBA_DEPENDENCIES查看得到。 在mysql中也可以查看
通过查看 information_schema.  key_column_usage  


MariaDB [test]> create table t1 ( id int primary key,name varchar(10));
Query OK, 0 rows affected (0.36 sec)


MariaDB [test]> create table t2 (id int,score int);
Query OK, 0 rows affected (0.04 sec)

MariaDB [test]> Alter table t2 add foreign key (id) references t1(id);
Query OK, 0 rows affected (0.15 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test]> show create table t2;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                        |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `score` int(11) DEFAULT NULL,
  KEY `id` (`id`),
  CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`id`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


MariaDB [information_schema]> select * from key_column_usage where 
    -> REFERENCED_COLUMN_NAME='ID' and 
    -> REFERENCED_TABLE_NAME='T1'
    -> ;
+--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | POSITION_IN_UNIQUE_CONSTRAINT | REFERENCED_TABLE_SCHEMA | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
| def                | test              | t2_ibfk_1       | def           | test         | t2         | id          |                1 |                             1 | test                    | t1                    | id                     |
+--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
1 row in set (0.01 sec)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25099483/viewspace-1315196/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25099483/viewspace-1315196/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值