MySQL Delete method of foreign key definition

Source: Internet
Author: User
Tags mysql delete

MySQL foreign key after the set, if we no longer need this foreign key, can be deleted, the following will introduce you to the MySQL delete foreign key definition method, for your reference.

Do not know whether we have found that the foreign key in the definition of the foreign key articles.member_id than the ARTICLES.CATEGORY_ID clause more than a constraint fk_member?
This fk_member is used to implement the MySQL delete foreign key, as follows:

    1. MySQL> ALTER TABLE articles DROP FOREIGN KEY fk_member;
    2. Query OK, 1 row affected (0.25 sec)
    3. Records:1 duplicates:0 warnings:0

This articles.member_id the foreign key definition is deleted, but if the definition does not specify constraint Fk_symbol (that is, the foreign key symbol) when the implementation of the MySQL delete foreign key? Don't worry, MySQL will create one yourself when not specified, and can be viewed with the following commands:

  1. MySQL> SHOW CREATE TABLE articles;
  2. + ———-+ ———————————— +
  3. | Table | Create Table |
  4. + ———-+ ———————————— +
  5. | Articles | CREATE TABLE ' articles ' (
  6. ' article_id ' int (one) unsigned not NULL auto_increment,
  7. ' category_id ' tinyint (3) unsigned not NULL,
  8. ' member_id ' int (one) unsigned not NULL,
  9. ' title ' varchar (255) Not NULL,
  10. PRIMARY KEY (' article_id '),
  11. KEY ' category_id ' (' category_id '),
  12. KEY ' member_id ' (' member_id '),
  13. CONSTRAINT ' Articles_ibfk_1 ' FOREIGN KEY (' category_id ') REFERENCES ' categories ' (' ID ')
  14. ) engine=InnoDB DEFAULT charset=latin1 |
  15. + ———-+ ———————————— +
  16. 1 row in Set (0.01 sec)

You can see that the articles.category_id foreign key symbol is articles_ibfk_1, because you can execute the following command to implement the MySQL delete foreign key definition:

      1. MySQL> ALTER TABLE articles DROP FOREIGN KEY articles_ibfk_1;
      2. Query OK, 1 row affected (0.66 sec)
      3. Records:1 duplicates:0 warnings:0

MySQL Delete method of foreign key definition

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.