Foreign key Query and delete

Source: Internet
Author: User

```
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;

```

Where ' $databasename ' is replaced by the name of the database you want to query.

```
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 (one) not NULL auto_increment,
' mk_big_sent_id ' int (one) is not NULL,
' customer_id ' int (one) is 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 actio N 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)
```


To delete a foreign key:

```
ALTER TABLE mk_prize DROP FOREIGN KEY fk_think_mk_prize_shopweb1,

DROP FOREIGN KEY fk_mk_big_sent_visitor_mk_big_sent1;
```

Foreign key Query and delete

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.