Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 60 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
60
Dung lượng
909 KB
Nội dung
COP 4710: DatabaseSystems (Day 18) Page 1 Mark Llewellyn
COP 4710: Database Systems
Spring 2004
Introduction to SQL – Part 2
BÀI 14, 2 ngày
COP 4710: Database Systems
Spring 2004
Introduction to SQL – Part 2
BÀI 14, 2 ngày
School of Electrical Engineering and Computer Science
University of Central Florida
Instructor : Mark Llewellyn
markl@cs.ucf.edu
CC1 211, 823-2790
http://www.cs.ucf.edu/courses/cop4710/spr2004
COP 4710: DatabaseSystems (Day 18) Page 2 Mark Llewellyn
An Example Database(+)
COP 4710: DatabaseSystems (Day 18) Page 3 Mark Llewellyn
Special Operators in SQL
•
ANSI standard SQL allows the use of special operators in
conjunction with the WHERE clause. These special
operators (see Day 17, page26) include:
BETWEEN – Used to check whether an attribute value is within a
range.
IS NULL – Used to determine if an attribute value is null.
LIKE – Used to match an attribute value to a string pattern. Many
wildcard options are available.
IN – Used to determine if an attribute value is within a list of values.
EXISTS – Used to determine if a subquery returns an empty set or
not.
COP 4710: DatabaseSystems (Day 18) Page 4 Mark Llewellyn
The BETWEEN Special Operator(+)
•
Suppose that we want to see a listing for all products whose
prices are between $50 and $100. The BETWEEN operator
can be used for this query expression.
•
If your RDBMS does not support BETWEEN you would
need to express this query as:
SELECT *
FROM PRODUCT
WHERE P_PRICE BETWEEN 50.00 AND 100.00;
SELECT *
FROM PRODUCT
WHERE P_PRICE > 50.00 AND P_PRICE < 100.00;
COP 4710: DatabaseSystems (Day 18) Page 5 Mark Llewellyn
The IS NULL Special Operator(+)
•
Suppose that we want to see a listing for all products that do
not currently have a vendor assigned, i.e., V_CODE = null.
The null entries could be found with the following query
expression.
•
NOTE: SQL uses a special operator for testing for nulls.
You cannot use a condition such as V_CODE = NULL. The
reason is that NULL is technically not a “value”, but a
special property of an attribute that represents precisely the
absence of any value at all.
SELECT P_CODE, P_DESCRIPT, V_CODE
FROM PRODUCT
WHERE V_CODE IS NULL;
COP 4710: DatabaseSystems (Day 18) Page 6 Mark Llewellyn
The LIKE Special Operator(+)
•
The LIKE special operator is used in conjunction with
wildcards to find patterns within string attributes.
•
Standard SQL allows you to use the percent sign (%) and
underscore (_) wildcard characters to make matches when
the entire string is not known.
% means any and all following characters are eligible.
‘M%’ includes Mark, Marci, M-234x, etc.
_ means any one character may be substituted for the underscore.
‘_07-345-887_’ includes 407-345-8871, 007-345-8875
•
Note: Access uses * instead of % and ? instead of _. Oracle
searches are case-sensitive, Access searches are not.
COP 4710: DatabaseSystems (Day 18) Page 7 Mark Llewellyn
The LIKE Special Operator (cont.)(+)
•
Suppose that we would like to find all the VENDOR rows
for contacts whose last names begin with Smith.
SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONE
FROM VENDOR
WHERE V_CONTACT LIKE ‘Smith%’;
COP 4710: DatabaseSystems (Day 18) Page 8 Mark Llewellyn
The IN Special Operator(+)
•
Many queries that would seem to require the use of the
logical OR operator can be more easily handled with the help
of the special operator IN.
•
For example the query:
can be handled more efficiently with:
SELECT *
FROM PRODUCT
WHERE V_CODE = 21344 OR V_CODE = 24288;
SELECT *
FROM PRODUCT
WHERE V_CODE IN (21344, 24288);
COP 4710: DatabaseSystems (Day 18) Page 9 Mark Llewellyn
The IN Special Operator (cont.)(+)
•
The IN operator is especially valuable when it is used in conjunction
with subqueries.
•
For example, suppose you want to list the V_CODE and V_NAME of
only those vendors that actually provide products. In this case, you
could use a subquery within the IN operator to automatically generate the
value list. The query expression would be:
•
We’ll look more closely at the IN operator later when we deal more in
depth with subqueries.
SELECT V_CODE, V_NAME
FROM VENDOR
WHERE V_CODE IN ( SELECT V_CODE
FROM PRODUCT);
COP 4710: DatabaseSystems (Day 18) Page 10 Mark Llewellyn
The EXISTS Special Operator(+)
•
The EXISTS operator can be sued whenever there is a
requirement to execute a command based on the result of
another query. That is, if a subquery returns any rows, then
run the main query, otherwise, don’t. We’ll see this operator
in more detail when we look at subqueries in more depth.
•
For example, suppose we want a listing of vendors, but only
if there are products to order. The following query will
accomplish our task.
SELECT *
FROM VENDOR
WHERE EXISTS ( SELECT *
FROM PRODUCT
WHERE P_ONHAND <= P_MIN);
[...]... of pages COP 4710: DatabaseSystems (Day 18) query UNION query Page 26 Mark Llewellyn Union Operator (cont.) COP 4710: DatabaseSystems (Day 18) Page 27 Mark Llewellyn Union Operator (cont.) The result of the UNION of the CUSTOMER and CUSTOMER_2 tables Customer names Dunne and Olowski appear in both original tables and thus appear only once in the union result COP 4710: DatabaseSystems (Day 18) Page... qualified attribute names are not unique COP 4710: DatabaseSystems (Day 18) Page 17 Mark Llewellyn Recursive Joins (cont.)(+) Creating an alias using Access notation COP 4710: DatabaseSystems (Day 18) Page 18 Mark Llewellyn Outer Joins • The query results shown on page 23 resulted from the natural join of the PRODUCT and VENDOR tables Notice that there are 14 product rows listed in this output If you compare... all PRODUCT rows with all matching VENDOR rows COP 4710: DatabaseSystems (Day 18) Page 23 Mark Llewellyn Right Outer Joins (cont.) (+) Result shows all rows from PRODUCT with all matching rows from VENDOR (right outer join) COP 4710: DatabaseSystems (Day 18) Page 24 Mark Llewellyn Relational Set Operators • Recall that relational algebra is set-oriented and includes many set operators such as union,... MIN(P_OHAND) AS MINQTY, AVG(P_ONHAND) AS AVGQTY FROM PRODUCT GROUP BY V_CODE; COP 4710: DatabaseSystems (Day 18) Page 13 Mark Llewellyn Joining Database Tables • The ability to combine (join) tables on common attributes is perhaps the most important distinction between a relational database and other types of databases • In SQL, a join is performed whenever data is retrieved from more than one table... Systems (Day 18) Page 32 query MINUS query Mark Llewellyn Set Difference Operator (cont.) Results of the set difference query from the previous page COP 4710: DatabaseSystems (Day 18) Page 33 Mark Llewellyn An Example Database( +) COP 4710: DatabaseSystems (Day 18) Page 34 Mark Llewellyn SQL Join Operations • The SQL join operations merge rows from two tables and returns the rows that: 1 Have common values... full outer joins later COP 4710: Database Systems (Day 18) Page 19 Mark Llewellyn Left Outer Joins(+) • To include the null valued V_CODE tuples from the PRODUCT table in the final output, we’ll need to issue the following query: Note: The word “outer” does not appear in the query It is simply either a left join or a right join, the outer is implied COP 4710: Database Systems (Day 18) Page 20 Mark Llewellyn... matching rows from PRODUCT (left outer join) COP 4710: Database Systems (Day 18) Page 21 Mark Llewellyn Right Outer Joins (+) • The VENDOR table is shown below Notice that there are rows in this table in which the V_CODE does not match any of the V_CODE values in the PRODUCT table These vendors do not appear in the PRODUCT table COP 4710: Database Systems (Day 18) Page 22 Mark Llewellyn Right Outer Joins... is called the join condition • The join condition is generally composed of an equality comparison between the foreign key and the primary key in the related tables COP 4710: Database Systems (Day 18) Page 14 Mark Llewellyn Joining Database Tables (cont.)(+) • Suppose we want to join the VENDOR and PRODUCT tables V_CODE is the foreign key in the PRODUCT table and the primary key in the VENDOR table, the... normally only required where the same attribute appears in more than one of the joined relations COP 4710: DatabaseSystems (Day 18) Page 15 Mark Llewellyn Joining Database Tables (cont.) • If you do not specify a join condition in the WHERE clause, a Cartesian product results Using our sample database, the PRODUCT table contains 16 tuples (rows) and the VENDOR table contains 11 tuples, which results... Olowski appear twice since duplicates are not removed in this form of UNION COP 4710: DatabaseSystems (Day 18) Page 29 Mark Llewellyn Intersect Operator • The syntax of an INTERSECT query is: • Access does not support the INTERSECT statement intersection in Access you need to use the IN operator COP 4710: DatabaseSystems (Day 18) Page 30 query INTERESCT query To effect an Mark Llewellyn Intersect . Database Systems (Day 18) Page 1 Mark Llewellyn
COP 4710: Database Systems
Spring 2004
Introduction to SQL – Part 2
BÀI 14, 2 ngày
COP 4710: Database Systems
Spring. Mark, Marci, M-234x, etc.
_ means any one character may be substituted for the underscore.
‘_0 7-3 4 5-8 87_’ includes 40 7-3 4 5-8 871, 00 7-3 4 5-8 875
•
Note: