Database Systems: Design, Implementation, and Management: Introduction To Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management: Introduction To Structured Query Language (SQL)
Chapter 7
Introduction to Structured Query
Language (SQL)
Objectives
In this chapter, students will learn:
The basic commands and functions of SQL
How to use SQL for data administration (to
create tables and indexes)
How to use SQL for data manipulation (to add,
modify, delete, and retrieve data)
How to use SQL to query a database for useful
information
Database Systems, 10th Edition
Introduction to SQL
SQL functions fit into two broad categories:
Data definition language
Data manipulation language
CUSTOMER
INVOICE
LINE
PRODUCT
VENDOR
First task:
RDBMS creates physical files that will hold
database
Differs substantially from one RDBMS to another
10
Data Types
Data type selection is usually dictated by nature
of data and by intended use
Supported data types:
11
12
13
14
15
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, 10th Edition
16
SQL Indexes
When primary key is declared, DBMS
automatically creates unique index
Often need additional indexes
Using CREATE INDEX command, SQL indexes
can be created on basis of any selected
attribute
Composite index
Index based on two or more attributes
Often used to prevent data duplication
Database Systems, 10th Edition
17
INSERT
SELECT
COMMIT
UPDATE
ROLLBACK
DELETE
18
19
20
Syntax:
COMMIT [WORK];
21
22
23
Syntax:
ROLLBACK;
24
25
26
SELECT Queries
Fine-tune SELECT command by adding
restrictions to search criteria using:
Conditional restrictions
Arithmetic operators
Logical operators
Special operators
27
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
28
29
30
31
Arithmetic Operators:
The Rule of Precedence
32
33
Special Operators
BETWEEN: checks whether attribute value is
within a range
IS NULL: checks whether attribute value is null
LIKE: checks whether attribute value matches
given string pattern
IN: checks whether attribute value matches any
value within a value list
EXISTS: checks if subquery returns any rows
Database Systems, 10th Edition
34
35
36
37
Adding a Column
Dropping a Column
Use ALTER to add column
Do not include the NOT NULL clause for new
column
38
39
40
41
42
43
Ordering a Listing
ORDER BY clause is useful when listing order
is important
Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[ORDER BY columnlist [ASC | DESC]];
44
45
Aggregate Functions
COUNT function tallies number of non-null values
of an attribute
Takes one parameter: usually a column name
46
Grouping Data
Frequency distributions created by GROUP BY
clause within SELECT statement
Syntax:
SELECT
FROM
[WHERE
[GROUP BY
[HAVING
[ORDER BY
Database Systems, 10th Edition
columnlist
tablelist
conditionlist]
columnlist]
conditionlist]
columnlist [ASC | DESC] ] ;
47
48
49
50
Recursive Joins
Alias is especially useful when a table must be
joined to itself
Recursive query
Use aliases to differentiate the table from itself
51
Summary
SQL commands can be divided into two overall
categories:
Data definition language commands
Data manipulation language commands
52
Summary (contd.)
DML commands allow you to add, modify, and
delete rows from tables
The basic DML commands:
SELECT, INSERT, UPDATE, DELETE,
COMMIT, and ROLLBACK
53
Summary (contd.)
WHERE clause can be used with SELECT,
UPDATE, and DELETE statements
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
Ascending or descending order
Database Systems, 10th Edition
54
Summary (contd.)
Join output of multiple tables with SELECT
statement
Join performed every time you specify two or
more tables in FROM clause
If no join condition is specified, DBMX performs
Cartesian product
55