Nguyễn Thị Hải Bình Khoa Công nghệ thông tin Đại học Giao thông Vận tải... Nguyễn Thị Hải Bình Type of SQL Statements Three essential categories • Data Definition Language DDL Statements
Trang 1ORACLE SQL
GV Nguyễn Thị Hải Bình Khoa Công nghệ thông tin Đại học Giao thông Vận tải
Trang 2Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Type of SQL Statements
Three essential categories
• Data Definition Language (DDL) Statements
• DDL allows you to create, modify, and remove database objects.
• Data Manipulation Language (DML) Statements
• DML is used to manipulate data.
• SELECT
• Is used to retrieve data from Oracle Database objects.
2
Trang 4Công nghệ Oracle
GV Nguyễn Thị Hải Bình
SQL Tools
SQL*Plus Command Line
Start up SQL*Plus Command Line
• Open command-line prompt (cmd)
• Type: sqlplus sys/pwd as sysdba
or sqlplus system/pwd
Exit SQL*Plus
• Type: exit
4
Trang 1111
Trang 14Object Context Menu
• Right-click on an object to display its
“Context Menu”
• Edit option: view/modify the table
definition.
• Other options open additional menus
allowing the user to issue commands graphically.
Trang 20Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Oracle’s naming standards
• The name should be 1 to 30 characters long
• The name is interpreted as uppercase (unless enclosed
in double quotes)
• The name must begin with a letter
• The name contains only letters, numbers, or thesethree symbols: $, #, _ (unless enclosed in doublequotes)
• If enclosed in double quotes, a column name
• is case sensitive
• can begin with any character
• include any letter, number, space character or symbol
20
Trang 22• A column defined as NUMBER(n, m)
• can have a maximum of (n-m) digits before the decimal point and m digits after the decimal point.
22
Trang 23• Stores alphanumeric data.
• Is the variable-length data type
BLOB
• Stores one value up to four gigabytes in size
• Usually reserved for multimedia applications such asstoring a video or audio file
23
Trang 24Công nghệ Oracle
GV Nguyễn Thị Hải Bình 24
Trang 25THE SELECT STATEMENT
25
Trang 26• What types of SELECT statements are possible?
• What else is interesting about SELECT statements?
26
Trang 27Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Simple Query
27
Trang 28Công nghệ Oracle
GV Nguyễn Thị Hải Bình 28
Trang 29Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Tips on writing good queries
• Use table alias
• Be careful with parentheses
29
Trang 30Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Tips on writing good queries
• Upper and lowercase make no difference so long as
they are not in quotation marks
• When enclosed in quotation marks (single or double),
then upper and lowercase are considered different.
30
Trang 32Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Simple WHERE clause examples
32
Trang 44Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Top – N Queries
44
Trang 46Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Sorted Query
46
Trang 47Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Sorting and Null Values
48
Trang 50Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Using DISTINCT
53
Trang 51• The NVL (value, replace) function replaces null values
in expressions, avoiding SQL errors The SET NULLenvironment variable does the same thing in SQL*Plus
• Null values sort as the highest value by default
54
Trang 5255
Trang 53Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Arithmetic Operators
56
Trang 54Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Logical Operators
57
Trang 57ROW FUNCTIONS
60
Trang 58• A function always returns a single value.
• You can call a function within a query or other SQLcommand
61
Trang 59Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Type of Functions
Single – row functions
• Functions that operate on a single row at a time
Grouping functions
• Aggregate Functions Functions that operate on a
group of rows at one time and return a single row
• Analytical Functions Functions that operate on groups
of rows and return one or more summary rows
Object Reference Functions
• Functions that manipulate the value in columns withthe REF datatype in object tables
User-Defined Functions.
62
Trang 62Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
String Functions
INSTR(expression, substring [, p [, i]])
• Returns the position of a substring within a string
• The position and occurrence parameters are optional
• The p parameter determines a start point to search
from
• If p<0, search from right to left.
• The i parameter indicates which duplicate, if any, of the
substring should be matched
65
Trang 64• Left or right pad a string from the left or the right (start
or end of the string) with the specified characters inthe second string, up to a string length of n characters
67
Trang 71Công nghệ Oracle
GV Nguyễn Thị Hải Bình 74
SQL> select rtrim('test2101','1') from dual
SQL> select rtrim('test2101','10') from dual;
SQL> select rtrim('test2101','2') from dual;
SQL> select rtrim('test2101','21') from dual
SQL> select rtrim('test2101','012') from dual
Trang 72Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
String Functions
REPLACE(expression, search [, replace])
• REPLACE will replace every occurrence of the searchstring with the replacement string
75
Trang 73Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
String Functions
TRANSLATE(expression, search [, replace])
• TRANSLATE function replaces a sequence of characters
in a string with another set of characters
• However, it replaces a single character at a time
76
Trang 74Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
String Functions
SUBSTR(expression, [-]position[, length])
• The SUBSTR function returns a portion of a string
• If the length parameter is omitted, then all charactersafter the value of position are returned
• If the position parameter is positive, then the substringvalue is extracted from the left of the string
• If the parameter is negative, the value is extracted fromthe right (end) of the string
77
Trang 80Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
Datatype Conversion Functions
Number Conversion Functions
• TO_CHAR(number[, format]): convert a number to astring
• TO_NUMBER(string [, format]):convert a string to anumber
Date Conversion Functions
• TO_CHAR(date[, format]): Converts a date to a string
• TO_DATE(string [, format]): Converts a stringrepresentation of a date to a date
83
Trang 84Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
Miscellaneous Functions
DECODE( expression , compare_value, return_value,
[,compare, return_value] [,default_return_value] )
with:
• expression is the value to evaluate
• compare_value is the value that can match the
evaluated value
• return_value is the value that is returned if
compare_value equals the value
• The default_return_value is the value that is returned if
no match is found
87
Trang 85Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Single-row Functions
Miscellaneous Functions
DECODE( expression , compare_value, return_value,
[,compare, return_value] [,default_return_value] )
88
Trang 86,'Harry','It is Harry!' ,'Joe','It is Joe!‘
,'Not Harry or Joe.')
DECODE('Joe'
,'Harry','It is Harry!' ,'Joe','It is Joe!'
,'Not Harry or Joe.')
DECODE('Mary'
,'Harry','It is Harry!' ,'Joe','It is Joe!'
,'Not Harry or Joe.')
Trang 8790
Trang 90Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Combining Functions
93
Trang 92Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Combining Functions
95
Trang 93JOINING TABLES
96
Trang 94Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Natural Join (Inner Join)
97
Trang 95suppliers orders
Trang 9699
Trang 97Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Left Outer Join
100
Trang 98suppliers orders
Trang 99102
Trang 100Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Right Outer Join
103
Trang 103Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Full Outer Join
106
Trang 106Công nghệ Oracle
GV Nguyễn Thị Hải Bình
GROUP BY
109
Trang 107Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Group By
110
Trang 118Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Analytic Functions
122
Trang 119Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Analytic Functions
123
Trang 120Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Query_partition_clause
124
Trang 121Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Order_by_clause
125
Trang 123Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ROLLUP and CUBE Clause
127
Trang 124Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ROLLUP and CUBE Clause
128
Trang 125Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ROLLUP and CUBE Clause
129
Trang 126Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ROLLUP and CUBE Clause
130
Trang 127131
Trang 128Công nghệ Oracle
GV Nguyễn Thị Hải Bình
A simple example
132
Trang 129Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Types of Subqueries
Based on what they return
• Single row/single column
• Multiple Rows / One Column Each
• Multiple Columns / Single or Multiple Rows
Based on the connection between calling and called queries (subqueries)
Trang 130Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Comparison Conditions and Subqueries
134
Trang 143Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ORDER BY in Composite Queries
• The ORDER BY clause is applied to all rows returned inthe final result set
• Columns in the ORDER BY clause can be referenced bycolumn names or column aliases present in the firstquery of the statement
147
Trang 144Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ORDER BY in Composite Queries
148
Trang 145Công nghệ Oracle
GV Nguyễn Thị Hải Bình
ORDER BY in Composite Queries
149
Trang 148[START WITH initial_condition]
CONNECT BY [nocycle] PRIOR recurse_condition
[ORDER SIBLINGS BY order_by_clause]
152
Trang 149Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CONNECT BY example
153
Trang 150Công nghệ Oracle
GV Nguyễn Thị Hải Bình 154
Trang 151Công nghệ Oracle
GV Nguyễn Thị Hải Bình
LEVEL example
155
Trang 152Công nghệ Oracle
GV Nguyễn Thị Hải Bình
START WITH and ORDER BY example
156
Trang 153Công nghệ Oracle
GV Nguyễn Thị Hải Bình
START WITH and ORDER BY example
157
Trang 155Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Loop in data
159
Trang 156Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Loop in data
160
Trang 157Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CONNECT_BY_ISCYCLE pseudocolumn
161
Trang 158Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CONNECT BY ROOT example
162
Trang 159Công nghệ Oracle
GV Nguyễn Thị Hải Bình
GROUP BY
163
Trang 160164
Trang 166Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Regular Expressions
• Regular expressions enable you to search for patterns
in string data by using standardized syntax conventions
Regular Expression Functions
• REGEXP_INSTR( string, pattern [, start_position [,
th_appearance [, return_option [, match_parameter ] ] ] ] )
170
Trang 167Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Regular Expressions
Regular Expression Functions
• REGEXP_SUBSTR( string, pattern [, start_position [,
Trang 168Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Regular Expressions
Regular Expression Functions
• REGEXP_REPLACE( string, pattern [, replacement_string[, start_position [, th_appearance [, match_parameter ] ] ] ] )
Trang 169Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Regular Expressions
Regular Expression Functions
• REGEXP_LIKE( string, pattern [, match_parameter ] )
173
Trang 186DATA MANIPULATION
LANGUAGE (DML)
190
Trang 190Công nghệ Oracle
GV Nguyễn Thị Hải Bình
INSERT Statement - Example
194
Trang 196Công nghệ Oracle
GV Nguyễn Thị Hải Bình
UPDATE Statement
200
Trang 197Công nghệ Oracle
GV Nguyễn Thị Hải Bình
UPDATE Statement - Example
201
Trang 198Công nghệ Oracle
GV Nguyễn Thị Hải Bình
UPDATE Statement - Example
202
Trang 199Công nghệ Oracle
GV Nguyễn Thị Hải Bình
UPDATE Statement - Example
203
Trang 200Công nghệ Oracle
GV Nguyễn Thị Hải Bình
UPDATE Statement – Example …
204
Trang 201Công nghệ Oracle
GV Nguyễn Thị Hải Bình
UPDATE Statement – Example …
205
Trang 202Công nghệ Oracle
GV Nguyễn Thị Hải Bình
DELETE Statement
206
Trang 203Công nghệ Oracle
GV Nguyễn Thị Hải Bình
DELETE Statement - Examples
207
Trang 204Công nghệ Oracle
GV Nguyễn Thị Hải Bình
DELETE Statement - Examples
• How would I write an Oracle DELETE statement todelete all records in TableA whose data in field1 &field2 DO NOT match the data in fieldx & fieldz ofTableB?
208
Trang 205Công nghệ Oracle
GV Nguyễn Thị Hải Bình
TRUNCATE TABLE Statement
• Is used to remove all records from a table
209
Trang 206Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement
210
Trang 207Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement
211
Trang 208Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement
212
Trang 209Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement – WHERE clause
213
Trang 210Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement – DELETE clause
214
Trang 211Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement – DELETE clause…
215
Trang 212Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement – DELETE clause…
216
Trang 213Công nghệ Oracle
GV Nguyễn Thị Hải Bình
MERGE Statement – DELETE clause…
217
Trang 214XML IN ORACLE
218
Trang 215219
Trang 216Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CREATE TABLE Statement
220
Trang 217Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CREATE TABLE AS Statement
• Is used to create a table from an existing table bycopying the existing table’s columns
221
Trang 218Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CREATE TABLE AS Statement - Examples
222
Trang 220Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Primary Key
• A primary key is a single field or combination of fields that
uniquely defines a record.
• None of the fields that are part of the primary key can contain a null value.
• A table can have only one primary key.
• In Oracle, a primary key can not contain more than 32 columns.
• A primary key can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.
224
Trang 221Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Primary Key - Examples
225
Trang 222Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Create A Primary Key – ALTER TABLE statement
226
Trang 223Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Drop/ Disable/ Enable A Primary Key
227
Trang 225Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Create A Foreign Key
229
Trang 226Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Create A Foreign Key - Examples
230
Trang 227Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Foreign Keys with cascade delete
• A foreign key with cascade delete means that if arecord in the parent table is deleted, then the
automatically be deleted
231
Trang 228Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Foreign Keys with cascade delete
232
Trang 229Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Foreign Keys with set null on delete
• A foreign key with "set null on delete" means that if arecord in the parent table is deleted, then thecorresponding records in the child table will have theforeign key fields set to null
• The records in the child table will not be deleted
233
Trang 230Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Foreign Keys with set null on delete
234
Trang 231Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Drop/ Disable/ Enable A Foreign Key
235
Trang 233Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Unique Constraints
237
Trang 234Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Drop/Disable/Enable A Unique Constraint
238
Trang 235• A check constraint can NOT include a SQL Subquery.
• A check constraint can be defined in either a SQL
239
Trang 236Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Check Constraints - Syntax
240
Trang 237Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Check Constraints - Examples
241
Trang 238Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Drop/ Disable/ Enable A Check Constraint
242
Trang 239Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Changing Table Structure
Table changes
• Change the name of a table
Adding or changing columns
• Change the datatype, length, or name of columns
Removing columns
• Remove a column and its data
Rebuilding tables
• Reordering the columns in a table
• Adding columns between existing columns
• Change the primary key of a table
243
Trang 240RENAME old_name TO new_name;
ALTER TABLE old_name TO new_name;
Trang 242Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Add Columns in a Table
Add multiple columns in a table
246
Trang 243Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Modify Columns in a Table
247
Trang 244Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Drop/Rename a Column
248
Trang 245249
Trang 246Công nghệ Oracle
GV Nguyễn Thị Hải Bình
CREATE VIEW Statement
250
Trang 247Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Update View
251
Trang 248Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Drop View
252
Trang 249Công nghệ Oracle
GV Nguyễn Thị Hải Bình
Question?
• Can you update the data in an Oracle VIEW?
• Does the Oracle View exist if the table is dropped fromthe database?
253