7 Data Query database jobs

Join query: while involving more than two tables query

  • Connection conditions: Conditions for connecting two tables
[<表名1>.]<列名1>  <比较运算符>  [<表名2>.]<列名2>
如课件上的例题【例】 Student.Sno = SC.Sno
  • Field connection: column name in the connected condition
    such as: Sno field is connected in the above example
    Note: connection field type must be comparable, but not necessarily the same name

Equijoins: join operator is =
(equivalent relational algebra connected in Comparative)

[Example 3.49] circumstances query each student and elective courses

         SELECT  Student.*, SC.*
         FROM     Student, SC
         WHERE  Student.Sno = SC.Sno;
  • Nested loop method
    first finds the first tuple in Table 1, Table 2, and then scanned from the beginning one by one to find tuples satisfying the connecting member, after the table to find the first tuple in the tuple splicing 1 together to form a tuple in the result table.
    After all of the lookup Table 2, Table 1 to find the second tuple, and then start over again scan the table 2 one by one to find tuples satisfy the join condition, after found in Table 1 and the second tuple tuple spliced together to form a tuple in the result table.
    Repeat until all tuples in Table 1 have been processed

  • Sort-merge method (SORT-MERGE)
    first by connection properties of Tables 1 and 2 sorting
    table of the first element group 1, 2 from the beginning scan table, a sequential search tuples satisfy the join condition, after found in Table 1 the first tuple in the tuple spliced together to form a tuple in the results table. When encountered in Table 2 Table 1 tuple is greater than the first connecting field value, the query of Table 2 does not continue
    to find the second tuple in Table 1, and then continues sequentially scanning the table from the breakpoint just 2 find tuples satisfying the join condition, after the table to find a first tuple of the tuple spliced together to form a tuple in the results table. Direct tuple encountered in Table 2. Table 1 is connected is greater than the value of the field, a query of Table 2 does not continue to
    repeat the operation until all of Table 1 or Table 2 tuples have been processed so far

  • Connection index (INDEX-JOIN)
    of Table 2 are connected by the field index established
    for each tuple in Table 1, which is connected in turn based on the index field values of a lookup table 2, to find tuples satisfying the condition, found after the table a first tuple in the tuple spliced together to form a tuple result table

[Example 3.50] to [Example 3.49] natural complete connection.

 SELECT  Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
 FROM     Student,SC
 WHERE  Student.Sno = SC.Sno;
  • A SQL statement can choose to complete the connection and query the same time.
    Execution process:
    start the SC selected Cno = '2' and Grade> tuples intermediate 90 is formed a relationship
    tuple and then join condition satisfies Student connecting end results relationship

[Example 3.51] Query No. 2 elective courses and grades for all students to learn numbers and names of 90 points or more.

SELECT Student.Sno, Sname
FROM     Student, SC
WHERE  Student.Sno=SC.Sno  AND    		               
               SC.Cno=' 2 ' AND SC.Grade>90;
  • Itself connected: a table connected to its own
    needs to the table aliases to distinguish
    all the property names are the property of the same name, you must use the "Alias"

[Example 3.52] Prerequisite indirect query each course (i.e., the Prerequisite Prerequisite)

SELECT  FIRST.Cno, SECOND.Cpno
 FROM  Course  FIRST, Course  SECOND
 WHERE FIRST.Cpno = SECOND.Cno;
  • Connected to the common external connection distinction
    general connection output operation only satisfy the join condition tuple
    outer join operation to specify a table connector body, the connector body does not satisfy the condition table output together tuple

  • Left outer join
    lists all tuples left Relations

  • Right outer join
    list all the tuples in the right relation

[Example 3.53] rewrite [Example 3.49]

SELECT Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
FROM  Student  LEFT OUT JOIN SC ON  (Student.Sno=SC.Sno); 

[Example 3.54] to query each student number, name, elective courses and grades name

  SELECT Student.Sno, Sname, Cname, Grade
   FROM    Student, SC, Course    
   WHERE Student.Sno = SC.Sno 
                  AND SC.Cno = Course.Cno;

