首页 > 其他 > 详细

外键查询及删除

时间:2015-08-07 21:44:53      阅读:290      评论:0      收藏:0      [点我收藏+]

```
SELECT Concat(TABLE_NAME, ‘.‘, COLUMN_NAME) AS ‘foreign key‘,
Concat(referenced_table_name, ‘.‘, referenced_column_name) AS ‘references‘
FROM information_schema.key_column_usage
WHERE table_schema = ‘$databasename‘
AND referenced_table_name IS NOT NULL;

```

其中, ‘$databasename‘ 要替换成您要查询的那个数据库的名称。

 

```
mysql> SHOW CREATE TABLE mk_big_sent_visitor\G
*************************** 1. row ***************************
Table: mk_big_sent_visitor
Create Table: CREATE TABLE `mk_big_sent_visitor` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mk_big_sent_id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_mk_big_sent_visitor_mk_big_sent1_idx` (`mk_big_sent_id`),
KEY `fk_mk_big_sent_visitor_customer1_idx` (`customer_id`),
CONSTRAINT `fk_mk_big_sent_visitor_customer1` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_mk_big_sent_visitor_mk_big_sent1` FOREIGN KEY (`mk_big_sent_id`) REFERENCES `mk_big_sent` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘‘
1 row in set (0.00 sec)
```


删除外键:

```
ALTER TABLE mk_prize DROP FOREIGN KEY fk_think_mk_prize_shopweb1,

DROP FOREIGN KEY fk_mk_big_sent_visitor_mk_big_sent1;
```

外键查询及删除

原文:http://www.cnblogs.com/martinjinyu/p/4711756.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!