"SQL must know" Learning Note II)

Source: Internet
Author: User

"SQL must Know" learning notes (ii)

Let's move on to the last article. This article mainly reviews the sub-query, the joint query, the replication table three kinds of content.

The last part is basically a simple select query, which is a single statement that retrieves data from an individual database table, but the business logic in the actual application is often very complex, so it uses some more complex queries, such as subqueries and federated queries.

1. Sub-query

  When a query is a condition of another query, it is called a subquery. But when it comes to subqueries, not to mention the difference between it and nested queries, the following diagram illustrates

  

The following is an explanation of their relationship with an SQL statement.

  

In the query is divided into nested subqueries and related subqueries, the difference between them is whether the query depends on the external query, the execution of nested subqueries does not depend on the external query, and the execution of the correlated subquery depends on the external query.

Select *  from where < (Selectavgfrom-- search for all book information  above the average price

An SQL statement above is a nested subquery that analyzes his execution.

① executes the subquery first, that is, the average price of the book in the books table is queried, and then the result is passed to the parent query as a condition of the parent query.

② executes the parent query and returns the result.

Select *  from  as where < (SelectAVGfrom aswhere A. Class number =B. Class number)--  Book information that is larger than the average price of the book in the query table

The upper statement is a nested subquery that analyzes his execution.

① first reads a data from the parent query, that is, the class number, and then passes the class number to the subquery.

② executes a subquery, queries the average price of the book of this class number, and passes it to the parent query.

③ The parent query determines whether the data satisfies the condition, does not satisfy the exclusion, and satisfies the reservation.

④ then the parent query gets the class number in the next data and repeats the ① to the ③ step until all the outer data is processed.

2. Table Union Query

 One of the most powerful features of SQL is the ability to join tables in the execution of data queries. A junction is the most important operation that takes advantage of SQL's Select to understand the three paradigms of a database before being able to use it effectively.

1NF (atomicity): The field is no longer divided, otherwise it is not a relational database.

2NF (Uniqueness): There is a primary key, the non-primary key field is dependent on the primary key, or only one transaction can be described in a table.

3NF (no dependency): Primary key fields cannot depend on each other and cannot pass dependencies.

The three-paradigm is to solve the redundancy problem of database, but in many business logic, it is necessary to query two or more two tables at the same time, and then use the Joint (knot) query of the table.

Cartesian product (Cartesian product): The result of a table relationship without a junction condition is a Cartesian product. The number of rows that will be the first table multiplied by the number of rows in the second table.

Joint query, is to find out the Cartesian product of the table, after the Cartesian product screening.

The connection type is divided into several types, as shown in.

    • (Inner) Join: Returns a row if there is at least one match in the table
    • Left Join: Returns all rows from the table, even if there is no match in the right table
    • Right Join: Returns all rows from the correct table even if there is no match in the left table
    • Full Join: Returns a row if there is a match in one of the tables
    • Cross Join: A Cartesian product that returns two related tables

An inner join (Inner join) is the most common connection operation. There are two different kinds of syntax, one is to display the connection symbol, the other is the implicit link symbol, and the difference is to use the keyword without the join.

 

Grammar:Select <To select a field>  from <Main data Sheet> <Join method> <Secondary data table> [On <join rules >] --in the connection, the join rule will be excluded: exclusiveNotation 1:Select *  fromAInner JoinB onA.name=B.name--Show Connection SymbolsNotation 2:Select *  fromA, bwhereA.name=B.name--Implicit link symbols--In contrast, the inner join queries the common set C of both A and B tables

An outer join does not require that each record of the two tables connected is a matching record in the offset table, that the table to keep all records (even if the record does not match) is called a reserved table, and the connection to the left of the Join keyword is called the left connection, and on the right is called the right connection, and when both table records are saved, Called a full outer join. Left OUTER JOIN query, if there is no matching record in the right table, the value of all columns from the right table is set to NULL, right outer join, and full outer join is also the case.

Grammar:Select <To select a field>  from <Left data table> <left|right>[outer] Join <Right Data sheet> [On <join rules >] --in outer joins: inclusivenessLeft Outer connection:Select *  fromA Left(outer)JoinB onA.name=B.name--The control, or left outer join, queries all the sets of table A and B and a common set a1+cRight outer connection:Select *  fromA Right(outer)JoinB onA.name=B.name--Check out all the collections of table A and B and a public collection b1+c by the right outer joinFull Outer connection:Select *  fromA Full(outer)JoinB onA.name=B.name--All the collections of table A and all the collections in B are queried against the full outer join A1+C+B1

3. Copying a table

Sometimes it is necessary to import data from a table directly into a new table, which can be used select...into...from or insert into...select, but there is a difference between the two.

Select *  into  from Books        -- when using this statement, the database must not exist in the table newtable, if there is a table in the database, then an error insert into Select*from-- In this statement, the database must exist in the original table newtable, if not present, then perform an error

One more thing to note: When using a copy table statement, only the data and structure of the table are copied, but the primary key, foreign key, constraint, and index in the table are not copied.

"SQL must know" Learning Note II)

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.