Modern Database Management
Thirteenth Edition, Global Edition
Chapter 5
Introduction to SQL
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Learning Objectives
5.1 Define terms
5.2 Interpret the history and role of SQL in database
development
5.3 Define a database using the SQL data definition
language
5.4 Write single-table queries using SQL commands
5.5 Establish referential integrity using SQL
5.6 Discuss the SQL:1999 and SQL:2016 standards
SQL Overview
• Structured Query Language – often pronounced “Sequel”
• The standard for Relational Database Management
Systems (RDBMS)
• RDBMS: A database management system that manages
data as a collection of tables in which all relationships are
represented by common values in related tables
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
History of SQL
• 1970 – E. F. Codd develops relational database concept
• 1974-79 – System R with Sequel (later SQL) created at IBM Research Lab
• 1979 – Oracle markets first relational DB with SQL
• 1981 – SQL/DS first available RDBMS system on DOS/VSE
– Others followed: INGRES (1981), IDM (1982), DG/SGL (1984), Sybase
(1986)
• 1986 – ANSI SQL standard released
– Major ANSI standard updates in 1989, 1992, 1999, 2003, 2006, 2008,
2011, 2016
• Today – SQL is supported by most major database vendors
Is SQL a standard? No longer certified by NIST.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Original Purpose of SQL Standard
• Specify syntax/semantics for data definition and manipulation
• Define data structures and basic operations
• Enable portability of database definition and application
modules
• Specify minimal (level 1) and complete (level 2) standards
• Allow for later growth/enhancement to standard (referential
integrity, transaction management, user-defined functions,
extended join operations, national character sets)
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Benefits of a Standardized Relational
Language
• Reduced training costs
• Productivity
• Application portability
• Application longevity
• Reduced dependence on a single vendor
• Cross-system communication
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SQL Environment
• Catalog
– A set of schemas that constitute the description of a database
• Schema
– The structure that contains descriptions of objects created by a user
(base tables, views, constraints)
• Data Definition Language (DDL)
– Commands that define a database, including creating, altering, and
dropping tables and establishing constraints
• Data Manipulation Language (DML)
– Commands that maintain and query a database
• Data Control Language (DCL)
– Commands that control a database, including administering privileges
and committing data
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-1 SQL Environment, as Described
by the SQL:2016 Standard
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SQL Data Types
• Strings
– CHARACTER (n), VARYING CHARACTER (n)
• Binary
– Binary Large Object (BLOB)
• Number
– Numeric (precision, scale), Decimal (p, s), Integer
• Temporal
– Timestamp, Timestamp with local time zone
• Boolean
– True or False values
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-4 DDL, DML, DCL, and the
Database Development Process
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SQL Database Definition
• Data Definition Language (DDL)
• Major CREATE statements:
– CREATE SCHEMA – defines a portion of the
database owned by a particular user
– CREATE TABLE – defines a new table and its
columns
– CREATE VIEW – defines a logical table from one or
more tables or views
• Other CREATE statements: CHARACTER SET,
COLLATION, TRANSLATION, ASSERTION, DOMAIN
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Steps in Table Creation
1. Identify data types for attributes
2. Identify columns that can and cannot be null
3. Identify columns that must be unique (candidate keys)
4. Identify primary key–foreign key mates
5. Determine default values
6. Identify constraints on columns (domain specifications)
7. Create the table and associated indexes
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-5 General Syntax for CREATE TABLE
Statement Used in Data Definition Language
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
The Following Slides Create Tables for This
Enterprise Data Model
(from Chapter 1, Figure 1-3)
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-6 SQL Database Definition
Commands for PVF Company (Oracle 12c)
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Defining Attributes and Their Data Types
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Non-Nullable Specifications
Some primary keys are composite– composed of multiple attributes
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Controlling the Values in Attributes
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Identifying Foreign Keys and Establishing
Relationships
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Data Integrity Controls
• Referential integrity – constraint that ensures that foreign
key values of a table must match primary key values of a
related table in 1: N relationships
• Restricting:
– Deletes of primary records
– Updates of primary records
– Inserts of dependent records
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-7 Ensuring Data Integrity
Through Updates
Relational integrity is enforced via the primary-key to foreign-key
match
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Changing Tables
• ALTER TABLE statement allows you to change column
specifications:
– ,
• Table Actions:
– ,
– ,
• Example (adding a new column with a default value):
– ,
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Removing Tables
• DROP TABLE statement allows you to remove tables
from your schema:
–,
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
INSERT Statement
• Adds one or more rows to a table
• Inserting into a table:
– ,
• Inserting a record that has some null attributes requires
identifying the fields that actually get data:
– ,
• Inserting from another table:
– ,
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Creating Tables with Identity Columns
Inserting into a table does not require explicit customer ID entry or field
list.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
DELETE Statement
• Removes rows from a table
• Delete certain rows
–,
• Delete all rows
–,
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
UPDATE Statement
• Modifies data in existing rows
–
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
MERGE Statement
Makes it easier to update a table. It allows combination of Insert and
Update in one statement.
Useful for updating master tables with new data.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Schema Definition
• Control processing/storage efficiency:
– Choice of indexes
– File organizations for base tables
– File organizations for indexes
– Data clustering
– Statistics maintenance
• Creating indexes
– Speed up random/sequential access to base table data
– Example
▪ ,
▪ This makes an index for the CUSTOMERNAME field of the
CUSTOMER_T table
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SELECT Statement
• Used for queries on single or multiple tables
• Clauses of the SELECT statement:
– SELECT: List the columns (and expressions) to be returned
from the query
– FROM: Indicate the table(s) or view(s) from which data will be
obtained
– WHERE: Indicate the conditions under which a row will be
included in the result
– GROUP BY: Indicate categorization of results
– HAVING: Indicate the conditions under which a category (group)
will be included
– ORDER BY: Sorts the result according to specified criteria
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SELECT Example
• Find products with standard price less than $275
• Comparison operators include
– = Equal to
– > Greater than
– >= Greater than or equal to
– < Less than
– <= Less than or equal to
– <> Not equal to
– != Not equal to
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SELECT Example Using Alias
• Alias is an alternative column or table name
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SELECT Example Using a Function
• Using the COUNT aggregate function to find totals
• Note: With aggregate functions you can’t have single-
valued columns included in the SELECT clause, unless
they are included in the GROUP BY clause.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SELECT Example – Boolean Operators
• AND, OR, and NOT Operators for customizing conditions
in WHERE clause
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-8 Boolean Query a Without Use of
Parentheses
By default, processing order of Boolean operators is NOT, then
AND, then OR
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
SELECT Example–Boolean Operators
• With parentheses…these override the normal
precedence of Boolean operators
With parentheses, you can override normal precedence
rules. In this case parentheses make the OR take place
before the AND.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-9 Boolean Query B with Use of
Parentheses
With parentheses, you can override normal precedence rules. In
this case parentheses make the OR take place before the AND.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Sorting Results with ORDER BY Clause
• Sort the results first by STATE, and within a state by the
CUSTOMER NAME
• Note: The IN operator in this example allows you to
include rows whose CustomerState value is either FL, TX,
CA, or HI. It is more efficient than separate OR conditions.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Categorizing Results Using GROUP BY
Clause
• For use with aggregate functions
– Scalar aggregate: single value returned from SQL
query with aggregate function
– Vector aggregate: multiple values returned from SQL
query with aggregate function (via GROUP BY)
• You can use single-value fields with aggregate functions
if they are included in the GROUP BY clause
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Qualifying Results by Categories Using the
HAVING Clause
• For use with GROUP BY
• Like a WHERE clause, but it operates on groups
(categories), not on individual rows. Here, only those
groups with total numbers greater than 1 will be included
in the final result.
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
A Query with Both WHERE and HAVING
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Figure 5-10 SQL Statement Processing
Order
(based on van der Lans, 2006, p. 100)
Copyright © 2020 Pearson Education Ltd. All Rights Reserved
Copyright
Copyright © 2020 Pearson Education Ltd. All Rights Reserved