About database transactions and Foreign keys

Source: Internet
Author: User
This is the case: the_id of Table B in Table Aidint (11) Ba_idint (11) is the foreign key of Table A's primary key ----------- the division line ------------ I started mysql, then, an operation needs to insert A piece of data to Table A (hereinafter referred to as operation 1) before inserting data... the situation is as follows:

Table
Id int (11)

Table B
A_id int (11)

The a_id of Table B is the foreign key of Table A's primary key.

----------- The dividing line of all evil ------------

I enabledmysqlAnd then insert a data entry toTable(Hereinafter referred toOperation 1), And then insert the dataTable B(Hereinafter referred toOperation 2).

Operation 2 depends on the ID returned after operation 1 is successfully inserted

When a transaction is enabled, after the operation is successful, data is inserted into Table B. At this time, the foreign key is detected. Although the operation is successful, but the transaction has not been submitted, so there is no such data in Table A, so operation 2 will report an error...

How do you solve this problem (my current solution is to cancel the foreign key index !!!)?

Reply content:

The situation is as follows:

Table
Id int (11)

Table B
A_id int (11)

The a_id of Table B is the foreign key of Table A's primary key.

----------- The dividing line of all evil ------------

I enabledmysqlAnd then insert a data entry toTable(Hereinafter referred toOperation 1), And then insert the dataTable B(Hereinafter referred toOperation 2).

Operation 2 depends on the ID returned after operation 1 is successfully inserted

When a transaction is enabled, after the operation is successful, data is inserted into Table B. At this time, the foreign key is detected. Although the operation is successful, but the transaction has not been submitted, so there is no such data in Table A, so operation 2 will report an error...

How do you solve this problem (my current solution is to cancel the foreign key index !!!)?

I tried it. Operation 2 will not report an error. Look for another reason.

You can start two transactions for each of the two operations.

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.