64505_Introduction-to-SQL
64505_Introduction-to-SQL
1
Produced By © Information System Department
Defining a Database
2
Produced By © Information System Department
Relational Database Concept
3
Produced By © Information System Department
Definition of a Relational
Database
4
Produced By © Information System Department
Data Models
5
Produced By © Information System Department
Entity Relationship Model
6
Produced By © Information System Department
Entity Relationship Model
• Scenario
– “. . . Assign one or more employees to a
department . . .”
– “. . . Some departments do not yet have
assigned employees . . .”
7
Produced By © Information System Department
Relating Multiple Tables
8
Produced By © Information System Department
Relational Database Properties
A relational database:
• Can be accessed and modified by executing
structured query language (SQL) statements
• Contains a collection of tables with no physical
pointers
• Uses a set of operators
9
Produced By © Information System Department
Plugging into the Grid
10
Produced By © Information System Department
Oracle Enterprise Grid
Computing
11
Produced By © Information System Department
Oracle 10g Products and Forms
Development
12
Produced By © Information System Department
SQL Statements
Data retrieval
SELECT
13
Produced By © Information System Department
SQL Statements
Transaction control
COMMIT
ROLLBACK
SAVEPOINT
14
Produced By © Information System Department
SQL Statements
15
Produced By © Information System Department
Writing SQL Statements
16
Produced By © Information System Department
Basic SELECT Statement
17
Produced By © Information System Department
Selecting All Columns
SELECT *
FROM departments;
18
Produced By © Information System Department
Selecting Specific Columns
19
Produced By © Information System Department
Arithmetic Expressions
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
20
Produced By © Information System Department
Operator Precedence
* / + -
21
Produced By © Information System Department
Using Arithmetic Operators
22
Produced By © Information System Department
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name: there can
also be the optional AS keyword between the
column name and alias
• Requires double quotation marks if it contains
spaces or special characters or is case sensitive
23
Produced By © Information System Department
Concatenation Operator
A concatenation operator:
• Concatenates columns or character strings to
other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
24
Produced By © Information System Department
Literal Character Strings
25
Produced By © Information System Department
Character Strings and Dates
26
Produced By © Information System Department
Duplicate Rows
27
Produced By © Information System Department
Limiting the Rows Selected
28
Produced By © Information System Department
Comparison Conditions
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
29
Produced By © Information System Department
Using Comparison Conditions
30
Produced By © Information System Department
Other Comparison Conditions
Operator Meaning
BETWEEN...AND...
IN(set)
LIKE
IS NULL
31
Produced By © Information System Department
Single-Row Functions
32
Produced By © Information System Department
Character Functions
Function Result
33
Produced By © Information System Department
Character Manipulation Functions
Function Result
34
Produced By © Information System Department
Number Functions
35
Produced By © Information System Department
Working with Dates
36
Produced By © Information System Department
Arithmetic with Dates
37
Produced By © Information System Department
Arithmetic with Dates
Operation Result
date + number Date
(Adds a number of days to a date)
38
Produced By © Information System Department
Date Functions
Function Description
39
Produced By © Information System Department
Conversion Functions
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
41
Produced By © Information System Department
Using the TO_CHAR Function with
Dates
TO_CHAR(date, 'format_model')
42
Produced By © Information System Department
Elements of the Date Format
Model
43
Produced By © Information System Department
Elements of the Date Format
Model
44
Produced By © Information System Department
Using the TO_CHAR Function with
Dates
SELECT last_name,
TO_CHAR(hire_date, 'fmDD Month YYYY') HIREDATE
FROM employees;
45
Produced By © Information System Department
Using the TO_CHAR Function with
Numbers
46
Produced By © Information System Department
Using the TO_NUMBER and
TO_DATE Functions
TO_NUMBER(char[, 'format_model'])
TO_DATE(char[, 'format_model'])
47
Produced By © Information System Department
Nesting Functions
F3(F2(F1(col,arg1),arg2),arg3)
SELECT last_name,
NVL(TO_CHAR(manager_id), 'No Manager')
FROM employees
WHERE manager_id IS NULL;
48
Produced By © Information System Department
NVL Function
49
Produced By © Information System Department
Conditional Expressions
50
Produced By © Information System Department
The CASE Expression
51
Produced By © Information System Department
The CASE Expression
52
Produced By © Information System Department
The DECODE Function
53
Produced By © Information System Department
The DECODE Function
54
Produced By © Information System Department
Obtaining Data from Multiple
Tables
55
Produced By © Information System Department
Cartesian Products
56
Produced By © Information System Department
What Is an Equijoin?
57
Produced By © Information System Department
Retrieving Records with Equijoins
SELECT employees.employee_id,
employees.last_name,
employees.department_id,
departments.department_id,
departments.location_id
FROM employees, departments
WHERE employees.department_id =
departments.department_id;
58
Produced By © Information System Department
Additional Search Conditions
Using the AND Operator
59
Produced By © Information System Department
Qualifying Ambiguous Column
Names
60
Produced By © Information System Department
Using Table Aliases
61
Produced By © Information System Department
Joining More than Two Tables
62
Produced By © Information System Department
Nonequijoins
63
Produced By © Information System Department
Outer Joins
64
Produced By © Information System Department
Outer Joins Syntax
65
Produced By © Information System Department
Outer Joins Syntax
66
Produced By © Information System Department
Self Joins
67
Produced By © Information System Department
Joining a Table to Itself
68
Produced By © Information System Department
Group Functions
69
Produced By © Information System Department
Group Functions
• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
70
Produced By © Information System Department
Group Functions Syntax
71
Produced By © Information System Department
Using the AVG and SUM
Functions
72
Produced By © Information System Department
Using the MIN and MAX
Functions
You can use MIN and MAX for any data type.
73
Produced By © Information System Department
Using the COUNT Function
74
Produced By © Information System Department
Group Functions and Null Values
75
Produced By © Information System Department
Creating Groups of Data:
GROUP BY Clause Syntax
76
Produced By © Information System Department
Grouping by More Than One
Column
77
Produced By © Information System Department
Excluding Group Results: The
HAVING Clause
78
Produced By © Information System Department
Excluding Group Results: The
HAVING Clause
79
Produced By © Information System Department
Nesting Group Functions
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;
80
Produced By © Information System Department
Using a Subquery to Solve a
Problem
81
Produced By © Information System Department
Subquery Syntax
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
82
Produced By © Information System Department
Using a Subquery
SELECT last_name
FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
83
Produced By © Information System Department
Guidelines for Using Subqueries
84
Produced By © Information System Department
Types of Subqueries
85
Produced By © Information System Department
Single-Row Subqueries
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
86
Produced By © Information System Department
Executing Single-Row Subqueries
87
Produced By © Information System Department
The HAVING Clause with
Subqueries
88
Produced By © Information System Department
Multiple-Row Subqueries
Operator Meaning
IN Equal to any member in the list
ANY Compare value to each value
returned by the
subquery
ALL Compare value to every value
returned by the
subquery
89
Produced By © Information System Department
Multiple-Row Subqueries
90
Produced By © Information System Department
Null Values in a Subquery
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
no rows selected
91
Produced By © Information System Department
Data Manipulation Language
92
Produced By © Information System Department
The INSERT Statement Syntax
93
Produced By © Information System Department
Inserting Rows
94
Produced By © Information System Department
Inserting Rows with Null Values
95
Produced By © Information System Department
Copying Rows from Another
Table
96
Produced By © Information System Department
The UPDATE Statement Syntax
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
97
Produced By © Information System Department
The UPDATE Statement Syntax
UPDATE copy_emp
SET department_id = 110;
98
Produced By © Information System Department
Updating Two Columns with a
Subquery
99
Produced By © Information System Department
The DELETE Statement
100
Produced By © Information System Department
Deleting Rows from a Table
101
Produced By © Information System Department
Database Transactions
102
Produced By © Information System Department
Implicit Transaction Processing
103
Produced By © Information System Department
Committing Data
104
Produced By © Information System Department
ROLLBACK
ROLLBACK ;
105
Produced By © Information System Department
Controlling Transactions
106
Produced By © Information System Department
Rolling Back Changes to a Marker
UPDATE...
SAVEPOINT update_done;
Savepoint created.
INSERT...
ROLLBACK TO update_done;
Rollback complete.
107
Produced By © Information System Department
Locking
108
Produced By © Information System Department
Implicit Locking
109
Produced By © Information System Department
Data Definition Language DDL
Statement Description
110
Produced By © Information System Department
Database Objects
Object Description
111
Produced By © Information System Department
Naming Rules
112
Produced By © Information System Department
The CREATE TABLE Statement
• You specify:
– Table name
– Column name, column data type, and column size
113
Produced By © Information System Department
Creating Tables
114
Produced By © Information System Department
Tables in the Oracle Database
• User tables:
– Are a collection of tables created and maintained
by the user
– Contain user information
• Data dictionary:
– Is a collection of tables created and maintained by
the Oracle Server
– Contain database information
115
Produced By © Information System Department
Querying the Data Dictionary
Prefix Description
USER_ These views contain information about
objects owned by the user.
ALL_ These views contain information about all
of the tables (object tables and relational
tables) accessible to the user.
DBA_ These views are restricted views, which
can be accessed only by people who have
been assigned the DBA role.
V$ These views are dynamic performance
views, database server
performance, memory, and locking.
116
Produced By © Information System Department
Data Types
117
Produced By © Information System Department
Data Types
118
Produced By © Information System Department
Creating a Table by Using a
Subquery Syntax
119
Produced By © Information System Department
Creating a Table by Using a
Subquery Syntax
DESCRIBE dept80
120
Produced By © Information System Department
The ALTER TABLE Statement
121
Produced By © Information System Department
The ALTER TABLE Statement
122
Produced By © Information System Department
Adding a Column
123
Produced By © Information System Department
Modifying a Column
124
Produced By © Information System Department
Dropping a Column
125
Produced By © Information System Department
Dropping a Table
126
Produced By © Information System Department
Changing the Name of an Object
127
Produced By © Information System Department
Truncating a Table
128
Produced By © Information System Department
What Are Constraints?
129
Produced By © Information System Department
Data Integrity Constraints
Constraint Description
NOT NULL Specifies that the column cannot
contain a null value
UNIQUE Specifies a column or combination of
columns whose values must be unique
for all rows in the table
PRIMARY KEY Uniquely identifies each row of the
table
FOREIGN KEY Establishes and enforces a foreign
key relationship between the column
and a column of the referenced table
CHECK Specifies a condition that must be true
130
Produced By © Information System Department
Defining Constraints
131
Produced By © Information System Department
Adding a Constraint Syntax
132
Produced By © Information System Department
Dropping a Constraint
133
Produced By © Information System Department
Controlling User Access
• Database security:
– System security
– Data security
• System privileges: Gaining access to the database
• Object privileges: Manipulating the content of the
database objects
• Schemas: Collections of objects, such as tables,
views, and sequences
134
Produced By © Information System Department
Creating Users
135
Produced By © Information System Department
User System Privileges
137
Produced By © Information System Department
Object Privileges
Object
Privilege Table View Sequence Procedure
ALTER ÷ ÷
DELETE ÷ ÷
EXECUTE ÷
INDEX ÷
INSERT ÷ ÷
REFERENCES ÷ ÷
SELECT ÷ ÷ ÷
UPDATE ÷ ÷
138
Produced By © Information System Department
Object Privileges
139
Produced By © Information System Department
Granting Object Privileges
140
Produced By © Information System Department
How to Revoke Object Privileges
141
Produced By © Information System Department
Revoking Object Privileges
142
Produced By © Information System Department