A nested query SELECT-FROM-WHERE query statement called a block
query in a query block nested condition in the WHERE clause of a query block further HAVING or phrase is called nested queries

 SELECT Sname	                           /*外层查询/父查询*/
 FROM Student
 WHERE Sno IN
                    ( SELECT Sno        /*内层查询/子查询*/
                      FROM SC

                      WHERE Cno= ' 2 ');

Not correlated subquery: query subquery is not dependent on the parent query from the inside out
layer by layer. That is, each sub-query before the query processing to solve a result of the sub-query search criteria used to establish the parent query.

   SELECT Sno, Sname, Sdept
    	FROM Student
   	WHERE Sdept  IN
                  (SELECT Sdept
                   FROM Student
                   WHERE Sname= ' 刘晨 ');

Correlated subquery: query subquery dependent on the parent query
first tuple first outer query takes the table in accordance with its associated attribute value query Inlay and inner query, returns true if the WHERE clause , the tuples take this into the result table
and then take the next tuple outer table this process is repeated until all the checks have been exhausted outer table

      SELECT Sno, Cno
      FROM    SC  x
      WHERE Grade >=(SELECT AVG(Grade) 
	                                       FROM  SC y
                                      WHERE y.Sno=x.Sno);

[Example 3.55] query in student learning with a line "Liu Chen."

          
                       SELECT  Sdept  
                       FROM     Student                            
                       WHERE  Sname= ' 刘晨 ';
                       SELECT   Sno, Sname, Sdept     
                       FROM      Student                 
                       WHERE   Sdept= ' CS '; 

or

                      SELECT Sno, Sname, Sdept
    	              FROM Student
   	                  WHERE Sdept  IN
                      (SELECT Sdept
                      FROM Student
                      WHERE Sname= ' 刘晨 ');

or

SELECT  S1.Sno, S1.Sname,S1.Sdept
      FROM     Student S1,Student S2
      WHERE  S1.Sdept = S2.Sdept  AND
                      S2.Sname = '刘晨';

[Example 3.56] query elective course called "information system" student number and name

SELECT Sno,Sname                                            
FROM    Student                             
WHERE Sno  IN
  (SELECT Sno                
   FROM    SC                 
   WHERE  Cno IN
   (SELECT Cno          
   FROM Course          
 WHERE Cname= '信息系统'                      
   )
  ):
  • The code above ideas divided into three steps:
    . ① first identify "information system" in the number of courses Course relations, as No. 3
    and then find out the relationship in SC ② choose to repair the No. 3 course student number
    ③ Finally, Student Sno relationship elected and Sname

Connection query implementation [Example 3.56]:

SELECT Sno,Sname
FROM    Student,SC,Course
WHERE Student.Sno = SC.Sno  AND
SC.Cno = Course.Cno AND
Course.Cname='信息系统';

[Example 3.57] to identify each student over his elective course grade point average number of courses.

   
   SELECT Sno, Cno
   FROM    SC  x
   WHERE Grade >=(SELECT AVG(Grade) 
   FROM  SC y
   WHERE y.Sno=x.Sno);

[Example 3.58] non-computer science department inquiry Department of Computer Science than a little arbitrary age of student names and ages of students

SELECT Sname,Sage
FROM    Student
WHERE Sage < ANY (SELECT  Sage
 FROM    Student
 WHERE Sdept= ' CS ')
 AND Sdept <> ‘CS ' ;         

[Example 3.59] query-based non-computer science students of all ages are less than the Department of Computer Science of the student's name and age.

  • Method One: Use the ALL predicate to achieve
SELECT Sname,Sage
FROM Student
WHERE Sage < ALL
(SELECT Sage
FROM Student
WHERE Sdept= ' CS ')
AND Sdept <> ' CS ’;
  • Method 2: The functions implemented in the form of aggregate
SELECT Sname,Sage
FROM Student
WHERE Sage < 
 (SELECT MIN(Sage)
 FROM Student
 WHERE Sdept= ' CS ')
 AND Sdept <>' CS ';

Multi-table query data in question in the implementation of the gradual diversification
need more practice thinking in different ways

Published 11 original articles · won praise 6 · views 2709

Guess you like

Origin blog.csdn.net/lrx359641708/article/details/105027349