ch03 - DS - The Relational Model 2 - SQL
ch03 - DS - The Relational Model 2 - SQL
Chapter 3
The Relational Model 2: SQL
http://sophorn26sun.blogspot.com/
Objectives
• Introduce Structured Query Language (SQL)
• Use simple and compound conditions in SQL
• Use computed fields in SQL
• Use built-in SQL functions
• Use subqueries in SQL
• Group records in SQL
fb.me/SophornDS 2
Objectives
fb.me/SophornDS 3
Introduction to SQL
• Provides with the capacities of querying a
relational database
• Enter commands to obtain the desired result
• Use commands to create and update tables and
to retrieve data from table
• Developed under the name SEQUEL at the IBM
San Jose research in mid-1970
• It was renamed SQL in 1980
• It is a standard language for relational database
manipulation
fb.me/SophornDS 4
Getting Started with SQL
fb.me/SophornDS 5
fb.me/SophornDS 6
Getting Started with Microsoft Office
Access 2010/2013 or 2016 (cont.)
• To execute the SQL commands shown in the
figures in Access 2013/2016
– Open the Premiere Products database
– Click on tab: Create, then click on Query Design
– Click the Close button in the Show Table dialog box
– Click the View button list arrow on the Query Design
tab, and then click SQL View
– The query opens in SQL view, ready for you to type
your SQL commands
fb.me/SophornDS 7
fb.me/SophornDS 8
Getting Started with MySQL
• MySQL is an open-source relational database
management system (RDBMS)
• Its name is a combination of "My", the name of co-
founder Michael Widenius's daughter and "SQL", the
abbreviation for Structured Query Language
• Owned and sponsored by the Swedish company
MySQL AB, which was bought by Sun Microsystems
(now Oracle Corporation)
fb.me/SophornDS 9
fb.me/SophornDS 10
Getting Started with MySQL
fb.me/SophornDS 11
fb.me/SophornDS 12
fb.me/SophornDS 13
Table Creation
fb.me/SophornDS 14
Typical Data Types
• INTEGER
– Numbers without a decimal point
• SMALLINT
– Uses less space than INTEGER
• DECIMAL(p, q)
– P number of digits; q number of decimal places
• CHAR(n)
– Character string n places long
• DATE
– Dates in DD-MON-YYYY or MM/DD/YYYY
fb.me/SophornDS 15
fb.me/SophornDS 16
Simple Retrieval
fb.me/SophornDS 17
Simple Retrieval
Figures 3.1 - 3.2
fb.me/SophornDS 18
SQL Query to List Part Table
Figures 3.3 - 3.4
fb.me/SophornDS 19
fb.me/SophornDS 20
SQL Comparison Operators
Figure 3.7
fb.me/SophornDS 21
fb.me/SophornDS 22
SQL Query to Find
All Customers in ‘Grove’
Figures 3.10 - 3.11
fb.me/SophornDS 23
fb.me/SophornDS 24
Compound Conditions
• Compound condition
– Connecting two or more simple conditions using one
or both of the following operators: AND and OR
– Preceding a single condition with the NOT operator
• Connecting simple conditions using AND operator
– All of the simple conditions must be true for the
compound condition to be true
• Connecting simple conditions using OR operator
– Any of the simple conditions must be true for the
compound condition to be true
fb.me/SophornDS 25
fb.me/SophornDS 26
SQL Query using ‘OR’
fb.me/SophornDS 27
fb.me/SophornDS 28
SQL Query using ‘NOT’
fb.me/SophornDS 29
fb.me/SophornDS 30
Computed Fields
fb.me/SophornDS 31
fb.me/SophornDS 32
SQL Query with Computed
Field and Condition
fb.me/SophornDS 33
fb.me/SophornDS 35
fb.me/SophornDS 36
Sorting
fb.me/SophornDS 37
fb.me/SophornDS 38
SQL Query to Sort on Multiple Fields
fb.me/SophornDS 39
Built-in Functions
fb.me/SophornDS 40
SQL Query to Count Records
fb.me/SophornDS 41
fb.me/SophornDS 42
SQL Query to Perform Calculations
and Rename Fields
fb.me/SophornDS 43
Subqueries
fb.me/SophornDS 44
SQL Query with Subquery
fb.me/SophornDS 45
Grouping
fb.me/SophornDS 46
SQL Query to Group Records
fb.me/SophornDS 47
fb.me/SophornDS 48
SQL Query with ‘WHERE’
and ‘HAVING’ Clauses
SELECT RepNum, COUNT(*) AS NumCustomers, AVG(Balance) AS
AverageBalance
FROM Customer
WHERE CreditLimit<10000
GROUP BY RepNum
ORDER BY RepNum
;
fb.me/SophornDS 49
Joining Tables
• Queries can locate data from more than one table
• Enter appropriate conditions in the WHERE clause
• To join tables, construct the SQL command as:
1. SELECT clause: list all fields you want to display
2. FROM clause: list all tables involved in the query
3. WHERE clause: give the condition that will restrict
the data to be retrieved to only those rows from the
two tables that match
fb.me/SophornDS 50
SQL Query to Join Tables
Figures 3.48 - 3.49
SELECT CustomerNum, CustomerName, Rep.RepNum, LastName,
FirstName
FROM Customer, Rep
WHERE Customer.RepNum=Rep.RepNum
;
fb.me/SophornDS 51
fb.me/SophornDS 52
Query to Join Multiple Tables
Figures 3.52 - 3.53
SELECT Orders.OrderNum, OrderDate, Customer.CustomerNum,
CustomerName, Part.PartNum, Description, NumOrdered, QuotedPrice
FROM Orders, Customer, OrderLine, Part
WHERE Customer.CustomerNum=Orders.CustomerNum
AND Orders.OrderNum=OrderLine.OrderNum
AND OrderLine.PartNum=Part.PartNum
;
fb.me/SophornDS 53
INNER JOIN
SELECT …
FROM T1 [INNER] JOIN T2
ON T1.PK = T2.FK
fb.me/SophornDS 54
OUTER JOIN
SELECT …
FROM T1 LEFT OUTER JOIN T2
ON T1.PK = T2.FK
SELECT …
FROM T1 RIGHT OUTER JOIN T2
ON T1.PK = T2.FK
fb.me/SophornDS 55
Union
fb.me/SophornDS 56
SQL Query to Perform Union
fb.me/SophornDS 57
Intersection
SELECT CustomerNum, CustomerName
FROM Customer
WHERE RepNum=‘35’
AND CustomerNum IN(SELECT Customer.CustomerNum
FROM Customer, Orders
WHERE Customer.CustomerNum=Orders.CustomerNum)
;
fb.me/SophornDS 58
Difference (Subtract)
SELECT CustomerNum, CustomerName
FROM Customer
WHERE RepNum=‘35’
AND CustomerNum NOT IN(SELECT Customer.CustomerNum
FROM Customer, Orders
WHERE Customer.CustomerNum=Orders.CustomerNum)
;
fb.me/SophornDS 59
Example
fb.me/SophornDS 60
Updating Tables
fb.me/SophornDS 61
fb.me/SophornDS 62
SQL Query to Delete Rows
Figure 3.58
fb.me/SophornDS 63
• INTO clause
– Saves the results of a query as a table
– Specified before FROM and WHERE clauses
• MySQL
1.Create the new table using a CREATE TABLE
command
2.Use an INSERT command to insert the appropriate
data into the new table
OR: Use a SELECT command to insert the appropriate
data in CREATE TABLE command
fb.me/SophornDS 64
SQL Query to Create New Table from
Existing Table In Access
fb.me/SophornDS 65
fb.me/SophornDS 66
SQL Query to Create New Table from
Existing Table in MySQL
fb.me/SophornDS 67
fb.me/SophornDS 68
Alter table to add more column to
a table
fb.me/SophornDS 69
fb.me/SophornDS 70
Alter table to modify a column in
a table
fb.me/SophornDS 71
fb.me/SophornDS 72
CREATE INDEX command to
create a primary key to a table
fb.me/SophornDS 73
fb.me/SophornDS 74
DROP a table from a database
fb.me/SophornDS 75
Summary
• Structured Query Language (SQL) is a language
that is used to manipulate relational databases
• Basic form of an SQL query: SELECT-FROM-
WHERE
• Use CREATE TABLE command to describe table
layout to the DBMS, which creates the table
• In SQL retrieval commands, fields are listed after
SELECT, tables are listed after FROM, and
conditions are listed after WHERE
• In conditions, character values must be enclosed in
single quotation marks
fb.me/SophornDS 76
Summary (continued)
• Compound conditions are formed by combining
simple conditions using either or both of the
following operators: AND and OR
• Sorting is accomplished using ORDER BY clause
• When the data is sorted in more than one field, can
have a major and minor sort keys
• Grouping: use the GROUP BY clause
• HAVING clause: restricts the rows to be displayed
fb.me/SophornDS 77
Summary (continued)
fb.me/SophornDS 78
Summary (continued)
fb.me/SophornDS 79