A simple mysql Data Problem

Source: Internet
Author: User
If the people table idpeoplenameorders table idproductnamepeople (whether to store the people id or directly copy the field) directly put the id, what if the people is deleted? If you copy a field directly, if you modify the data in the people table, you must also modify the orde... people table.
Id peoplename

Orders table
Id productname people (whether to store the people id or directly copy the field)

If you put the id directly, what if people is deleted?
If you copy a field directly, if you modify the data in the people table, do you also need to modify the data in the orders table?

Are these two understandings correct?
What do you do during actual operations?

Thank you.

Reply content:

People table
Id peoplename

Orders table
Id productname people (whether to store the people id or directly copy the field)

If you put the id directly, what if people is deleted?
If you copy a field directly, if you modify the data in the people table, do you also need to modify the data in the orders table?

Are these two understandings correct?
What do you do during actual operations?

Thank you.

First, select Put id and add foreign key constraints. What should I do if people is deleted if you say "Put id directly"? mysql has three options on foreign key constraints:
1. CASCADE: The people table is deleted, and records of the corresponding order table are also deleted;
2. set null: The people table is deleted, and the columns of records in the corresponding order table are left empty;
3. RESTRICT (no action): If the order table has corresponding records, deletion of the people table is not allowed.
See: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

This may solve your problem.

Directly copy a field to the people table, unless the people and order table operation here does become a performance bottleneck, and the field copied in the actual test can indeed improve the performance. this adds redundant data. Disadvantages: 1. increase the required storage space; 2. you must maintain the redundant fields of the order and people tables.

It should be

people:        id     peoplenameorders:    id    productname    pid       comment "people id"

If people is deleted, related orders should also be deleted. If they are saved, redundant data will be generated.
Is it possible that people has been deleted? Users are generally not deleted.

Agree that people upstairs will not be deleted. If you really want to delete people, you can add Transaction Management in the program, cascade the deletion of people and order, and roll back if the deletion fails. Alternatively, you can only use a foreign key and add cascade deletion constraints.

The id of people stored in the orders table. We recommend that you do not delete the people table directly when you modify it. Add an inuse field to indicate whether the value is valid.

We recommend that you design the table structureBeginnersFirst, learn the three paradigm

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.