Download presentation
Presentation is loading. Please wait.
Published byMichael Caldwell Modified over 6 years ago
1
CH 9 SQL 9.1 Querying A Single Table 9.2 Querying Multiple Tables
9.3 Changing Data
2
SQL(Structured Query Language)
Endorsed by ANSI(American National Standard Institute) Data access language used by DB2, SQL/DS, ORACLE, INGRES, SYBASE, SQL Server, dBASE for Windows, Paradox, Microsoft Access The development of SQL began at IBM’s San Jose research facilities in mid-1970s under the name SEQUEL In 1980 the product was renamed SQL SQL92(CH. 9 discuss this version)-1992 SQL3 (Most recent version, discussed in CH. 17)- 1997 jhgfjhg
3
SQL’s commands and expressions
SQL is not a programming language; rather it is a data sub-language, or data access language, that is embedded in other languages Expressions may differ in minor ways from the ANSI standard (e.g. Oracle or SQL Server) SQL commands can be used interactively as a query language Can be embedded in application programs CH 9 only concerned with data manipulation statements (although there are also SQL commands for data definition and control) jhgfjhg
4
9.1 Querying A Single Table
9.1.1 SQL Projections 9.1.2 SQL Selection 9.1.3 Sorting 9.1.4 Built-in function 9.1.5 Grouping jhgfjhg
5
Figure 9-1 SQL Example Relations 1. JUNIOR (Snum, Name, Major)
2. HONOR-STUDENT (Number, Name,Interest) 3. STUDENT (SID, Name, Mahor, GradeLevel, Age) 4. CLASS (NAME, Time, Room) 5. ENROLLMENT (StudentNumber, ClassName, PositionNumber) 6. FACULTY (FID, Name, Department) jhgfjhg
6
jhgfjhg
7
Sample data used for SQL example (a) STUDENT Relation
100 JONES HISTORY GR 21 150 PARKS ACCOUNTING SO 19 200 BAKER MATH 50 250 GLASS SN 300 41 350 RUSSELL JR 20 400 RYE FR 18 450 24 SID Name Major GradeLevel Age jhgfjhg
8
(b) ENROLLMENT (c) CLASS StudentNumber ClassName PositionNumber
100 BD445 1 150 BA200 200 2 CS250 300 CS150 400 BF410 450 3 StudentNumber ClassName PositionNumber (c) CLASS BA200 M-F9 SC110 BD445 MWF3 SC213 BF410 MWF8 CS150 EA304 CS250 MWF12 EB210 Name Time Room jhgfjhg
9
9.1.1 Projections Using SQL Name the relation to be projected and list the columns to be shown Projection : STUDENT[SID, Name, Major] Standard SQL: SELECT SID, Name, Major FROM STUDENT Result: 100 JONES HISTORY 150 PARKS ACCOUNTING 200 BAKER MATH 250 GLASS 300 350 RUSSELL 400 RYE 450 jhgfjhg
10
9.1.1 Projections Using SQL Ex.: output contains duplicated rows
SELECT Major FROM STUDENT HISTORY ACCOUNTING MATH Ex.: Duplicated rows are removed SELECT DISTINCT Major FROM STUDENT HISTORY ACCOUNTING MATH jhgfjhg
11
9.1.2 Selections Using SQL Ex.: Selecting all attributes
SELECT SID, Name, Major, GradeLevel, Age FROM STUDENT WHERE Major = 'MATH' Ex.: * means that all columns of the table are to be obtained SELECT * 200 BAKER MATH GR 50 350 RUSSELL JR 20 jhgfjhg
12
Ex.: Selection and Projection SELECT Name, Age FROM STUDENT
WHERE Major = 'MATH' BAKER RUSSELL 50 20 Ex.: Several conditions in the WHERE clause SELECT Name, Age FROM STUDENT WHERE Major = 'MATH' AND Age > 21 BAKER 50 jhgfjhg
13
Ex.: conditions in WHERE clause can refer to a set of values
- Key Word: IN or NOT IN SELECT Name FROM STUDENT WHERE Major IN ['MATH', 'ACCOUNTING'] PARKS BAKER RUSSELL RYE Ex.: Display the names of students who have either a math or an accounting major SELECT Name FROM STUDENT WHERE Major NOT IN ['MATH', 'ACCOUNTING'] JONES GLASS jhgfjhg
14
WHERE Age > 19 AND Age < 30
Ex.: conditions in WHERE clause can refer to ranges and to partial values - Key Word: BETWEEN SELECT Name, Major FROM STUDENT WHERE Age BETWEEN 19 AND 30 JONES HISTORY RUSSELL MATH Ex.: Equivalent to SELECT Name, Major FROM STUDENT WHERE Age > 19 AND Age < 30 jhgfjhg
15
Ex.: LIKE can be used to select on partial values
‘_’ represent a single unspecified character ‘%’ represents a series of one or more unspecified characters - Key Word: LIKE SELECT Name, GradeLevel FROM STUDENT WHERE GradeLevel LIKE ‘_R’ JONES GR BAKER RUSSELL JR RYE FR Ex.: fond students whose last names end with S SELECT Name FROM STUDENT WHERE Name LIKE ‘%S’ JONES PARKS GLASS jhgfjhg
16
‘?’ represent a single unspecified character
Ex.: MS Access uses a different set of wildcard symbols than the ANSI standard ‘?’ represent a single unspecified character ‘*’ represents a series of one or more unspecified characters Keyword IS NULL are used to search for null values SELECT Name FROM STUDENT WHERE GradeLevel IS NULL jhgfjhg
17
9.1.3 Sorting The rows of the result relation can be sorted by the values in one or more columns Ex.: SELECT Name, Major, Age FROM STUDENT WHERE Major = 'ACCOUNTING' ORDER BY Name BAKER ACCOUNTING 41 PARKS RYE 19 18 jhgfjhg
18
Ex.: Ascending(ASC) or Descending(DESC) orders
SELECT Name, Major, Age FROM STUDENT WHERE GradeLevel IN ['FR', 'SO', 'SN'] ORDER BY Major ASC, Age DESC BAKER ACCOUNTING 41 PARKS RYE 19 18 GLASS HISTORY JONES 50 24 ORDER BY can be combined with any of the SELECT statement jhgfjhg
19
9.1.4 SQL Built-in Functions
Functions : COUNT, SUM, AVG, MAX, MIN Ex.: count the number of STUDENT rows SELECT COUNT (*) FROM STUDENT 8 Ex.: count the number of DISTINCT Major SELECT COUNT (Major) FROM STUDENT SELECT COUNT(DISTINCT Major) 8 3 jhgfjhg
20
9.1.5 Built-in Functions and Grouping
GROUP BY Ex.: GROUP BY - count the number of students in each Major SELECT Major, COUNT (*) FROM STUDENT GROUP BY Major HISTORY 3 ACCOUNTING MATH 2 jhgfjhg
21
HAVING clause Ex.: HAVING - groups of students having the same major are formed, and then groups having more than two students are selected SELECT Major, COUNT (*) FROM STUDENT GROUP BY Major HAVING COUNT(*) > 2 HISTORY 3 ACCOUNTING jhgfjhg
22
Ex.: HAVING and WHERE clause : WHERE condition is applied before or after the HAVING condition
SELECT Major, AVG (Age) FROM STUDENT WHERE GradeLevel = 'SN' GROUP BY Major HAVING COUNT(*) > 1 HISTORY 37 jhgfjhg
23
9.2 Querying Multiple Tables
2 Strategies 9.2.1 Retrieval Using Subquery 9.2.2 Joining with SQL jhgfjhg
24
9.2.1 Retrieval Using Sub-Query
Ex.: need to know the names of those students enrolled in the class BD445 Step 1⇒ If we know that students with SIDs of 100 and 200 are enrolled in this class, then the following will produce the correct names SELECT Name FROM STUDENT WHERE SID IN [100, 200] Step 2⇒ can find SIDs of students in a class SELECT StudentNumber FROM ENROLLMENT WHERE ClassName = 'BD445' 100 200 jhgfjhg
25
Ex.: Combining the two queries, then can obtain the following
SELECT Name FROM STUDENT WHERE SID IN (SELECT StudentNumber FROM ENROLLMENT WHERE ClassName = 'BD445') -The second SELECT, which is called a subquery, is enclosed in parentheses -SID and StudentNumber must come from the same domain JONES BAKER jhgfjhg
26
Step 2 ⇒need to identify number of students in these classes
Ex.: Subqueries can consists of three or even more tables we want to know the names of the students enrolled in classes in Monday, Wednesday, and Friday at 3 o’clock (MWF3) Step 1 ⇒need the names of those classes that meet at MWF3 SELECT CLASS.Name FROM CLASS WHERE Time = 'MWF3' Step 2 ⇒need to identify number of students in these classes SELECT ENROLLMENT.StudentNumber FROM ENROLLMENT WHERE ENROLLMENT.ClassName IN (SELECT CLASS.Name WHERE Time = 'MWF3’) 100 200 300 jhgfjhg
27
Ex.: Get the names of those students
SELECT STUDENT.Name FROM STUDENT WHERE STUDENT.SID IN (SELECT ENROLLMENT.StudentNumber FROM ENROLLMENT WHERE ENROLLMENT.ClassName IN (SELECT CLASS.Name FROM CLASS WHERE Time = 'MWF3')) JONES BAKER jhgfjhg
28
Problem in Subquery Works well as long as the attributes in the answer come from a single table If the result comes from two or more tables, we have a problem Ex.: suppose we want to know the names of students and the names of their classes (for this, need SID, StudentName, ClassName) In this case, the results come from two tables (STUDENT and ENROLLMENT), and so the subquery strategy will not work jhgfjhg
29
9.2.2 Joining with SQL Ex.: To produce the SID, StudentName, ClassName for every student, must join STUDENT table and ENROLLMENT table SELECT STUDENT.SID, STUDENT.Name, ENROLLMENT.ClassName FROM STUDENT, ENROLLMENT WHERE STUDENT.SID = ENROLLMENT.StudentNumber 100 150 200 300 400 450 JONES PARKS BAKER RYE BD445 BA200 CS250 CS150 BF410 jhgfjhg
30
SELECT STUDENT.SID, ENROLLMENT.ClassName FROM STUDENT, ENROLLMENT
Ex.: WHERE clause can contain qualifiers in addition to those needed for the join SELECT STUDENT.SID, ENROLLMENT.ClassName FROM STUDENT, ENROLLMENT WHERE STUDENT.SID = ENROLLMENT.StudentNumber AND STUDENT.Name = 'RYE' AND ENROLLMENT.PositionNumber = 1 400 BF410 jhgfjhg
31
Ex.: Joining more than 2 tables
SELECT STUDENT.SID, CLASS.Name, CLASS.Time, ENROLLMENT.PositionNumber FROM STUDENT, ENROLLMENT, CLASS WHERE STUDENT.SID = ENROLLMENT.StudentNumber AND ENROLLMENT.ClassName = CLASS.Name AND STUDENT.Name = 'BAKER' 200 300 BD445 MWF3 CS250 MWF12 CS150 2 1 jhgfjhg
32
9.2.3 Comparison of Join and Subquery
A join can be used as an alternative way of expressing many subqueries Ex.: Finding the students enrolled in the class BD445 SELECT STUDENT.Name FROM STUDENT, ENROLLMENT WHERE STUDENT.SID = ENROLLMENT.StudentNumber AND ENROLLMENT.ClassName = 'BD445’ Ex.: ”What are the names of the students in class MWF3 FROM STUDENT, ENROLLMENT, CLASS AND ENROLLMENT.ClassName = CLASS.Name AND CLASS.Time = 'MWF3' jhgfjhg
33
Comparison 1) Although join expressions can substitute for many subquery expressions, they cannot substitute for all of them(e.g. EXISTS and NOT EXISTS cannot be represented by joins) 2) subquery cannot be substituted for all joins as mentioned jhgfjhg
34
Ex.: we want to know the names of classes taken by undergraduates
1) Subquery SELECT DISTINCT ClassName FROM ENROLLMENT WHERE StudentNumber IN (SELECT SID FROM STUDENT WHERE GradeLevel NOT = 'GR') 2) Join SELECT DISTINCT ENROLLMENT.ClassName FROM ENROLLMENT, STUDENT WHERE ENROLLMENT.StudentNumber = STUDENT.SID AND STUDENT.GradeLevel NOT = 'GR' jhgfjhg
35
Ex.: if we want to know both the names of the classes and the grade levels of the undergraduate students, must use a join (cannot use subquery) SELECT DISTINCT ENROLLMENT.ClassName, STUDENT.GradeLevel FROM ENROLLMENT, STUDENT WHERE ENROLLMENT.StudentNumber = STUDENT.SID AND STUDENT.GradeLevel NOT = 'GR' BA200 SO CS150 SN FR BF410 CS250 jhgfjhg
36
9.2.4 Outer Join ANSI standard SQL does not support outer joins. They are, however, supported by many DBMS products. Suppose we want a list of all students and the names of the classes that they are taking. Further, suppose that we want to include all students, even those who are taking no class (the following SQL expression is given in MS Access) SELECT Name, ClassName FROM STUDENT LEFT JOIN ENROLLMETN ON SID = StudentNumber; jhgfjhg
37
9.2.4 Outer Join SELECT Name, ClassName
FROM STUDENT LEFT JOIN ENROLLMETN ON SID = StudentNumber; JONES BD445 PARKS BA200 BAKER CS250 GLASS null CS150 RUSSELL RYE BF410 jhgfjhg
38
9.2.5 EXISTS and NOT EXISTS EXISTS and NOT EXISTS: logical operators whose value is either true or false depending on the presence or absence of rows that fit the qualifying conditions Ex.: EXISTS - we want to know the student numbers of students enrolled in more than one class SELECT DISTINCT StudentNumber FROM ENROLLMENT A WHERE EXISTS (SELECT * FROM ENROLLMENT B WHERE A.StudentNumber = B.StudentNumber AND A.ClassName NOT = B.ClassName) jhgfjhg
39
1) Find two rows in ENROLLMENT having the same student number but different class names. If two such rows exist, then the logical value of EXISTS is true 2) Imagine two separate and identical copies of the ENROLLMENT table (A and B). Compare each row in A and the first row in B. Then compare first row in A and the second row in B. 200 400 jhgfjhg
40
NOT EXISTS Ex.: we want to know the names of students taking all classes (wants the names of students such that there are no classes that the student did not take) SELECT STUDENT.Name FROM STUDENT WHERE NOT EXISTS (SELECT * FROM ENROLLMENT WHERE NOT EXISTS (SELECT * FROM CLASS WHERE CLASS.Name = ENROLLMENT.ClassName AND ENROLLMENT.StudentNumber = STUDENT.SID)) jhgfjhg
41
9.3 Changing Data 9.3.1 Inserting Data 9.3.2 Deleting Data
9.3.3 Modifying Data jhgfjhg
42
9.3.1 Inserting Data Ex.: to insert a single row
INSERT INTO ENROLLMENT VALUES (400, 'BD445', 44) Ex.: If we do not know all of this data (attributes) (if do not know PositionNumber) (StudentNumber, ClassName) VALUES (400, 'BD445') Ex.: can copy rows in mass from one table to another. INSERT INTO JUNIOR VALUES (SELECT SID, Name, Major FROM STUDENT WHERE GradeLevel = 'JR') jhgfjhg
43
9.3.2 Deleting Data Ex.: deleting the row for Student 100
DELETE STUDENT WHERE STUDENT.SID = 100 ▶ If Student 100 is enrolled in classes, this delete will cause an integrity problem, as the ENROLLMENT rows having StudentNumber = 100 will have no corresponding STUDENT row. jhgfjhg
44
9.3.2 Deleting Data Ex.: Groups of rows can be deleted as shown in the next two examples (delete all enrollments for accounting majors as well as all accounting majors) DELETE ENROLLMENT WHERE ENROLLMENT.StudentNumber IN (SELECT STUDENT.SID FROM STUDENT WHERE STUDENT.Major = 'ACCOUNTING') DELETE STUDENT WHERE STUDENT.Major = 'ACCOUNTING' ♠ The order of these two operations is important, for if it were reversed, non of the ENROLLMENT rows would be deleted because the matching STUDENT rows would already have been deleted jhgfjhg
45
9.3.3 Modifying Data Ex.: UPDATE ENROLLMENT SET PositionNumber = 44
WHERE SID = 400 UPDATE ENROLLMENT SET PositionNumber = MAX (PositionNumber) + 1 jhgfjhg
46
Mass update (suppose the name of a course has been changed form BD445 to BD564. In this case, to prevent integrity problems, both the ENROLLMENT and the CLASS tables must be changed) UPDATE ENROLLMENT SET ClassName = 'BD564' WHERE ClassName = 'BD445' UPDATE CLASS jhgfjhg
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.