SQL ---- EXISTS keyword

Transfer: http://blog.sina.com.cn/s/blog_65dbc6df0100mvfx.html

1.EXISTS basic meaning

  English meaning is that there is, but what he means is almost equivalent to the existential quantifier 'З'. He did not return data, when the query after the band is null, the return "FALSE", a non-empty returns "TRUE".

   Because it  is true or false value EXISTS returned, so he brought the sub-queries directly with the general 'select *' because given the column name did not mean much.

   In fact, EXISTS belonging to the relevant sub-queries, that depends on the conditions of the sub-query search attribute value in the outer query parent. For example:
SELECT Sname
from Student
WHERE EXISTS (
      SELECT *
      from SC                                 
      WHERE Sno = Student.Sno and Cno = '. 1');
said dependence is that a "Sno = Student.Sno". To explain this, the query process is not performed ordinary bottom-up, he relies outer query, the execution start time of the parent query fetch a tuple, then the conditions according to  the query within Sno = Student.Sno to give then the parent query results to take a second tuple, if repeated.

And may NOT used in conjunction with anti-sense understanding, in turn, such as:
the SELECT Sname
from Student
the WHERE not esists (
      * SELECT
      from SC
      WHERE Sno = Student.Sno and Cno = '. 1');

EXISTS subqueries can not be some other form of subqueries equivalent alternative, but all  IN, comparison operators, the ANY, and the ALL subqueries can be used with a query equivalence replacement of sub EXISTS. But in doing, consider efficiency, efficient use what kind ah, this is the optimization problem.

For example in a  first example tells IN, we can change it, becomes:
SELECT Sno, Sname, Sdept
from Student Sl
WHERE EXISTS (
SELECT *
from Student S2
WHERE S2.Sdept and S2 = S1.Sdept .Sname = 'a');

where, EXISTS just know whether the query can be a null value, the efficiency of use than the iN query to a number of efficient.

2. How to understand how the statement:

        SELECT Sname
        FROM Student
        WHERE NOT EXISTS
                    (SELECT *
                        FROM Course
                        WHERE NOT EXISTS
                                      (SELECT *
                                       FROM SC
                                       WHERE Sno= Student.Sno
                                             AND Cno= Course.Cno
                                       )
                       );

Examples of this object is to find whether there is a database record does not correspond to the three tables (the tables the following relationship should exist SC.Cno = Course.Cno, sc.Sno = Student.Sno

)

Its function and in somewhat similar , this statement into use in this way is , so it is easier to understand .

 

SELECT Sname FROM Student

WHERE Sno IN (SELECT Sno FROM SC WHERE Cno IN (SELECT Cno FROM Course))

3. exist and in difference

in and  exists also a good difference .

in a set operators .

A in {a, c, d , s, d ....}

this calculation , a front element , followed by a collection of element types is set as the previous element .

   And exists a presence determination If the outcome later in the query , then exists is true , false otherwise .

   in operation with the statement , following it with a select must choose a field , rather than select *.

   for example, you want to determine whether there is a class called "Xiao Ming students," you can use in arithmetic :

"Xiao Ming " in

(select sname from student)

这样(select sname from student)

Returns a collection of the class name , in a judgment "Bob " is a data set for this ;

at the same time , you can also use exists statement :

exists

(select * from student where sname="小明")

Both the number of Han is about the same , but because of different optimization schemes , usually NOT

EXISTS than NOT IN to be fast , because NOT EXISTS algorithm can be used in conjunction NOT IN to die, and EXISTS is not as good as IN fast ,

Because this time IN more likely to be used in conjunction algorithm .

select * from 表A where exists(select * from 表B

where table B.id = Table A.id)

sentence corresponds 

select * from table A where id in (select id

from 表B)

: Table for each of the data A , is executed ( SELECT * from Table B where

Table B.id = Table A.id) determining the presence of , if a table exists in table B A current row same id, it exists as a true , the line display , or not display

exits for large queries within a small outside, in small suitable for large outer query

IN
determine whether a given value or values in the list subquery match. 
EXISTS
specify a subquery, detect the presence of the line.


Compared using  EXISTS and  queries IN,
this example compares two similar semantic query. The first query uses  EXISTS and the second query uses  IN. Note that both queries return the same information. 

USE

pubs
GO
SELECT DISTINCT pub_name
FROM publishers
WHERE EXISTS
(SELECT *
FROM titles
WHERE pub_id = publishers.pub_id
AND type = 'business')
GO

=============================================================
-- Or, using the IN clause:
USE pubs
GO
SELECT distinct pub_name
FROM publishers
WHERE pub_id IN
(SELECT

the pub_id
the FROM the titles
the WHERE type = 'Business')
the GO

is the result set according to any one query: 
the pub_name
----------------------------- -----------
Algodata

Infosystems
New Moon Books

(2 row(s) affected)

existential quantifier exits equivalent: represents a set exists , is not empty act only one set example.  exist P

Indicates P is not empty true ; not exist P expressed is true empty p  represents the relationship between a scalar and a monobasic relationship in. For example: s in P represented when s is a value of P equal is true ; s

not in P represented by s and is true of each value is not equal to P

Guess you like

Origin www.cnblogs.com/sharpest/p/11406777.html