MySQL linked list statement--blog Park Old Bull Lecture Hall

Source: Internet
Author: User

Why linked list? --Blog Park Lao Niu Big Lecture Hall

Because the table has a relationship to the table, and the query requires some data from two tables.

The list is based on the premise that the primary foreign key must be set between the table and the table?

No, in fact, the table and table do not need to set the primary foreign key relationship, with database statements can be implemented linked list query, delete, modify, increase and so on.

Why set the primary foreign key?

Usually we see a relationship between a table and a table, and we often set the primary foreign key. Why? In fact, this is to standardize! Suppose a person who does not understand the structure of your table can modify your foreign keys arbitrarily. That's not a serious watch.

Are we going to set the main foreign key?
Sub-conditions: 1, if the table structure is simple, a small number of tables. Logic is not complex. Then this does not need to set the primary foreign key. Especially for those unfamiliar with database statements, it is much more convenient.

2, if the table structure is complex, there are a large number of tables, logic complex. Then it is impossible to remember the relationship between all the primary foreign keys, then you need to set the primary foreign key.

How to implement linked list query?

Linked list query is divided into: Leftist table, right linked list. such as.

  Linked lists can not only be queried, but also linked list queries, linked list additions, linked list deletion, linked list modification.

Example a 、--blog Park Lao Niu da Lecture Hall

(leftist table), now has two tables, a table and B table, a table main fields are: Id,a?1,a2,a3. B table fields are: id,b1,b1_id. where the ID of a table is the primary key, B1_ID is the foreign key. as

Idea: Left join is based on the left of a table, query out the associated data. The excess data is cleared, and the missing data is empty.

Zolin Query Results:

  

Example two 、--Blog Park Lao Niu da Lecture Hall

(Right-hand table), now has two tables, a table and B table, a table main fields are: Id,a?1,a2,a3. B table fields are: id,b1,b1_id. Where the ID of a table is the primary key, B1_ID is the foreign key. The figure is as shown above

Idea: The right connection is based on the right side of the B table, the principle is similar to the left join.

If the result of a right connection is:

Example Four 、--Blog Park Lao Niu da Lecture Hall

Above are all linked list query results, below I said a list delete operation.

Title: Existing Two tables: Chapters table: ID (primary key), name. Division table: ID (primary key), name_id (foreign key for chapters), part.

Requirements: Implement the list delete operation.

Statement: DELETE C,d from chapters C joins Division D on C.ID=D.NAME_ID WHERE c.id=1

MySQL linked list statement--blog Park Old Bull Lecture Hall

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.