Download presentation
Presentation is loading. Please wait.
Published byAntony Lee Modified over 9 years ago
2
Structured Query Language ( 結構化查詢語言 )
3
Database Programming CREATE TABLE … ALTER TABLE… ADD/DROP/MODIFY (…) DESC … DROP TABLE … RENAME … TO … INSERT INTO … VALUES (…) SELECT * FROM … Lesson Review:
4
Updating Data in a Table UPDATE TableName SET Column1 = NewValue1, Column2 = NewValue2 WHERE Condition(s) The WHERE clause is optional.
5
Delete Records from a Table DELETE FROM TableName WHERE Condition(s) The WHERE clause is optional.
6
What is S Q L?L? When a user wants to get some information from a database file, he can issue a _______. A query is a user–request to retrieve data or information with a certain ___________. Structured Query Language(SQL) ( 結構化查詢語言 ) Most ___________________________ (DBMS) support SQL. e.g. Oracle query condition Database Management System
8
Basic structure of an SQL query SELECT * / Column1, Column2, … FROM TableNames
10
Retrieve data with specified selection criteria SELECT Column1, Column2, … FROM TableNames WHERE Conditions OperatorDescription =Equal to <> or != or ^= Not Equal to >Greater/ Larger than <Less/ Smaller than >=Greater or equal to <=Less or equal to BETWEENWithin the range LIKEMatch the pattern
11
Comparison operators Examples WHERE event_date = ' 01-JAN-04' WHERE rental_fee >=2000 WHERE cd_title = ' White Rose' You may also see the <> (not equal to) symbol written as != or ^ = In the example shown from the DJ on Demand database, which rows will be selected? Will salaries of 3000 be included in the results set? SELECT last_name, salary FROM employees WHERE salary <= 3000
12
table Aliases – Column Headings
13
Concatenation – Link columns
14
Retrieve value(s) without Duplication SELECT DISTINCT Column1, Column2, … FROM TableNames WHERE Conditions
15
SELECT title, year FROM d_cds WHERE year BETWEEN '1999' AND '2001‘ Comparison Operators BETWEEN…AND
16
SELECT title, type_code FROM d_songs WHERE type_code IN ( 77, 12 ) IN
17
LIKE The % symbol is used to represent any sequence of zero or more characters. The underscore (_ ) symbol is used to represent a single character. In the example shown below, all employees with last names beginning with any letter followed by an "o" and then followed by any other number of letters will be returned. SELECT last_name FROM employees WHERE last_name LIKE '_o%' Which of the following last names could have been returned from the above query? 1. Sommersmith 2. Oog 3. Fong 4. Mo If you said 1, 2, 3, and 4, you're correct!
18
IS NULL, IS NOT NULL The IS NULL condition tests for unavailable, unassigned, or unknown data. IS NOT NULL tests for data that is present in the database. In this example, the WHERE clause is written to retrieve all the last names and manager IDs of those employees who do not have a manager. SELECT last_name, manager_id FROM employees WHERE manager_id IS NULL Read the following and explain what you expect will be returned: SELECT first_name, last_name, auth_expense_amt FROM d_partners WHERE auth_expense_amt IS NOT NULL
19
LOGICAL CONDITIONS Logical conditions combine the result of two component conditions to produce a single result based on them. For example, to attend a rock concert, you need to buy a ticket AND have transportation to get there. If both conditions are met, you go to the concert. AND In the query below, the results returned will be rows that satisfy BOTH conditions specified in the WHERE clause. SELECT id, title, duration, type_code FROM d_songs WHERE id > 40 AND type_code = 77
20
OR If the WHERE clause uses the OR condition, the results returned from a query will be rows that satisfy either one of the OR conditions. In other words, all rows returned have an ID greater than 40 OR they have a type_code equal to 77. SELECT id, title, duration, type_code FROM d_songs WHERE id > 40 OR type_code = 77
21
SELECT title, type_code FROM d_songs WHERE type_code NOT IN 77 NOT
22
SELECT title, year FROM d_cds ORDER BY year ORDER BY
25
e.g. ROUND(45.926, 2) -> 45.93 TRUNC (45.926, 2) -> 45.92 MOD( 1600 / 300) -> 100
26
Tutorial Exercise Example: Database (stud.dbf)
27
General Structure I List the names and ages (1 d.p.) of 1B girls. List the names and ages (1 d.p.) of 1B girls. 1B Girls ?
28
Condition for "1B Girls": 1)class = "1B" 2)sex = "F" 3)Both ( AND operator) General Structure I List the names and ages (1 d.p.) of 1B girls.
29
General Structure I List the names and ages (1 d.p.) of 1B girls. List the names and ages (1 d.p.) of 1B girls. What is "age"?
30
Functions: # days :SYSDATE – dob # years :(SYSDATE – dob) / 365 1 d.p.:ROUND(__, 1) General Structure I List the names and ages (1 d.p.) of 1B girls. List the names and ages (1 d.p.) of 1B girls.
31
List the names and ages (1 d.p.) of 1B girls. SELECT class, sex, name, ROUND((SYSDATE-dob)/365,1) AS "age" FROM stud WHERE class='1B' AND sex='F' General Structure I
32
Tutorial Exercise Time
33
SELECT last_name, specialty, auth_expense_amt FROM d_partners WHERE specialty ='All Types' OR specialty IS NULL AND auth_expense_amt = 300000 SELECT last_name, specialty, auth_expense_amt FROM d_partners WHERE (specialty ='All Types' OR specialty IS NULL) AND auth_expense_amt = 300000
36
ComparisonII expr IN ( value1, value2, value3) expr BETWEEN value1 AND value2 expr LIKE "%_"
37
ComparisonII eg. 6 List the 1A students whose Math test score is between 80 and 90 (incl.) SELECT name, mtest FROM student ; WHERE class="1A" AND ; mtest BETWEEN 80 AND 90 Result
38
ComparisonII eg. 7 List the students whose names start with "T". SELECT name, class FROM student ; WHERE name LIKE "T%" Result
39
ComparisonII eg. 8 List the Red house members whose names contain "a" as the 2nd letter. eg. 8 List the Red house members whose names contain "a" as the 2nd letter. SELECT name, class, hcode FROM student ; WHERE name LIKE "_a%" AND hcode="R" Result
40
GroupingIII SELECT...... FROM...... WHERE condition ; GROUP BY groupexpr [HAVING requirement] Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) –groupexpr specifies the related rows to be grouped as one entry. Usually it is a column. –WHERE condition specifies the condition of individual rows before the rows are group. HAVING requirement specifies the condition involving the whole group.
41
GroupingIII eg. 11 List the number of students of each class. eg. 11 List the number of students of each class.
42
COUNT( ) Group By Class 1A1A1A1A COUNT( ) 1B1B1B1B 1C1C1C1C 1A1A1A1A 1B1B1B1B 1C1C1C1C Student class 1A1A1A1A 1A1A1A1A 1A1A1A1A 1B1B1B1B 1B1B1B1B 1B1B1B1B 1B1B1B1B 1B1B1B1B 1B1B1B1B 1C1C1C1C 1C1C1C1C 1C1C1C1C
43
GroupingIII SELECT class, COUNT(*) FROM student ; GROUP BY class eg. 11 List the number of students of each class. eg. 11 List the number of students of each class. Result
44
GroupingIII eg. 12 List the average Math test score of each class. eg. 12 List the average Math test score of each class.
45
Group By Class AVG( ) 1A1A1A1A 1B1B1B1B 1C1C1C1C Student class 1A1A1A1A 1A1A1A1A 1A1A1A1A 1B1B1B1B 1B1B1B1B 1B1B1B1B 1B1B1B1B 1B1B1B1B 1B1B1B1B 1C1C1C1C 1C1C1C1C 1C1C1C1C
46
GroupingIII eg. 12 List the average Math test score of each class. eg. 12 List the average Math test score of each class. SELECT class, AVG(mtest) FROM student ; GROUP BY class Result
47
GroupingIII eg. 13 List the number of girls of each district. eg. 13 List the number of girls of each district. SELECT dcode, COUNT(*) FROM student ; WHERE sex="F" GROUP BY dcode Result
48
GroupingIII eg. 14 List the max. and min. test score of Form 1 students of each district. eg. 14 List the max. and min. test score of Form 1 students of each district. SELECT MAX(mtest), MIN(mtest), dcode ; FROM student ; WHERE class LIKE "1_" GROUP BY dcode Result
49
GroupingIII eg. 15 List the average Math test score of the boys in each class. The list should not contain class with less than 3 boys. eg. 15 List the average Math test score of the boys in each class. The list should not contain class with less than 3 boys. SELECT AVG(mtest), class FROM student ; WHERE sex="M" GROUP BY class ; HAVING COUNT(*) >= 3 Result
50
Display Order IV SELECT...... FROM...... WHERE...... GROUP BY..... ; ORDER BY colname ASC / DESC
51
Display Order IV SELECT name, id FROM student ; WHERE sex="M" AND class="1A" ORDER BY name eg. 16 List the boys of class 1A, order by their names. eg. 16 List the boys of class 1A, order by their names. ORDER BY dcode Result
52
Display Order IV SELECT name, id, class, dcode FROM student ; WHERE class="2A" ORDER BY dcode eg. 17 List the 2A students by their residential district. eg. 17 List the 2A students by their residential district. Result
53
Display Order IV SELECT COUNT(*) AS cnt, dcode FROM student ; GROUP BY dcode ORDER BY cnt DESC eg. 18 List the number of students of each district eg. 18 List the number of students of each district (in desc. order). Result
54
Display Order IV SELECT name, class, hcode FROM student ; WHERE sex="M" ORDER BY hcode, class eg. 19 List the boys of each house order by the classes. (2-level ordering) eg. 19 List the boys of each house order by the classes. (2-level ordering)
55
Display Order IV Result Order by class Blue House Green House : : Order by hcode
56
Union, Intersection and Difference of Tables Union, Intersection and Difference of Tables3 AB The union of A and B (A B) A table containing all the rows from A and B.
57
Union, Intersection and Difference of Tables Union, Intersection and Difference of Tables3 The intersection of A and B (A B) A table containing only rows that appear in both A and B. AB
58
Union, Intersection and Difference of Tables Union, Intersection and Difference of Tables3 The difference of A and B (A–B) A table containing rows that appear in A but not in B. AB
59
3 Consider the members of the Bridge Club and the Chess Club. The two database files have the same structure: The Situation: Bridge Club & Chess Club field typewidth contents id numeric 4 student id number name character 10 name sex character 1 sex: M / F class character 2 class
60
Union, Intersection and Difference of Tables 3
61
Union, Intersection and Difference of Tables Union, Intersection and Difference of Tables3 SELECT * FROM bridge ; UNION ; SELECT * FROM chess ; ORDER BY class, name INTO TABLE party eg. 22 The two clubs want to hold a joint party. Make a list of all students. (Union) eg. 22 The two clubs want to hold a joint party. Make a list of all students. (Union) SELECT...... FROM...... WHERE...... ; UNION ; SELECT...... FROM...... WHERE...... Result
62
Union, Intersection and Difference of Tables Union, Intersection and Difference of Tables3 SELECT * FROM bridge ; WHERE id IN ( SELECT id FROM chess ) ; TO PRINTER eg. 23 Print a list of students who are members of both clubs. (Intersection) eg. 23 Print a list of students who are members of both clubs. (Intersection) SELECT...... FROM table1 ; WHERE col IN ( SELECT col FROM table2 ) Result
63
Union, Intersection and Difference of Tables Union, Intersection and Difference of Tables3 SELECT * FROM bridge ; WHERE id NOT IN ( SELECT id FROM chess ) ; INTO TABLE diff eg. 24 Make a list of students who are members of the Bridge Club but not Chess Club. (Difference) eg. 24 Make a list of students who are members of the Bridge Club but not Chess Club. (Difference) SELECT...... FROM table1 ; WHERE col NOT IN ( SELECT col FROM table2 ) Result
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.