Download presentation
Presentation is loading. Please wait.
1
Chapter 9 SQL and RDBMS Part C. SQL
Copyright 2005 Radian Publishing Co.
2
Copyright 2005 Radian Publishing Co.
Contents Chapter 9 SQL and RDBMS 9.1 What is SQL 9.1 A. DDL 9.1 B. DML 9.1 C. DCL 9.2 SQL and RDBMS 9.2 A. MS Access 97, 2003 and Visual FoxPro 9.2 B. MySQL 9.3 Creating a Database 9.4 Creating a Table 9.5 Creating a Table with Constraints 9.6 Filling Table with Data 9.7 Other DDL Commands Copyright 2005 Radian Publishing Co.
3
Copyright 2005 Radian Publishing Co.
Chapter 9 SQL and RDBMS SQL stands for Structured Query Language. A relational database management system (RDBMS) fully supports SQL. Copyright 2005 Radian Publishing Co.
4
Copyright 2005 Radian Publishing Co.
9.1 What is SQL Set-oriented means that SQL processes sets of data in groups. Non-procedural means that SQL does not care about how the result is obtained, but it stresses on what result will be obtained. SQL commands can be classified into three types: Data Definition Language (DDL) commands Data Manipulation Language (DML) commands Data Control Language (DCL) commands Copyright 2005 Radian Publishing Co.
5
Copyright 2005 Radian Publishing Co.
9.1 A. DDL Data Definition Language (DDL) commands are used to create, alter and delete tables, views and indexes, including CREATE DATABASE DROP DATABASE CREATE TABLE DROP TABLE ALTER TABLE CREATE INDEX DROP INDEX CREATE VIEW DROP VIEW Copyright 2005 Radian Publishing Co.
6
Copyright 2005 Radian Publishing Co.
9.1 B. DML (1/2) Data Manipulation Language (DML) commands are used to update, insert, modify and query the data in the database, including SELECT INSERT UPDATE DELETE Copyright 2005 Radian Publishing Co.
7
Copyright 2005 Radian Publishing Co.
9.1 B. DML (2/2) The reasons for writing programs to issue SQL commands are meeting specific needs of an organisation providing interface for users who do not know SQL handling errors enforce database security Copyright 2005 Radian Publishing Co.
8
Copyright 2005 Radian Publishing Co.
9.1 C. DCL Data Control Language (DCL) commands help DBA to control and maintain the database, including GRANT, ALTER USER etc. DBA stands for database Administrator (see Chapter 3) Copyright 2005 Radian Publishing Co.
9
Copyright 2005 Radian Publishing Co.
9.2 SQL and RDBMS The SQL standards are promoted by two standards organisations: the American National Standards Institute (ANSI) and International Standards Organisation (ISO). In the textbook, ANSI-92 (launched in 1992) will be used. Proprietary Extensions mean including extension to the standards, like ANSI-92. Copyright 2005 Radian Publishing Co.
10
9.2 A. MS Access 97, 2003 and Visual FoxPro
MS Access® 97, MS Access® 2003 and MS Visual FoxPro® 5.0 are software products of Microsoft. Copyright 2005 Radian Publishing Co.
11
Copyright 2005 Radian Publishing Co.
9.2 B. MySQL MySQL is an open source RDBMS product, meaning that it is possible for anyone to use and modify the software. MySQL always works in the client-server mode, even in a standalone computer. Fig.9.1 Versions of MySQL Copyright 2005 Radian Publishing Co.
12
9.3 Creating a Database (1/2)
Fig.9.3 Handling of database files by different DBMS Copyright 2005 Radian Publishing Co.
13
9.3 Creating a Database (2/2)
In MS Access, related tables are stored in a single file. In Visual FoxPro, database container is optional. The SQL command to create database: CREATE DATABASE dBName; MySQL command to show database: SHOW DATABASES; MySQL command to open a database: USE dBName; Copyright 2005 Radian Publishing Co.
14
Copyright 2005 Radian Publishing Co.
9.4 Creating a Table (1/3) The basic syntax for creating a new table is CREATE TABLE TableName1 (FieldName1 FieldType [(FieldWidth [, Precision])] [NOT NULL] [PRIMARY KEY] [UNIQUE] [, FieldName2 ...]); Copyright 2005 Radian Publishing Co.
15
Copyright 2005 Radian Publishing Co.
9.4 Creating a Table (2/3) Fig.9.4 Table to be created Fig.9.5 Structure of the table to be created Copyright 2005 Radian Publishing Co.
16
Copyright 2005 Radian Publishing Co.
9.4 Creating a Table (3/3) The SQL statement for creating the table BRASSITEM Copyright 2005 Radian Publishing Co.
17
9.5 Creating a Table with Constraints (1/2)
Some DBMS enforces constraints to insure data integrity. The constraints include the field value is unique (keywords UNIQUE or PRIMARY KEY). the field value is non-empty (keywords NOT NULL or PRIMARY KEY). providing default field value (keyword DEFAULT) generating a number for a numeric field when new record is created (keyword AUTO_INCREMENT) make sure that a foreign key will be updated if the primary key is updated (keyword FOREIGN KEY, CHECK, CASCADE, RESTRICT, ON DELETE and ON UPDATE) Copyright 2005 Radian Publishing Co.
18
9.5 Creating a Table with Constraints (2/2)
MySQL command to show the structure of the table: SHOW FIELDS FROM TableName; Copyright 2005 Radian Publishing Co.
19
9.6 Filling Table with Data (1/3)
The basic syntax for inserting a new record is: INSERT INTO TableName [(FieldName1 [, FieldName2 ...])] VALUES (Expression1 [, Expression2 ...]); Copyright 2005 Radian Publishing Co.
20
9.6 Filling Table with Data (2/3)
If all the fields will be filled with values, you may skip entering the field names in the Insert statement. Copyright 2005 Radian Publishing Co.
21
9.6 Filling Table with Data (3/3)
The date format in ANSI-92 is YYYY-MM-DD. Dates should be enclosed in quotation marks. For example ' ' represents the 1st day in April of 1991. This format applies to MySQL, MS Access 97 and MS Access 2003, but not in Visual FoxPro. In Visual FoxPro, however, the default date format is MM-DD-YYYY, and braces are used instead of quotation marks. For example { } Copyright 2005 Radian Publishing Co.
22
Copyright 2005 Radian Publishing Co.
9.7 Other DDL Commands (1/5) The basic syntax for creating an index is: CREATE INDEX IndexName ON TableName (FieldName [ASC|DESC], ...); Copyright 2005 Radian Publishing Co.
23
Copyright 2005 Radian Publishing Co.
9.7 Other DDL Commands (2/5) The syntax for changing the structure of a table is: ALTER TABLE TableName1 ADD | ALTER (MODIFY|CHANGE) | DROP [COLUMN] FieldName1 FieldType [(FieldWidth [, Precision])]; Copyright 2005 Radian Publishing Co.
24
Copyright 2005 Radian Publishing Co.
9.7 Other DDL Commands (3/5) In MS Access and FoxPro, ALTER COLUMN is used to change the size and data type of a field. But, you cannot change a field name. In MySQL, MODIFY COLUMN is used to change the size and data type of a field. CHANGE COLUMN is used to change a field name. Copyright 2005 Radian Publishing Co.
25
Copyright 2005 Radian Publishing Co.
9.7 Other DDL Commands (4/5) The SQL command to delete a table: DROP TABLE TableName; Copyright 2005 Radian Publishing Co.
26
Copyright 2005 Radian Publishing Co.
9.7 Other DDL Commands (5/5) The SQL command to delete database: DROP DATABASE DBName; Copyright 2005 Radian Publishing Co.
27
Chapter 10 Queries and Updating
Part C. SQL Chapter 10 Queries and Updating Copyright 2005 Radian Publishing Co.
28
Copyright 2005 Radian Publishing Co.
Contents Chapter 10 Queries and Updating 10.1 Simple Query 10.1 A. Select * 10.1 B. Selecting Individual Columns 10.1 C. Queries with Distinction 10.1 D. Ordering the Result 10.1 E. Efficiency Issue with Order 10.1 F. Specifying Alias for Column 10.1 G. Simple Expressions in Query 10.2 The WHERE Clause 10.2 A. Using Logical Operators 10.2 B. The IN Operator 10.2 C. The BETWEEN … AND Operator 10.2 D. The LIKE Operator 10.3 Exporting the Query Results 10.3 A. Outputting as a New Table 10.3 B. Outputting as a a Text File 10.4 Updating and Deleting Records 10.4 A. The SQL UPDATE 10.4 B. The SQL DELETE Copyright 2005 Radian Publishing Co.
29
Chapter 10 Queries and Updating
In this chapter, you will learn how to query the data contained in tables using the SQL SELECT command. You will also learn how to update and delete records using SQL UPDATE and DELETE commands. Copyright 2005 Radian Publishing Co.
30
Copyright 2005 Radian Publishing Co.
10.1 Simple Query A query is a simple request to retrieve information from a database using criteria. Copyright 2005 Radian Publishing Co.
31
Copyright 2005 Radian Publishing Co.
10.1 A. Select * (1/3) In SELECT *, the asterisk is a wildcard character that tells DBMS to return all the fields. Copyright 2005 Radian Publishing Co.
32
Copyright 2005 Radian Publishing Co.
10.1 A. Select * (2/3) Copyright 2005 Radian Publishing Co.
33
Copyright 2005 Radian Publishing Co.
10.1 A. Select * (3/3) A cursor is the result of a query. It is a read-only table available for browsing, reporting or input to application programs. It is temporary as it will be removed when the application is closed. Copyright 2005 Radian Publishing Co.
34
10.1 B. Selecting Individual Columns
The SQL command allows you to display any columns you want. SELECT FieldNames FROM TableName; Copyright 2005 Radian Publishing Co.
35
10.1 C. Queries with Distinction
The keyword DISTINCT is used to remove duplicate records in the query result. SELECT DISTINCT FieldNames FROM TableName; Copyright 2005 Radian Publishing Co.
36
10.1 D. Ordering the Result (1/2)
The basic syntax for ordering the result is SELECT [DISTINCT] FieldNames FROM TableName ORDER BY FieldName1, FieldName2 ¡K [ASC|DESC] Copyright 2005 Radian Publishing Co.
37
10.1 D. Ordering the Result (2/2)
Copyright 2005 Radian Publishing Co.
38
10.1 E. Efficiency Issue with Order
The efficiency of ordering can be improved by creating indexes. If a certain ordering is frequently needed, an index should be created for this ordering. Copyright 2005 Radian Publishing Co.
39
10.1 F. Specifying Alias for Column (1/2)
A column alias can provide more descriptive names for columns. For MySQL and Visual FoxPro, alias can be used in references in the ORDER BY and GROUP BY clauses, but this is not allowed in MS Access. Copyright 2005 Radian Publishing Co.
40
10.1 F. Specifying Alias for Column (2/2)
Copyright 2005 Radian Publishing Co.
41
10.1 G. Simple Expressions in Query
A query may consist of expressions instead of field names in the SELECT clause. Copyright 2005 Radian Publishing Co.
42
Copyright 2005 Radian Publishing Co.
10.2 The WHERE Clause The syntax of a SELECT statement with a WHERE clause is SELECT [DISTINCT] SelectItems FROM TableName WHERE Conditions ORDER BY OrderItem1 [, OrderItem2 ] ; Alias is not allowed in the WHERE clause for all DBMS. An exact match is required for both MySQL and MS Access, but not for Visual FoxPro. (Note: All the DBMS mentioned in the textbook are not case-sensitive. ) Copyright 2005 Radian Publishing Co.
43
10.2 A. Using Logical Operators
Logical operators, AND, OR and NOT, are used in the WHERE clause. Copyright 2005 Radian Publishing Co.
44
Copyright 2005 Radian Publishing Co.
10.2 B. The IN Operator The IN operator returns true if the specified data matches any one of the elements in the given set. Copyright 2005 Radian Publishing Co.
45
10.2 C. The BETWEEN … AND Operator
The BETWEEN … AND operator returns true if the specified data falls between a starting value and an ending value inclusively. Copyright 2005 Radian Publishing Co.
46
Copyright 2005 Radian Publishing Co.
10.2 D. The LIKE Operator (1/2) The LIKE operator compares the skeletons of two strings. The wildcard (_) means any single character in the position. The wildcard (%) means any string in forming the skeleton. Copyright 2005 Radian Publishing Co.
47
Copyright 2005 Radian Publishing Co.
10.2 D. The LIKE Operator (2/2) Copyright 2005 Radian Publishing Co.
48
10.3 Exporting the Query Results
Since the result of a query will be lost when the application is closed, sometimes, you might want to store the results. Fig.10.2 Variations between DBMS in exporting query results Copyright 2005 Radian Publishing Co.
49
10.3 A. Outputting as a New Table
In MS Access, the syntax to output the query as a table is: SELECT [DISTINCT] SelectItems INTO OutTable FROM TableName ... In MySQL, the syntax to output the query as a table is: CREATE TABLE OutTable SELECT SelectItems In Visual FoxPro, the syntax to output the query as a table is: INTO TABLE OutTable Copyright 2005 Radian Publishing Co.
50
10.3 B. Outputting as a a Text File
In MySQL, the syntax to output the query result to a text file is: SELECT SelectItems INTO OUTFILE 'FileName' FROM TableName ... In Visual FoxPro, the syntax to output the query result to a text file is: TO FILE 'FileName' Copyright 2005 Radian Publishing Co.
51
Copyright 2005 Radian Publishing Co.
10.4 A. The SQL UPDATE (1/2) The basic syntax for updating a set of records is UPDATE TableName SET FieldName1 = Expression1 [, FieldName2 = Expression2 ] WHERE Conditions; Copyright 2005 Radian Publishing Co.
52
Copyright 2005 Radian Publishing Co.
10.4 A. The SQL UPDATE (2/2) Copyright 2005 Radian Publishing Co.
53
Copyright 2005 Radian Publishing Co.
10.4 B. The SQL DELETE (1/2) The basic syntax for deleting a set of records is DELETE FROM TableName WHERE Conditions; If the WHERE clause is missed, all records will be deleted. Copyright 2005 Radian Publishing Co.
54
Copyright 2005 Radian Publishing Co.
10.4 B. The SQL DELETE (2/2) Delete those records with quantity less than 5 Delete all records Copyright 2005 Radian Publishing Co.
55
Chapter 11 Functions and Groups
Part C. SQL Chapter 11 Functions and Groups Copyright 2005 Radian Publishing Co.
56
Copyright 2005 Radian Publishing Co.
Contents Chapter 11 Functions and Groups 11.1 Using Functions in Queries 11.1 A. String Functions 11.1 B. Numeric Functions 11.1 C. Date Functions 11.1 D. Logical Functions 11.2 Aggregate Functions 11.2 A. The Function COUNT() 11.2 B. The Function SUM() 11.2 C. The Function AVG() 11.2 D. The Function MAX() 11.2 E. The Function MIN() 11.3 Group 11.3 A. Group and Aggregate Functions 11.3 B. Grouping by Multiple Columns 11.3 C. Applying Filtering before Grouping 11.3 D. The HAVING clause 11.3 E. Combing HAVING and WHERE 11.3 F. Grouping by Non-column Names 11.3 G. Using GROUP BY with ORDER BY Copyright 2005 Radian Publishing Co.
57
Chapter 11 Functions and Groups
We shall make use of the functions available in your DBMS to give more varied results from a database. A function accepts one or more input data through the arguments and returns a result (see textbook Appendix D). Copyright 2005 Radian Publishing Co.
58
11.1 Using Functions in Queries
The built-in functions available from DBMS are categorised into four main classes: String Numeric Date Logical (see Appendix D on the Textbook for more information) Copyright 2005 Radian Publishing Co.
59
Copyright 2005 Radian Publishing Co.
11.1 A. String Functions Common string functions include: LENGTH(String) UPPER(String), LOWER(String) LTRIM(String), RTRIM(String), TRIM(String), LEFT(String, n), RIGHT(String, n), MID(String, n, m), INSTR(String, SubStr) ASCII(String) CHAR(String) CONCAT(String1, String2, ...), etc. Copyright 2005 Radian Publishing Co.
60
Copyright 2005 Radian Publishing Co.
11.1 B. Numeric Functions Common numeric functions include: ABS(X) SQRT(X) ROUND(X,D), TRUNCATE(X, 0) MOD(N,M) POW(X,Y) RAND() FORMAT(X, D), etc. Copyright 2005 Radian Publishing Co.
61
Copyright 2005 Radian Publishing Co.
11.1 C. Date Functions Common date functions include: CURDATE() DAY(Date), MONTH(Date), YEAR(Date) DAYOFWEEK(Date) DATE_ADD(Date) DAYNAME(Date), MONTHNAME(Date), etc. MS Access does not allow reserved words, like DAY, MONTH and YEAR, as column aliases. Copyright 2005 Radian Publishing Co.
62
11.1 D. Logical Functions (1/3)
The logical function is: IF(expr1, expr2, expr3). If expr1 is TRUE then IF() returns expr2, else it returns expr3. Copyright 2005 Radian Publishing Co.
63
11.1 D. Logical Functions (2/3)
Copyright 2005 Radian Publishing Co.
64
11.1 D. Logical Functions (3/3)
Note MS Access does not accept alias in the ORDER BY clause. Therefore, the expression IIF(…) has to be repeated. For Visual FoxPro, alias must be used in the ORDER BY clause Copyright 2005 Radian Publishing Co.
65
Copyright 2005 Radian Publishing Co.
11.2 Aggregate Functions Aggregate functions return a value based on multiple values in a column. Examples are: COUNT() SUM() AVG() MAX() MIN() Copyright 2005 Radian Publishing Co.
66
11.2 A. The Function COUNT() (1/2)
COUNT() returns the number of rows retrieved by a SELECT statement. Usually, it is used as COUNT(*). It does not make sense to mix an aggregate function with unrelated fields. Copyright 2005 Radian Publishing Co.
67
11.2 A. The Function COUNT() (2/2)
Copyright 2005 Radian Publishing Co.
68
Copyright 2005 Radian Publishing Co.
11.2 B. The Function SUM() SUM() adds the specified fields in a column and returns the sum. Copyright 2005 Radian Publishing Co.
69
Copyright 2005 Radian Publishing Co.
11.2 C. The Function AVG() AVG() function computes the average of a column. Copyright 2005 Radian Publishing Co.
70
Copyright 2005 Radian Publishing Co.
11.2 D. The Function MAX() MAX() returns the largest value in a column. The MAX() function can also work on dates and strings. Copyright 2005 Radian Publishing Co.
71
Copyright 2005 Radian Publishing Co.
11.2 E. The Function MIN() MIN() works returns the lowest number of a column. Copyright 2005 Radian Publishing Co.
72
Copyright 2005 Radian Publishing Co.
11.3 Group (1/2) Records which have the same value in a column can form a group. The syntax of SELECT is now extended to: SELECT [DISTINCT] SelectItems FROM TableName WHERE Conditions GROUP BY GroupColumn1 [,GroupColumn2 ] ORDER BY OrderItem1 [, OrderItem2 ... ; Copyright 2005 Radian Publishing Co.
73
Copyright 2005 Radian Publishing Co.
11.3 Group (2/2) Copyright 2005 Radian Publishing Co.
74
11.3 A. Group and Aggregate Functions
The power of GROUP BY is evident when it is used with aggregate functions. The aggregate function returns a value for each group. Copyright 2005 Radian Publishing Co.
75
11.3 B. Grouping by Multiple Columns
Similar to ORDER BY, you may put more than one field in the GROUP BY clause. Copyright 2005 Radian Publishing Co.
76
11.3 C. Applying Filtering before Grouping
Filtering of records is done by the WHERE clause. Copyright 2005 Radian Publishing Co.
77
Copyright 2005 Radian Publishing Co.
11.3 D. The HAVING clause (1/2) HAVING clause is designed to work with aggregate functions or group. The HAVING clause must be placed after GROUP BY and before ORDER BY. Copyright 2005 Radian Publishing Co.
78
Copyright 2005 Radian Publishing Co.
11.3 D. The HAVING clause (2/2) Copyright 2005 Radian Publishing Co.
79
11.3 E. Combing HAVING and WHERE (1/2)
Don't use HAVING for items that should be in the WHERE clause. WHERE clause filters records and HAVING clause filters groups. In combing the two, the WHERE clause is placed before the HAVING clause and will be performed first. Copyright 2005 Radian Publishing Co.
80
11.3 E. Combing HAVING and WHERE (2/2)
Copyright 2005 Radian Publishing Co.
81
11.3 F. Grouping by Non-column Names (1/2)
Expressions can be used in the GROUP BY clause. Copyright 2005 Radian Publishing Co.
82
11.3 F. Grouping by Non-column Names (2/2)
Copyright 2005 Radian Publishing Co.
83
11.3 G. Using GROUP BY with ORDER BY (1/2)
The order of execution is: WHERE, GROUP BY, HAVING, ORDER BY. Copyright 2005 Radian Publishing Co.
84
11.3 G. Using GROUP BY with ORDER BY (2/2)
Copyright 2005 Radian Publishing Co.
85
Chapter 12 Joining Tables
Part C. SQL Chapter 12 Joining Tables Copyright 2005 Radian Publishing Co.
86
Copyright 2005 Radian Publishing Co.
Contents Chapter 12 Joining Tables 12.1 Business Applications 12.2 Set Operations 12.2 A. Union 12.2 B. Intersect 12.2 C. Minus 12.3 Other Types of JOIN 12.3 A. Cross-join 12.3 B. Outer-join 12.3 C. Simulating MINUS by Outer-Join 12.3 D. Joining to Oneself 12.3 E. Bus Routes Using Joining to Oneself Copyright 2005 Radian Publishing Co.
87
Chapter 12 Joining Tables
Table joining gathers data from more than one table when retrieving data from a database. Join condition may be placed in JOIN clause. It can also be an inequality. Copyright 2005 Radian Publishing Co.
88
12.1 Business Applications
The basic syntax of inner join is: SELECT … FROM TableName1, TableName2 WHERE JoinCondition [AND FilterCondition] The filter condition must be written after the join condition. An alternative syntax for inner-join is: SELECT ... FROM TableName1 INNER JOIN TableName2 ON JoinCondition WHERE FilterCondition Copyright 2005 Radian Publishing Co.
89
Copyright 2005 Radian Publishing Co.
12.2 Set Operations (1/3) A join combines tables by columns. Fig.12.7 Joining tables by columns Copyright 2005 Radian Publishing Co.
90
Copyright 2005 Radian Publishing Co.
12.2 Set Operations (2/3) Combining tables by rows requires the original tables to have compatible structures. Fig.12.8 Joining tables by rows Copyright 2005 Radian Publishing Co.
91
Copyright 2005 Radian Publishing Co.
12.2 Set Operations (3/3) We shall discuss three mathematical set operations: UNION INTERSECT MINUS Fig.12.9 The three set operations Copyright 2005 Radian Publishing Co.
92
Copyright 2005 Radian Publishing Co.
12.2 A. Union (1/2) UNION simply groups the records of two tables together. Duplicated rows are removed. If duplicated rows are necessary, use UNION ALL. Syntax for UNION: SELECT … FROM TableA UNION SELECT … FROM TableB Copyright 2005 Radian Publishing Co.
93
Copyright 2005 Radian Publishing Co.
12.2 A. Union (2/2) Copyright 2005 Radian Publishing Co.
94
Copyright 2005 Radian Publishing Co.
12.2 B. Intersect (1/2) INTERSECT selects records which are common to both tables. Syntax for INTERSECT: SELECT … FROM TableA INTERSECT SELECT … FROM TableB Copyright 2005 Radian Publishing Co.
95
Copyright 2005 Radian Publishing Co.
12.2 B. Intersect (2/2) Copyright 2005 Radian Publishing Co.
96
Copyright 2005 Radian Publishing Co.
12.2 C. Minus MINUS selects records which are found in one set but not on the other. Syntax for MINUS: SELECT … FROM TableA MINUS SELECT … FROM TableB Copyright 2005 Radian Publishing Co.
97
Copyright 2005 Radian Publishing Co.
12.3 Other Types of JOIN (1/2) Fig All possible table combination Copyright 2005 Radian Publishing Co.
98
Copyright 2005 Radian Publishing Co.
12.3 Other Types of JOIN (2/2) A cross-join is a join without any join condition. Joins with a join condition are divided into inner-joins and outer-joins. An inner-join consists of records matching the join conditions. Inner- joins are further classified into equi-joins and non-equi-joins. An equi-join uses the equal operator (=) in the join condition. A non-equi-join uses relational operators, >, <, >=, <=, <>. A natural join is an equi-join without duplicating columns. Copyright 2005 Radian Publishing Co.
99
Copyright 2005 Radian Publishing Co.
12.3 A. Cross-join (1/3) A cross-join is a join without any join condition. It will map every record in one table with every record in the other table. Fig Cross-joining Table A and Table B Copyright 2005 Radian Publishing Co.
100
Copyright 2005 Radian Publishing Co.
12.3 A. Cross-join (2/3) Copyright 2005 Radian Publishing Co.
101
Copyright 2005 Radian Publishing Co.
12.3 A. Cross-join (3/3) Qualifiers are used to remove ambiguity, where two tables have the same column name. Both join condition and filter condition can exist in the WHERE clause. Copyright 2005 Radian Publishing Co.
102
Copyright 2005 Radian Publishing Co.
12.3 B. Outer-join (1/6) An outer-join selects both matched and non-matched records. It is further classified into left-outer-joins, right-outer-joins and full-outer- joins. Copyright 2005 Radian Publishing Co.
103
Copyright 2005 Radian Publishing Co.
12.3 B. Outer-join (2/6) A left-outer-join selects all the rows from the first table including those match and those do not match the rows in the second table. The syntax for a left-outer-join is SELECT ... FROM TableName1 LEFT OUTER JOIN TableName2 ON JoinCondition Copyright 2005 Radian Publishing Co.
104
Copyright 2005 Radian Publishing Co.
12.3 B. Outer-join (3/6) Copyright 2005 Radian Publishing Co.
105
Copyright 2005 Radian Publishing Co.
12.3 B. Outer-join (4/6) A right-outer-join selects all the rows from the second table including those match and those do not match the rows in the first table. Syntax for right-outer-join: SELECT ... FROM TableName1 RIGHT OUTER JOIN TableName2 ON JoinCondition Copyright 2005 Radian Publishing Co.
106
Copyright 2005 Radian Publishing Co.
12.3 B. Outer-join (5/6) Copyright 2005 Radian Publishing Co.
107
Copyright 2005 Radian Publishing Co.
12.3 B. Outer-join (6/6) A full-outer-join reveals unmatched records on both sides as well as the matched records. Syntax for full-outer-join: SELECT ... FROM TableName1 FULL OUTER JOIN TableName2 ON JoinCondition Copyright 2005 Radian Publishing Co.
108
12.3 C. Simulating MINUS by Outer-Join
Refer to textbook P.286 Copyright 2005 Radian Publishing Co.
109
12.3 D. Joining to Oneself (1/2)
Joining a table to itself means writing the same table name twice in FROM clause of the SQL statement. Copyright 2005 Radian Publishing Co.
110
12.3 D. Joining to Oneself (2/2)
Copyright 2005 Radian Publishing Co.
111
12.3 E. Bus Routes Using Joining to Oneself
Refer to textbook P.289 Copyright 2005 Radian Publishing Co.
112
Chapter 13 Subqueries and Views
Part C. SQL Chapter 13 Subqueries and Views Copyright 2005 Radian Publishing Co.
113
Copyright 2005 Radian Publishing Co.
Contents Chapter 13 Subqueries and Views 13.1 Introduction to Subqueries 13.2 Type 1: Single-valued Subqueries 13.2 A. Subqueries with Aggregate Functions 13.2 B. Involving two different tables 13.3 Type 2: Multi-valued Subqueries 13.3 A. The IN Operator 13.3 B. The = ANY Operator 13.3 C. The EXISTS Operator 13.4 Further Examples 13.5 Views 13.5 A. Creating a Simple view 13.5 B. Finding the Mode Copyright 2005 Radian Publishing Co.
114
Chapter 13 Subqueries and Views
A subquery is a query which is embedded in another query. A view is a subset of the database that is presented to some users. It is a virtual table that does not take up as much physical space as an ordinary table does, yet it allows you to perform query and updating to the database. Copyright 2005 Radian Publishing Co.
115
13.1 Introduction to Subqueries (1/2)
If a query consists of a subquery, the subquery will always be executed first. There are several variations in subqueries: The subquery and the main query may refer to the same table, or different tables. The result of a subquery may be a single value, or a set of values. The subquery may be placed in the WHERE clause, or in the FROM clause. A subquery may consist of another subquery, i.e. multi-sub-level. Copyright 2005 Radian Publishing Co.
116
13.1 Introduction to Subqueries (2/2)
Singled-valued subqueries need relational operators, like =, >, >=, <>, <, <=; Multi-valued subqueries need operators like IN, ANY and EXISTS. Copyright 2005 Radian Publishing Co.
117
13.2 Type 1: Single-valued Subqueries
Single-valued subquery: SELECT ... FROM Table1 WHERE FieldName1 =|>|>=|<|<=|<> (SELECT ... from Table2) The query and the subquery may use the same table or different tables. Copyright 2005 Radian Publishing Co.
118
13.2 A. Subqueries with Aggregate Functions
A subquery with an aggregate function returns a single value. Copyright 2005 Radian Publishing Co.
119
13.2 B. Involving two different tables
The query and the subquery may use different tables. Copyright 2005 Radian Publishing Co.
120
13.3 Type 2: Multi-valued Subqueries
Some subqueries return a set of records. We cannot use simple relational operators, like =, >, <, <> etc. Special operators, like IN and EXISTS, are designed for multi-valued subqueries. Multi-valued subquery: SELECT ... FROM Table1 WHERE FieldName1 [IN | = ANY | EXISTS] (SELECT ... from Table2) Copyright 2005 Radian Publishing Co.
121
Copyright 2005 Radian Publishing Co.
13.3 A. The IN Operator The IN operator compares a piece of data with a set of values and returns true if any one of the values in the set matches with the data. Copyright 2005 Radian Publishing Co.
122
Copyright 2005 Radian Publishing Co.
13.3 B. The = ANY Operator The operator = ANY is the same as the IN operator. Copyright 2005 Radian Publishing Co.
123
Copyright 2005 Radian Publishing Co.
13.3 C. The EXISTS Operator The function EXISTS() is a boolean function which tells whether the result of a subquery is successful or not. Copyright 2005 Radian Publishing Co.
124
Copyright 2005 Radian Publishing Co.
13.4 Further Examples Refer to textbook P. 314 Copyright 2005 Radian Publishing Co.
125
Copyright 2005 Radian Publishing Co.
13.5 Views (1/2) A view is a named query result from one or more tables in a database. A view is often called a virtual table. You can perform queries and updating on a view, yet it does not take up as much physical space as an ordinary table. When a view is changed, the tables that the view is built from will be changed. Copyright 2005 Radian Publishing Co.
126
Copyright 2005 Radian Publishing Co.
13.5 Views (2/2) The major advantages of views are: A view can save the effort of entering the same complicated SQL statement every time. A view can enforce security. It can prevent users from viewing other data that may be private or confidential. Copyright 2005 Radian Publishing Co.
127
13.5 A. Creating a Simple view (1/2)
The basic syntax creating a view is CREATE VIEW ViewName AS SELECT ... Copyright 2005 Radian Publishing Co.
128
13.5 A. Creating a Simple view (2/2)
To make a query on the view SALESVIEW Copyright 2005 Radian Publishing Co.
129
Copyright 2005 Radian Publishing Co.
13.5 B. Finding the Mode (1/3) We may use View to help finding the mode, which tells the most popular quantity. For example: Copyright 2005 Radian Publishing Co.
130
Copyright 2005 Radian Publishing Co.
13.5 B. Finding the Mode (2/3) It is clear that “Central”, “Tai Kok Tsui” and “Tai Po” are popular district. Copyright 2005 Radian Publishing Co.
131
Copyright 2005 Radian Publishing Co.
13.5 B. Finding the Mode (3/3) The results of the query show the most popular districts. Copyright 2005 Radian Publishing Co.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.