How do I reasonably update two associated tables when MySQL lacks a foreign key constraint?

Source: Internet
Author: User
I have a demand that is probably the case:

There is an article table and a category table, and the article table has a CAT_ID association category table. Here, the MySQL layer does not have a foreign key constraint. In this case, every time you create an article table, what do you usually do with this cat_id?

Some of my options:
1, to check the category table, if this category does not exist, do not let the update. (optimized: If the number of categories is not much, put in memory directly, or use the cache)
2, do not take care of this category has not existed.

Update:
1, is to do a forum system, users can publish articles, and the category is mainly by the operation of the people to create.

Reply content:

I have a demand that is probably the case:

There is an article table and a category table, and the article table has a CAT_ID association category table. Here, the MySQL layer does not have a foreign key constraint. In this case, every time you create an article table, what do you usually do with this cat_id?

Some of my options:
1, to check the category table, if this category does not exist, do not let the update. (optimized: If the number of categories is not much, put in memory directly, or use the cache)
2, do not take care of this category has not existed.

Update:
1, is to do a forum system, users can publish articles, and the category is mainly by the operation of the people to create.

I usually use the second kind of direct neglect

If the rigor of the data on the business requirements are high, of course, the first
If the requirements are not very high, the second one is fine.
First, when you save to the library, the previous business will generally ensure that the ID is active and traceable;
Again even if this ID corresponds to the category does not exist or later this category is deleted do not do cascade Delete, the final effect is only in the query this category is not found data, usually and the ID empty effect close

As your first option, check the category table, the categories are generally not too large, you can use the cache

1. Check the category table if not, the key value is inserted into the 0 article
2. When displaying the uncategorized category (i.e., the key value 0 of the article)

  • 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.