Professional Documents
Culture Documents
Chapter 7
Database Systems:
Design, Implementation, and Management,
Sixth Edition, Rob and Coronel
1
7
Introduction to SQL
• SQL functions fit into two broad categories:
• Data definition language
– SQL includes commands to create
• Database objects such as tables, indexes, and
views
• Commands to define access rights to those
database objects
• Data manipulation language
– Includes commands to insert, update, delete,
and retrieve data within the database tables
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 3
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 5
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 6
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 7
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 8
7
• Authentication
– Process through which the DBMS verifies that
only registered users are able to access the
database
– Log on to the RDBMS using a user ID and a
password created by the database
administrator
• Schema
– Group of database objects—such as tables
and indexes—that are related to each other
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 10
7
Data Types
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 11
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 12
7
SQL Indexes
• When a primary key is declared, DBMS
automatically creates a unique index
• Often need additional indexes
• Using the CREATE INDEX command, SQL
indexes can be created on the basis of any
selected attribute
• Composite index
– Index based on two or more attributes
– Often used to prevent data duplication
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 15
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 16
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 17
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 18
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 19
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 21
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 22
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 26
7
UPDATE EMP
SET EMP_PCT = 3.85
WHERE EMP_NUM = '103';
UPDATE EMP
SET EMP_PCT = 5.00
WHERE EMP_NUM = ‘101’;
UPDATE EMP
SET EMP_PCT = 8.00
WHERE EMP_NUM = ‘102’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 27
7
Restoring Table Contents
• ROLLBACK
– Used restore the database to its previous
condition
– Only applicable if COMMIT command has not
been used to permanently store the changes
in the database
• Syntax
– ROLLBACK;
• COMMIT and ROLLBACK only work with
data manipulation commands that are used to
add, modify, or delete table rows
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 28
7
• Syntax
– DELETE FROM tablename
[WHERE conditionlist ];
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 30
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 31
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 32
7
Inserting Table Rows with a Select
Subquery
• INSERT
– Inserts multiple rows from another table
(source)
– Uses SELECT subquery
• Query that is embedded (or nested) inside
another query
• Executed first
• Syntax
– INSERT INTO tablename SELECT columnlist
FROM tablename
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 33
7
Inserting Table Rows with a Select
Subquery - Example
INSERT INTO PART (PART_CODE, PART_DESCRIPT,
PART_PRICE)
SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 34
7
Selecting Rows with Conditional
Restrictions
• Select partial table contents by placing
restrictions on rows to be included in output
– Add conditional restrictions to the SELECT
statement, using WHERE clause
• Syntax
– SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 35
7
Selected PRODUCT Table Attributes for
VENDOR Code 21344
SELECT P_DESCRIPT,
P_INDATE, P_PRICE,
V_CODE FROM
PRODUCT WHERE
V_CODE = 21344;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 36
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 37
7
Comparison Operators
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 38
7
Selected PRODUCT Table Attributes for
VENDOR Codes Other than 21344
SELECT P_DESCRIPT,
P_INDATE, P_PRICE,
V_CODE FROM
PRODUCT WHERE
V_CODE <> 21344;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 39
7
Selected PRODUCT Table Attributes
with a P_PRICE Restriction
SELECT
P_DESCRIPT,
P_ONHAND,
P_MIN,
P_PRICE
FROM PRODUCT
WHERE P_PRICE <=
10;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 40
7
Selected PRODUCT Table Attributes:
The ASCII Code Effect
SELECT P_CODE,
P_DESCRIPT,
P_ONHAND, P_MIN,
P_PRICE FROM
PRODUCT WHERE
P_CODE < ‘1558-
QW1’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 41
7
Selected PRODUCT Table Attributes:
Date Restriction
SELECT P_DESCRIPT,
P_ONHAND, P_MIN,
P_PRICE, P_INDATE
FROM PRODUCT
WHERE P_INDATE >=
’20 – Jan – 2004’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 42
7
SELECT Statement
with a Computed Column
SELECT P_DESCRIPT,
P_ONHAND, P_PRICE,
P_ONHAND * P_PRICE
FROM PRODUCT;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 43
7
SELECT Statement with a Computed
Column and an Alias
SELECT P_DESCRIPT,
P_ONHAND, P_PRICE,
P_ONHAND * P_PRICE
AS TOTVALUE FROM
PRODUCT;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 44
7
Arithmetic Operators:
The Rule of Precedence
• Perform operations within parentheses
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 45
7
Selected PRODUCT Table Attributes:
The Logical OR
SELECT P_DESCRIPT,
P_INDATE, P_PRICE,
V_CODE FROM
PRODUCT WHERE
V_CODE = 21344 OR
V_CODE = 24288;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 46
7
Selected PRODUCT Table Attributes:
The Logical AND
SELECT P_DESCRIPT,
P_INDATE, P_PRICE,
V_CODE FROM
PRODUCT WHERE
PRICE < 50 AND
P_INDATE > ’15 – Jan –
2004’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 47
7
Selected PRODUCT Table Attributes:
The Logical AND and OR
SELECT P_DESCRIPT,
P_INDATE, P_PRICE,
V_CODE FROM
PRODUCT WHERE
(P_PRICE < 50 AND
P_INDATE > ’15 – Jan –
2004’) OR V_CODE =
24288;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 48
7
Special Operators
• BETWEEN
– Used to check whether attribute value is within a
range
• IS NULL
– Used to check whether attribute value is null
• LIKE
– Used to check whether attribute value matches a
given string pattern
• IN
– Used to check whether attribute value matches
any value within a value list
• EXISTS
– Used to check if a subquery returns any rows
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 49
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 51
7
Changing a Column’s Data
Characteristics
• Use ALTER to change data characteristics
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 52
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 53
7
The Effect of Data Entry into the New
P_SALECODE Column
UPDATE PRODUCT
SET P_SALECODE = ‘1’
WHERE P_CODE = ‘1546-QQ2’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 54
7
Update of the P_SALECODE Column in
Multiple Data Rows
UPDATE PRODUCT
SET P_SALECODE = ‘1’
WHERE P_CODE = ‘2232/QWE’
OR P_CODE = ‘2232/QTY’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 55
7
The Effect of Multiple Data Updates in the
PRODUCT Table (MS Access)
UPDATE PRODUCT
SET P_SALECODE = ‘2’
WHERE P_INDATE <
’25-Dec-2003’;
UPDATE PRODUCT
SET P_SALECODE = ‘1’
WHERE P_INDATE >=
’16-Jan-2004’;
AND P_INDATE <=
’10-Feb-2004’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 56
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 57
7
PART Attributes Copied
from the PRODUCT Table
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 58
7
– Count
– Calculate averages
SELECT P_CODE,
P_DESRIPT, P_INDATE,
P_PRICE FROM
PRODUCT ORDER BY
P_PRICE;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 60
7
Partial Listing of
EMPLOYEE Table Contents
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 61
7
SELECT EMP_LNAME,
EMP_LNAME, EMP_FNAME,
EMP_INITIAL,
EMP_AREACODE,
EMP_PHONE FROM
EMPLOYEE ORDER BY
EMP_LNAME,
EMP_FNAME,
EMP_INITIAL;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 62
7
SELECT P_DESCRIPT,
V_CODE, P_INDATE,
P_PROCE FROM PRODUCT
WHERE P_INDATE < ’21-
Jan-2004’ AND
P_PRICE <= 50.00
ORDER BY V_CODE,
P_PROCE DESC;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 63
7
A Listing of Distinct (Different) V_CODE
Values in the PRODUCT Table
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 64
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 65
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 66
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 67
7
The Total Value of All Items
in the PRODUCT Table
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 68
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 69
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 70
7
Incorrect and Correct Use
of the GROUP BY Clause
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 71
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 72
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 74
7
SELECT
PRODUCT.P_DESCRIPT,
PRODUCT.P_PRICE,
VENDOR.V_NAME,
VENDOR.V_CONTACT,
VENDOR.V_AREACODE,
VENDOR.V_PHONE
FROM PRODUCT,
VENDOR WHERE
PRODUCT.V_CODE =
VENDOR.V_CODE;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 75
7
An Ordered and Limited Listing
After a JOIN
SELECT P_DESCRIPT,
P_PRICE, V_NAME,
V_CONTACT,
V_AREACODE,
V_PHONE FROM
PRODUCT, VENDOR
WHERE
PRODUCT.V_CODE =
VENDOR.V_CODE AND
P_INDATE > ’15-Jan-
2004’;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 76
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 77
7
SELECT E.EMP_NUM,
E.EMP_LNAME, E.EMP_MGR,
M.EMP_LNAME, FROM EMP
E, EMP M WHERE
E.EMP_MGR = M.EMP_NUM
ORDER BY E.EMP_MGR;
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 78
7
Converting an ER Model
into a Database Structure
• Requires following specific rules that govern
such a conversion
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 79
7
The Ch06_Artist Database ERD
and Tables
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 80
7
A Data Dictionary
for the Ch06_Artist Database
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 81
7
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 82
7
Summary
• SQL commands can be divided into two
overall categories:
– Data definition language commands
– Data manipulation language commands
• Basic data definition commands allow you to
create tables, indexes, and views
• Many SQL constraints can be used with
columns
• Aggregate functions
– Special functions that perform arithmetic
computations over a set of rows
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 83
7
Summary (continued)
• ORDER BY clause
– Used to sort output of a SELECT statement
– Can sort by one or more columns and use
either an ascending or descending order
• Join output of multiple tables with SELECT
statement
• Natural join uses join condition to match only
rows with equal values in specified columns
• Right outer join and left outer join used to
select rows that have no matching values in
other related table
Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel 84