Documente Academic
Documente Profesional
Documente Cultură
Objectives
In this chapter, students will learn:
The basic commands and functions of SQL
How to use SQL for data administration (to
create tables, indexes, and views)
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
2
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Introduction to SQL
SQL functions fit into two broad categories:
Data definition language
Data manipulation language
CUSTOMER
INVOICE
LINE
PRODUCT
VENDOR
5
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
First task:
RDBMS creates physical files that will hold
database
Differs substantially from one RDBMS to another
6
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Schema
Group of database objects that are related to
each other
7
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Data Types
Data type selection is usually dictated by nature
of data and by intended use
Supported data types:
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
9
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
10
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
CREATE TABLE
CREATE TABLE
<name> (
<col-def-1>,
<col-def-2>,
:
<col-def-n>,
<constraint-1>,
:
<constraint-k>)
You supply
A name for the table
A list of column definitions
A list of constraints (such
as keys)
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Column Definitions
<col-name> <type>
[NULL|NOT NULL]
[DEFAULT <val>]
[constraint-1 [,
constraint-2[,
...]]]
INT
REAL
CHAR(n)
VARCHAR(n)
DATE
12
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Column Definitions
Columns can be
specified as NULL or NOT
NULL
NOT NULL columns
cannot have missing
values
If neither is given then
columns are assumed
NULL
13
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Example
CREATE TABLE Student (
stuID INT NOT NULL,
stuName VARCHAR(50) NOT NULL,
stuAddress VARCHAR(50),
stuYear INT DEFAULT 1)
14
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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
15
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Constraints
CONSTRAINT
<name>
<type>
<details>
Common <type>s
PRIMARY KEY
UNIQUE
FOREIGN KEY
INDEX
16
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Primary Keys
Primary Keys are defined
through constraints
A PRIMARY KEY
constraint also includes a
UNIQUE constraint and
makes the columns
involved NOT NULL
17
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Unique Constraints
As well as a single
primary key, any set of
columns can be specified
as UNIQUE
This has the effect of
making candidate keys in
the table
18
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Example
CREATE TABLE Student (
stuID INT NOT NULL,
stuName VARCHAR(50) NOT NULL,
stuAddress VARCHAR(50),
stuYear INT DEFAULT 1,
CONSTRAINT pkStudent
PRIMARY KEY (stuID))
19
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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
20
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
INSERT
SELECT
COMMIT
UPDATE
ROLLBACK
DELETE
21
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
22
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
INSERT
INSERT INTO
<table>
(col1, col2, )
VALUES
(val1, val2, )
24
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
INSERT
Student
INSERT INTO Student
(ID, Name, Year)
VALUES (2, Mary, 3)
ID Name Year
John
1
2
John
Mary
1
3
Student
Student
1
ID Name Year
ID Name Year
1
2
John
Mary
Student
INSERT INTO Student
VALUES (2, Mary,
3)
ID Name Year
1
2
John
Mary
1
3
25
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Syntax:
COMMIT [WORK];
Simple SELECT
SELECT <columns>
FROM <table>
<columns> can be
A single column
A comma-separated list of
columns
* for all columns
stuID
stuName
stuAddress
stuYear
29
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Sample SELECTs
SELECT * FROM Student
stuID
1
2
3
4
5
6
7
8
stuName
Anderson
Brooks
Chen
DAngelo
Evans
Franklin
Gandhi
Harrison
stuAddress
15 High St
27 Queens Rd
Lenton Hall
Derby Hall
Lenton Hall
13 Elm St
Lenton Hall
Derby Hall
stuYear
1
3
1
1
2
3
1
1
30
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Sample SELECTs
SELECT stuName FROM Student
stuName
Anderson
Brooks
Chen
DAngelo
Evans
Franklin
Gandhi
Harrison
31
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Sample SELECTs
SELECT stuName, stuAddress
FROM Student
stuName
Anderson
Brooks
Chen
DAngelo
Evans
Franklin
Gandhi
Harrison
stuAddress
15 High St
27 Queens Rd
Lenton Hall
Derby Hall
Lenton Hall
13 Elm St
Lenton Hall
Derby Hall
32
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
UPDATE
UPDATE <table>
SET col1 = val1
[,col2 = val2]
[WHERE
<condition>]
34
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
UPDATE
Student
Student
ID
Name Year
1
2
3
4
John
Mark
Anne
Mary
1
3
2
2
UPDATE Student
SET Year = 1,
Name =
Jane
WHERE ID = 4
ID
Name Year
1
2
3
4
John
Mark
Anne
Jane
1
3
2
1
Student
UPDATE Student
SET Year = Year + 1
ID
Name Year
1
2
3
4
John
Mark
Anne
Mary
2
4
3
3
35
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Syntax:
ROLLBACK;
36
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
37
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
DELETE
Removes all rows which
satisfy the condition
If no condition is given
then ALL rows are deleted
- BE CAREFUL
DELETE FROM
<table>
[WHERE
<condition>]
38
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
DELETE
Student
DELETE FROM
Student
WHERE Year = 2
Student
ID
Name Year
1
2
3
4
John
Mark
Anne
Mary
ID
Name Year
1
2
John
Mark
1
3
1
3
2
2
DELETE FROM Student
or
TRUNCATE TABLE Student
Student
ID
Name Year
39
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
SELECT Queries
Fine-tune SELECT command by adding
restrictions to search criteria using:
Conditional restrictions
Arithmetic operators
Logical operators
Special operators
41
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Syntax:
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
42
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
43
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Arithmetic Operators:
The Rule of Precedence
44
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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
46
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
48
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
49
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Adding a Column
Dropping a Column
Use ALTER to add column
Do not include the NOT NULL clause for new
column
50
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
ALTERing Columns
To add or remove
columns use
ALTER TABLE <table>
ADD COLUMN <col>
ALTER TABLE <table>
DROP COLUMN <name>
Examples
ALTER TABLE Student
ADD COLUMN
Degree VARCHAR(50)
ALTER TABLE Student
DROP COLUMN Degree
51
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
52
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
53
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
54
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
55
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
57
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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]];
Ascending order by default
59
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Aggregate Functions
COUNT function tallies number of non-null
values of an attribute
Takes one parameter: usually a column name
Grouping Data
Frequency distributions created by GROUP BY
clause within SELECT statement
Syntax:
SELECT
FROM
[WHERE
[GROUP BY
[HAVING
[ORDER BY
columnlist
tablelist
conditionlist]
columnlist]
conditionlist]
columnlist [ASC | DESC] ] ;
62
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
63
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
66
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Recursive Joins
Outer Joins
Alias is especially useful when a table must be
joined to itself
Recursive query
Use aliases to differentiate the table from itself
67
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
SQL commands can be divided into two overall
categories:
Data definition language commands
Data manipulation language commands
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
69
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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
70
2010 Cengage Learning. All Rights Reserved. This edition is intended for use outside of the U.S. only, with content that may be different from the
U.S. Edition. May not be scanned, copied, duplicated, or posted to a publicly accessible website, in whole or in part.
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