SQL Basics
SQL Basics
Chapter 7
Introduction to Structured Query
Language (SQL)
Database Systems:
Design, Implementation, and Management,
Seventh Edition, Rob and Coronel
1
Introduction to SQL
SQL functions fit into two broad categories:
Data definition language
SQL includes commands to:
Create database objects, such as tables etc.
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
10
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
11
Data Types
Data type selection is usually dictated by
nature of data and by intended use
Pay close attention to expected use of
attributes for sorting and data retrieval
purposes
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
12
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
13
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
14
SQL Constraints
NOT NULL constraint
Ensures that column does not accept nulls
UNIQUE constraint
Ensures that all values in column are unique
DEFAULT constraint
Assigns value to attribute when a new row is added to
table
CHECK constraint
Validates data when attribute value is entered
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
15
16
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
17
18
Syntax:
COMMIT [WORK];
19
20
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
21
22
Syntax:
ROLLBACK;
23
24
Syntax:
INSERT INTO tablename SELECT columnlist
FROM tablename;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
25
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
26
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
27
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
28
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
29
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
30
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
31
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
32
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
33
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
34
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
35
Arithmetic Operators:
The Rule of Precedence
Perform operations within parentheses
Perform power operations
Perform multiplications and divisions
Perform additions and subtractions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
36
Arithmetic Operators:
The Rule of Precedence (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
37
Logical Operators:
AND, OR, and NOT
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
38
Logical Operators:
AND, OR, and NOT (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
39
Logical Operators:
AND, OR, and NOT (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
40
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 given string pattern
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
41
EXISTS
Used to check if subquery returns any rows
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
42
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
43
Adding a Column
Use ALTER to add column
Do not include the NOT NULL clause for new
column
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
44
Dropping a Column
Use ALTER to drop column
Some RDBMSs impose restrictions on the
deletion of an attribute
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
45
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
46
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
47
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
48
49
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
50
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
51
Ordering a Listing
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
52
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
53
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
54
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
55
Aggregate Functions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
56
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
57
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
58
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
59
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
60
Grouping Data
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
61
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
62
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
63
64
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
65
66
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
67
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
68
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
69
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
70
Recursive Joins
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
71
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
72
Outer Joins
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
73
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
74
Summary
SQL commands can be divided into two
overall categories:
Data definition language commands
Data manipulation language commands
75
Summary (continued)
DML commands allow you to add, modify, and delete
rows from tables
The basic DML commands are SELECT, INSERT,
UPDATE, DELETE, COMMIT, and ROLLBACK
INSERT command is used to add new rows to tables
SELECT statement is main data retrieval command
in SQL
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
76
Summary (continued)
Many SQL constraints can be used with
columns
The column list represents one or more
column names separated by commas
WHERE clause can be used with SELECT,
UPDATE, and DELETE statements to restrict
rows affected by the DDL command
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
77
Summary (continued)
Aggregate functions
Special functions that perform arithmetic
computations over a set of rows
ORDER BY clause
Used to sort output of SELECT statement
Can sort by one or more columns and use
either an ascending or descending order
78
Summary (continued)
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, 7th Edition, Rob & Coronel
79