Académique Documents
Professionnel Documents
Culture Documents
Oracle Database
Welcome to the 1st module of this course Database
Management System 1! For this lesson, we will introduce to
you the type of database system and its components.
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
describe what was done by the large computers in the
late 1940's until 1980's. The process of data
processing before is still being done by most large
organization. Example large volumes of raw
transaction data fed into programs that update a master
file, with fixed format reports written to paper.
2. The term Data Management Systems refers to an
expansion of data processing where the raw data that
fed into programs that update a master file, with fixed
format reports written to paper. is now fed into the
system from a variety of sources, including but not
limited to ATMs, EFT, and direct customer entry
through the Internet. The master file concept has been
largely displaced by database management systems,
and static reporting replaced or augmented by ad-hoc
reporting and direct inquiry, including downloading of
data by customers.
Characteristics of Database
1. Concurrent Access – is one of the characteristic of a
database system that allows several users to access the
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
protection of the database from unauthorized access
and changes.
5. Transactions - A transaction is a group of actions
which are done within a database to bring it from one
consistent state to a new consistent state. A transaction
is atomic which means that it cannot be divided up
any further. Within a transaction, all or none of the
actions need to be carried out.
6. Data Persistence - Data persistence means that in a
database management system all data is maintained as
long as it is not deleted explicitly in the database. The
life span of data needs to be determined directly or
indirectly be the user and must not be dependent on
system features which means a database administrator
may identify data that must be deleted or retained.
Disadvantages of a DBMS
1. Danger of a Overkill: For small and simple
applications or for single users a database system is
often not advisable.
2. Complexity: Having database system creates
additional complexity and requirements into the
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
company and or organization. Managing the operation
of a database management system with several users
and databases is quite costly and demanding.
3. Qualified Personnel: The professional operation of a
database system requires fully trained staff. Without a
qualified database administrator nothing will work for
long as no one will able to maintain and manage the
data store in the database.
4. Costs: having a database management system means
new costs are generated for the system itself but also
for additional hardware and the more complex
handling of the system.
5. Lower Efficiency: A database system is a multi-use
software or also known as generic software which is
often less efficient than specialized software which is
produced and optimized exactly for one problem.
Data Models
The underlying the structure of a database is the data model: it
is a collection of conceptual tools for describing data,
relationships, semantics, and integrity constraints.
Relational Model
The relational model uses a collection of tables to represent
both data and the relationships among those data a table in
relational model is also know an Entity. Each table or entity
has multiple columns or also known as an attribute which is
used to uniquely identify once table or entity to the other, and
each column has a unique name. The data is arranged in a
relation which is visually represented in a two dimensional
table meaning it is a relationship or connections of two or
more table. In the database, the data is inserted into the table
in the form of tuples or also known as values per row. The
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
relational model is implemented in database where a relation
is represented by a table, a tuple is represented by a row, an
attribute is represented by a column of the table, attribute
name is the name of the column such as ‘identifier’, ‘name’,
‘city’ etc., and attribute value contains the value for column in
the row.
Lesson Summary:
In this lesson, you should have learned the following.
Database
Database Management System
ER Model
Advantage and Disadvantage if database
Terms to Remember!
Application programs - are said to exhibit physical
data independence if they do not depend on the
physical schema.
Data-known facts that can be recorded and that have
implicit meaning.
Data integrity- is a byword for the quality and the
Textbook:
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
• Oracle Press (2010). Applied Oracle Security
References:
References • Pratt, Philip J. (2010). Database management systems
• Rob, Peter & Coronel, Carlo (2009). Database
Management Systems
• Schwalbe, Kathy (2011). Management of Information
Technology Projects
• Wheeler, Evan (2011). Security Risk Management :
Building an Information Security Risk Management
Program from the Ground Up
Supplementary Video
https://www.youtube.com/watch?v=1057YmExS-I
https://www.youtube.com/watch?v=kTVLO9F1QoY
https://www.youtube.com/watch?v=0uWA4zps-3k
Suggested Reading
• SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
• Database management system. In Encyclopedia
Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/152201/d
atabase-management-system-DBMS.
• SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/
SQL
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
Oracle Database
Welcome to the second module of this course Database
Management System 1! For this lesson, we will features and
usage of Oracle 11 express edition.
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Tuning facility.
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
• A relational database is a collection of relations or a
connection of two-dimensional tables controlled by
the Oracle server.
SELECT
INSERT
UPDATE
DELETE
MERGE
CREATE
ALTER
DROP
RENAME
TRUNCATE
COMMENT
GRANT
REVOKE
4. Transaction control
COMMIT
ROLLBACK
SAVEPOINT
Lesson Summary:
In this lesson, you should have learned the following.
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
• Focus area of Oracle;
• Features; and
Terms to Remember!
• In 1970 the relational model was proposed by Dr.
E.F Codd and has been the basis for Relational
Database Management System.
• A relational database is a collection of relations or a
connection of two-dimensional tables controlled by
the Oracle server.
Textbook:
• Oracle Press (2010). Applied Oracle Security
References:
• Pratt, Philip J. (2010). Database management systems
• Rob, Peter & Coronel, Carlo (2009). Database
Management Systems
• Schwalbe, Kathy (2011). Management of Information
Technology Projects
• Wheeler, Evan (2011). Security Risk Management :
Building an Information Security Risk Management
Program from the Ground Up
References
Suggested Reading
• SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
Welcome to the second module of this course. For this lesson, we will
discuss the task of Database Administrator, System requirement and
Installation procedure.
System Requirements
1. Memory requirements:
2
MODULE OF INSTRUCTION
3. Operating system
– Groups:
– oinstall
– dba
Optional groups (if doing separation of duty across multiple users):
– oper
– asmdba
– asmoper
– asmadmin
• Software owner, usually oracle
Requirements:
Installation Guide:
Step 1:
Step 2:
4
MODULE OF INSTRUCTION
Step 3:
Step 3:
Step 4:
Step 5:
Once verified the downloading the Oracle will start
Step 6:
6
MODULE OF INSTRUCTION
Step 7:
8
MODULE OF INSTRUCTION
Click Finish
10
MODULE OF INSTRUCTION
Note:
1. After clicking the finish button, locate the Oracle 11g
Express edition Folder found on the start menu of your
Operating system.
2. Then click the folder, click the PL/SQL Command Line.
3. When the CMD is open try to log-in using the username
and password set during installation.
4. Type in Connect then click enter
5. Then type the password: oracle (note typing the password
is hidden on the screen)
You must have the CONNECTED message. This means you
successfully log-in in Oracle using the PL/SQL command line.
Lesson Summary:
Before preceding with the next Chapters the student should
finished the following:
Create Oracle Account – www.oracle.com
Successfully Install Oracle Database 11g Express
Edition
Able to establish as user Connection at isqlplus using:
CONNECT
USERNAME: SYSTEM (Global Connection to Oracle)
PASSWORD: oracle (password inputted by the student during
installation)
Glossary
NLS_LANG: Optional environment variable that controls
language, territory, and client character set settings
References
Textbook:
Oracle Database 11g 2nd Edition K Gopalakrishnan (
2012)
References:
Carlos, Peter (2009). Database Systems
Connoly, Thomas & Begg, Carolyn (2010). Database
Systems : A practical approach to design,
implementation and management
Sciore, Edward (2009). Database Design and
Implementation
Bulusu, Lakshman (2008). Oracle PL/SQL : Expert
Techniques for Developers and Database
Administrators
Loshin, David (2008). Master Data Management
https://docs.oracle.com/cd/B19306_01/server.102/b14220/intro.htm
http://ugweb.cs.ualberta.ca/~c391/manual/chapt1.html
http://www.oracle-dba-online.com/introduction_to_oracle.htm
12
MODULE OF INSTRUCTION
Supplementary Video
https://www.youtube.com/watch?v=2deZysmz9rE&index=1&list=PLg
Xga1uh_X7dkEmttwzU7ulLa_abCq0N4
https://www.youtube.com/watch?v=B1EO24v2ico
https://www.youtube.com/watch?v=2deZysmz9rE
Welcome to the second module of this course. For this lesson, we will
discuss the task of Database Administrator, System requirement and
Installation procedure.
System Requirements
1. Memory requirements:
2
MODULE OF INSTRUCTION
3. Operating system
– Groups:
– oinstall
– dba
Optional groups (if doing separation of duty across multiple users):
– oper
– asmdba
– asmoper
– asmadmin
• Software owner, usually oracle
Requirements:
Installation Guide:
Step 1:
Step 2:
4
MODULE OF INSTRUCTION
Step 3:
Step 3:
Step 4:
Step 5:
Once verified the downloading the Oracle will start
Step 6:
6
MODULE OF INSTRUCTION
Step 7:
8
MODULE OF INSTRUCTION
Click Finish
10
MODULE OF INSTRUCTION
Note:
1. After clicking the finish button, locate the Oracle 11g
Express edition Folder found on the start menu of your
Operating system.
2. Then click the folder, click the PL/SQL Command Line.
3. When the CMD is open try to log-in using the username
and password set during installation.
4. Type in Connect then click enter
5. Then type the password: oracle (note typing the password
is hidden on the screen)
You must have the CONNECTED message. This means you
successfully log-in in Oracle using the PL/SQL command line.
Lesson Summary:
Before preceding with the next Chapters the student should
finished the following:
Create Oracle Account – www.oracle.com
Successfully Install Oracle Database 11g Express
Edition
Able to establish as user Connection at isqlplus using:
CONNECT
USERNAME: SYSTEM (Global Connection to Oracle)
PASSWORD: oracle (password inputted by the student during
installation)
Glossary
NLS_LANG: Optional environment variable that controls
language, territory, and client character set settings
References
Textbook:
Oracle Database 11g 2nd Edition K Gopalakrishnan (
2012)
References:
Carlos, Peter (2009). Database Systems
Connoly, Thomas & Begg, Carolyn (2010). Database
Systems : A practical approach to design,
implementation and management
Sciore, Edward (2009). Database Design and
Implementation
Bulusu, Lakshman (2008). Oracle PL/SQL : Expert
Techniques for Developers and Database
Administrators
Loshin, David (2008). Master Data Management
https://docs.oracle.com/cd/B19306_01/server.102/b14220/intro.htm
http://ugweb.cs.ualberta.ca/~c391/manual/chapt1.html
http://www.oracle-dba-online.com/introduction_to_oracle.htm
12
MODULE OF INSTRUCTION
Supplementary Video
https://www.youtube.com/watch?v=2deZysmz9rE&index=1&list=PLg
Xga1uh_X7dkEmttwzU7ulLa_abCq0N4
https://www.youtube.com/watch?v=B1EO24v2ico
https://www.youtube.com/watch?v=2deZysmz9rE
INSERT statement
Insert a new row containing values for each column.
List values in the default order of the columns in the
table.
Optionally, list the columns in the INSERT clause.
Enclose character and date values within single
quotation marks.
Add new rows to a table by using the INSERT
statement:
Syntax:
INSERT INTO tbl_name values (column_list);
In the syntax:
o TBL_NAME - Is the name of the table
o COLUMN_LIST - Is the name of the column
in the table to populate
o VALUES - Is the corresponding value for the
column
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Output:
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
those values with no inputs this is set to NULL in the
INSERT INTO statement. By using this method
automatically column with values set to NULL will
automatically have NULL values.
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
Output: Old table
DELETE Statement
You can remove existing rows from a table by using
the DELETE statement:
Syntax:
DELETE FROM TBL_NAME
WHERE CONDITION;
In the syntax:
Tbl_name - Is the name of the table
Where condition - Identifies the rows to be deleted,
and is composed of column names, expressions,
constants, subqueries, and comparison operators.
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
As in the output all rows are deleted if WHERE
clause is omitted.
Lesson Summary:
In this lesson, you should have learned how to use the
following statements:
Terms to Remember!
DELETE – a statement use to remove existing rows
in a table.
DML – stand for Data Manipulation Language.
Explicit method - an Insert statement that Specifies
the NULL keyword in the VALUES clause.
Implicit method - an Insert statement that Omits the
column from the column list.
INSERT - a statement used to add new rows in a
table.
Transaction - consists of a collection of DML
statements that form a logical unit of work.
UPDATE - a statement used to update existing rows
in a table.
References Textbook:
Oracle Press (2010). Applied Oracle Security
References:
Pratt, Philip J. (2010). Database management systems
Supplementary Video
https://www.youtube.com/watch?v=S54W4oSqp7s
https://www.youtube.com/watch?v=vnFu6sBfcSs
https://www.youtube.com/watch?v=0EmMIUvjPbA
Suggested Reading
SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
Database management system. In Encyclopedia
Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/152201/
database-management-system-DBMS.
SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/
SQL
Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_admini
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
stration.aspx
SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
Tutorialspoint.com
oracle.com
apex.oracle.com
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Select (ALL) column
• You can display all rows and column in a table by using
the SELECT followed by (*) asterisk.
• (*) is used to call/display all row in column.
• Example:
SELECT * FROM AUTHORS
• Output:
•
• As shown in the output all rows and columns in the
AUTHORS table are displayed.
• Example:
SELECT LNAME, FNAME
FROM AUTHORS;
• Output:
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
• Keywords typically are entered in uppercase; all other
words, such as table names and columns names are
entered in lowercase this is to make the codes readable.
Arithmetic Expressions
• Use arithmetic expression in order to create expression
with number and date values.
• An arithmetic expression can contain column names,
constant numeric values, and the arithmetic operators.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
• Just in case you wanted to create a report that computes
for the possible additional year of 5 to all books
published by the author.
• Example:
SELECT LNAME, FNAME, BOOK, YR_PUB + 5
FROM AUTHORS;
• Output:
•
• As shown in the output originally the year publish of Mr.
Operator Precedence
If the select statement containing an arithmetic expression
with more than one operator, multiplication, and division are
evaluated first. However, if operators in an expression are of
the same priority, evaluation is done from left to right.
You can use parentheses to force the expression that is
enclosed by the parentheses to be evaluated first.
Rules of Precedence
• Multiplication and division are evaluated first before
addition and subtraction.
• Operators of the same priority are evaluated from left to
right.
• Parentheses ( ) are used to override the default
precedence or to clarify the statement.
• Example: without the parentheses operator
SELECT LNAME, FNAME, BOOK, 10 * YR_PUB +
5
FROM AUTHORS;
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
• Output:
multiplied to 10.
• Output:
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
• For example, if you attempt to perform division by zero,
you get an error. However, if you divide a number by
null, the result is a null or unknown.
• Example:
SELECT LNAME, FNAME, BOOK, 10 * YR_PUB * 5
FROM AUTHORS;
• Output:
• Explanation:
Notice that value for the ID 20 for the YR_PUB
column is still NULL.
Column Aliases
When displaying the result of a query, the name of the
column after the table has been created will be once to be
displayed in the output. This column heading may not be
descriptive and, therefore, may be difficult to understand
most especially if it contains additional SELECT like
arithmetic expression. To change a column heading use a
column alias.
A column alias:
• Renames a column heading
• Is useful for calculations
• Immediately follows the column name (There can also be
the optional AS keyword between the column name and
the alias.)
• Requires double quotation marks if it contains spaces or
special characters, or if it is case-sensitive
• Example: AS Keyword
SELECT FNAME AS NAME, LNAME AS
SURNAME, YR_PUB + 5 AS YEAR
FROM AUTHORS;
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
Concatenation operator:
• Links columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character expression
• Example:
SELECT LNAME||FNAME AS "COMPLETE NAME", BOOK
FROM AUTHORS;
• Output:
• Output:
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
columns:
SELECT LNAME||' , '||FNAME|| ' is the author of
' ||BOOK as "AUTHORS RECORD"
from AUTHORS;
• Output:
Lesson Summary:
In this lesson, you should have learned how to use the
following statements:
• Use the * to call all rows and columns in the table.
• Display specific column by specifying the column lists in
the select list.
• Make use of arithmetic expression
• Rename a column heading
• Make use of concatenation operator as well as character
literal string.
Terms to Remember!
• Arithmetic Expressions - Create expression with
number and date values.
• Column Alias - renames a column heading
• Concatenation Operator - Links columns or character
Textbook:
References • Oracle Press (2010). Applied Oracle Security
References:
• Pratt, Philip J. (2010). Database management systems
• Rob, Peter & Coronel, Carlo (2009). Database
Management Systems
• Schwalbe, Kathy (2011). Management of Information
Technology Projects
• Wheeler, Evan (2011). Security Risk Management :
Building an Information Security Risk Management
Program from the Ground Up
_____________________________________________________________________________________
14
MODULE OF INSTRUCTION
https://www.techonthenet.com/oracle/select.php
Supplementary Video
https://www.youtube.com/watch?v=otI5dbmEU7c
https://www.youtube.com/watch?v=SoxHivg9Ldw
https://www.youtube.com/watch?v=WB0zBS1wre4
Suggested Reading
• SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
• Database management system. In Encyclopedia
Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/152201/dat
abase-management-system-DBMS.
• SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQ
L
• Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_administra
tion.aspx
• SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
• Tutorialspoint.com
• oracle.com
• apex.oracle.com
WHERE Clause You can restrict the rows that are returned from the query by
using the WHERE clause. A WHERE clause contains a
condition that must be met and it directly follows the FROM
clause. If the condition is true, the row meeting the condition
is returned.
Where Clause
• Restrict the rows that are returned by using the WHERE
clause:
• Syntax:
SELECT (COLUMN_LIST [EXPRESSION])
FROM TBLE_NAME
WHERE CONDITION;
• Where:
o A SELECT clause, which specifies the columns
to be displayed
o A FROM clause, which identifies the table
containing the columns that are listed in the
SELECT clause
o A WHERE is used to identify what specific
condition you would like to add in the select
statement to restrict the rows returned by a query.
• You can use WHERE clause to compare values in
columns, add literal character string in between two or
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
more column, apply arithmetic expressions, or functions.
It consists of three elements:
• Column name
• Comparison condition
• Column name, constant, or list of values
• Example:
SELECT ID, LNAME, FNAME, BOOK, TYPE
FROM AUTHORS
WHERE TYPE = 'CSIT';
• Output:
• Explanation:
As shown in the example from the original 8 rows the
report now only shows all authors in which the type of
the book is equal to CSIT.
• Explanation:
No rows are select because of the condition
TYPE=’GenEd’, since this type is in capital letter during
the time that the user inserted the values. This only
means that we have to follow the character type of the
values whether in capital, small or initcap.
Comparison Condition:
Comparison operators are used in conditions that
compare one expression with another value or
expression.
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> or != Not equal to
IN (SET) Match any of the values
inside the (SET)
BETWEEN..AND Between two inclusive
values
LIKE Match a character pattern
IS NULL Is a NULL value
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
Example:
SELECT LNAME, FNAME, BOOK, YR_PUB
FROM AUTHORS
WHERE YR_PUB < 1995;
Output:
Explanation:
Record shown that there is only one author who meets
the condition of YR_PUB less than to 1995.
Example:
SELECT LNAME, FNAME, BOOK, YR_PUB
FROM AUTHORS
WHERE YR_PUB <= 1995;
Output:
Explanation:
Record shown that there is two authors who meets the
condition of YR_PUB either less than or equal to 1995.
FROM AUTHORS
WHERE ID BETWEEN 10 AND 15;
Output:
Explanation:
Record shows the lists of authors whose ID is in between
the lowest range and highest range of 10 to 15. So
basically this is from 10, 11,12,13,14 and 15 any number
equal to these ranges will be included in the output.
FROM AUTHORS
WHERE TYPE IN ('BA','GENED');
Output:
Explanation:
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
Record shows the lists of authors whose type is equal to
the values called in the set either the type is (BA or
GENED).
FROM AUTHORS
WHERE FNAME LIKE 'M%';
• Output:
• Explanation:
Record shows the lists of authors whose first name starts
with capital letter M.
• Example: Getting the second letter
SELECT ID, FNAME, LNAME, BOOK, TYPE, AC_NO, YR_PUB
FROM AUTHORS
WHERE LNAME LIKE '%O';
• Output:
• Explanation:
Record shows the lists of authors whose last name ends
with capital letter O.
Escape Identifier
The percentage % and underscore _ symbols can be used in
any combination with literal characters. When you need to
have an exact match for the actual percentage % and
underscore _ characters, use the ESCAPE identifier. This
option specifies what the escape character is.
• Example: Skipping the first letter
SELECT ID, FNAME, LNAME, BOOK, TYPE, AC_NO, YR_PUB
FROM AUTHORS
WHERE FNAME LIKE '_E%';
• Output:
• Explanation:
Record shows the lists of authors whose first name
second letter is equal to capital letter E regardless of its
starting character because of _ (underscore) it skips the
first letter.
• Example: Getting string/number or character in
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
between %%.
SELECT ID, FNAME, LNAME, BOOK, TYPE, AC_NO, YR_PUB
FROM AUTHORS
WHERE LNAME LIKE '%CA%';
• Output:
• Explanation:
Record shows the lists of authors whose last name have
the string equal to CA whether on the first, in between
and or last.
FROM AUTHORS
WHERE FNAME IS NULL;
• Output:
• Explanation:
Explanation:
The lastname are sorted in Descending order starting
from z-a.
Example:
SELECT LNAME, BOOK, TYPE
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
FROM AUTHORS
WHERE TYPE = 'CSIT'
ORDER BY LNAME ASC;
Example: default
SELECT LNAME, BOOK, TYPE
FROM AUTHORS
WHERE TYPE = 'CSIT'
ORDER BY LNAME;
OUTPUT:
Lesson Summary:
In this lesson, you should have learned how to use the
following statements:
• Where condition that applies comparison condition
• Where condition that applies other comparison operator.
Terms to Remember!
• BETWEEN operators – is used to display rows based on
a range of values.
• Comparison operators - are used in conditions that
compare one expression with another value or
expression.
• IN operator - is used to test for values in a list.
• LIKE operator – use to perform wildcard searches of
valid search string values.
• Null value - means that the value is unavailable,
unassigned, unknown, or inapplicable.
Textbook:
• Oracle Press (2010). Applied Oracle Security
References:
• Pratt, Philip J. (2010). Database management systems
• Rob, Peter & Coronel, Carlo (2009). Database
Management Systems
• Schwalbe, Kathy (2011). Management of Information
Technology Projects
• Wheeler, Evan (2011). Security Risk Management :
Building an Information Security Risk Management
Program from the Ground Up
Supplementary Video
https://www.youtube.com/watch?v=BYl7bz234Ew
https://www.youtube.com/watch?v=rkFnksNgQTk
https://www.youtube.com/watch?v=vxlD94G0Geo
Suggested Reading
• SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
• Database management system. In Encyclopedia
Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/152201/data
base-management-system-DBMS.
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
• SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQ
L
• Database Administration. In Encyclopedia.com,
Retrieved from
http://www.encyclopedia.com/topic/Database_administra
tion.aspx
• SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
• Tutorialspoint.com
• oracle.com
• apex.oracle.com
The select statement is used to create different types of reports where the
Logical
user can apply the basic select, restrict the rows returned by the query,
Condition
use single row or multiple row functions and sort the data either
ascending or descending.
Explanation:
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
As shown in the output only those authors who have a type of CSIT
and book that contains the last letter equal to A are selected.
Explanation:
As shown in the output only those authors who have a type of CSIT
or either a book that contains the last letter equal to A are selected.
Explanation:
As shown in the output only those authors who have a YR_PUB not
equal to the value inside the IN (SET) are selected.
Rules of Precedence
The rules of precedence determine the order in which expressions are
evaluated and calculated.
The table in the slide lists the default order of precedence.
To override the default order of expression to be calculated first put
parentheses around the expression.
Operator Meaning
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition
Example:
SELECT (FNAME||','||LNAME)AS NAME, BOOK,
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
TYPE,AC_NO
FROM AUTHORS
WHERE TYPE='CSIT'
OR AC_NO LIKE'%S'
AND LNAME LIKE 'S%';
Output:
Explanation:
In this example, there are two conditions:
The first condition is that the AC_NO end with capital letter S and
the LNAM start with capital letter S.
The second condition is that the TYPE is CSIT.
Therefore, the SELECT statement reads as follows:
“Select the row if an author AC_NO ends with letter S and the
LNAME starts with letter S or the TYPE is equal to CSIT.
Example: with parentheses
SELECT (FNAME||','||LNAME)AS NAME, BOOK,
TYPE,AC_NO
FROM AUTHORS
WHERE (TYPE='CSIT'
OR AC_NO LIKE'%S')
AND LNAME LIKE 'S%';
Output:
Explanation:
In this example, there are two conditions:
The first condition is that the AC_NO ends with letter S or the TYPE
are equal to CSIT
The second condition is that the LNAME ends with letter S.
“Select the row if an author AC_NO ends with letter S and the
LNAME starts with letter S and the TYPE is equal to CSIT.
Lesson Summary:
In this lesson, you should have learned how to use the following
statements:
Apply the logical condition.
Identify the order of precedence.
Terms to Remember!
AND - Both component condition can be true for any record to be
selected.
Logical condition - combines the result of two component conditions
to produce a single result based on those conditions or it inverts the
result of a single condition
NOT - Reverse the value from TRUE to FALSE or FALSE to TRUE.
OR - Either component condition can be true for any record to be
selected.
Rules of precedence - determine the order in which expressions are
evaluated and calculated.
Textbook:
Oracle Press (2010). Applied Oracle Security
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
References:
Supplementary Video
https://www.youtube.com/watch?v=JRmpM9UNY-w
https://www.youtube.com/watch?v=OjJUPYS6o1E
https://www.youtube.com/watch?v=eXGOYouMkJA
Suggested Reading
SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
Database management system. In Encyclopedia Britannica, Retrieved
from http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.aspx
SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
Tutorialspoint.com
oracle.com
apex.oracle.com
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
Functions make the basic query block more powerful, and they are
Single-Row
used to manipulate data values.
Function
After completing this lesson, the student should be able to:
• Describe the various types of functions available in SQL
• Use the character, number, and date functions in SELECT
statements
Single-row functions:
• Use to manipulate data items
• Can accept arguments that may return one value
• Act on each row that is returned
• Can return one result per row
• Can be used to modify the data type
• Arguments can be nested
• Can accept arguments that can be a column or an expression
• Syntax:
SELECT Function_name (arguments1,2 [expression])
FROM tbl_name;
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Where:
function_name - Is the name of the function
arg1, arg2 - Is any argument to be used by the function. This
can be represented by a column name or expression.
From tbl_name – is the name of the table
Where:
• LOWER: Converts mixed-case or uppercase character strings to
lowercase
• UPPER: Converts mixed-case or lowercase character strings to
uppercase
• INITCAP: Converts the first letter of each word to uppercase and
the remaining letters to lowercase
Explanation:
It retrieves the record of all authors whose LNAME starts with
capital S then converts the concatenated LNAME and FNAME to
upper case.
Note that since during the insertion of values into authors table all
value is inserted in capital form the reason why no changes in the
conversion as shown in the output.
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
Using Case-Conversion function: lower
Example: lower
SELECT LOWER(LNAME||','||FNAME) AS "NAME"
FROM AUTHORS
WHERE LNAME LIKE'S%';
Output:
Explanation:
It retrieves the record of all authors whose LNAME starts with
capital S then converts the concatenated LNAME and FNAME to
lowercase.
Explanation:
It retrieves the record of all authors whose LNAME starts with
capital S then converts the concatenated LNAME and FNAME to
sentence case like. Where the first letter is capitalized and every
time an SQL encounter special character and or space is
automatically capitalized the first letter of the following string and
or character.
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
Using Character Manipulation Function: Concat
Example: CONCAT
SELECT CONCAT(LNAME,FNAME), YR_PUB
FROM AUTHORS
WHERE YR_PUB = 2010;
Output:
Explanation:
It retrieves the record of all authors whose YR_PUB is equal to
2010, then concatenates the LNAME to FNAME.
Explanation:
It retrieves the record of all authors whose BOOK counting from
1-6 is equal to ING.
Explanation:
It retrieves the record of all authors whose BOOK name starts with
the capital letter A then gets the length of the book as well as the
character position of A in the book column. Notice that the Oracle
only returns the character position of the first A it encounters
starting from the first letter up to the last. Like for example the
work DATABASE is composed of 3 A’s but since it only reads the
first letter A its INSTR value is 2 (second letter equal to A)
Explanation:
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
It retrieves the record of all authors whose BOOK has an ING
string then pads the YR_PUB to the left and replace the other
numbers with $ dollar sign symbol to make the digits size 6, as
well as pads the YR_PUB to the right and replace the other
numbers with $ dollar sign symbol to make the digits size 6.
Explanation:
It retrieves the record of all authors FNAME then replaced the
letter A with T in the column FNAME.
Explanation:
It retrieves the record of all authors BOOK then trim the letter A
from BOOK Column.
Number Function
Function Result
ROUND(75. 46,1) 75.5
TRUNC(75.46,1) 75
MOD(100/3) 10
Where:
• ROUND: a type of number function that is used to rounds value to
a specified decimal
• TRUNC: a type of number function that is used to truncates value
to a specified decimal
• MOD: a type of number function that is used to returns remainder
of division
DUAL Table
The DUAL table is owned by the user SYS and can be accessed by all
users. It contains one column, DUMMY, and one row with the value
X. The DUAL table is useful when you want to return a value only
once (for example, the value of a constant, pseudo column, or
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
expression that is not derived from a table with user data). The DUAL
table is generally used for completeness of the SELECT clause syntax
because both SELECT and FROM clauses are mandatory, and several
calculations do not need to select from the actual tables.
Output:
Explanation:
ROUND(75.475,2) – Round the number starting from decimal
point count to 2 = 75.47 and since the following number of 7 is 5 it
round off to 8 the which results to 75.48
ROUND(75.475,1) – Round the number starting form decimal
point count to 1 = 75.4 and since the following number of 4 is 7 it
round off to 5 the which results to 75.5
ROUND(75.475,-1) – Round the number starting form decimal
point count to 1 to the left(because of – negative sign) = 75 and
since the following number of 7 is 5 it rounds off to 8 and makes
the value of 5 to 0 which results to 80.
Explanation:
TRUNC(75.475,2) – Truncate/cut the number starting from
decimal point 2 which result to 75.47
TRUNC(75.475,1) – Truncate/cut the number starting from
decimal point 1 which result to 75.4
TRUNC(75.475,-1) – Truncate/cut the number starting from the
decimal point -1(starting to the left) which result to 75 and since
we have to cut the digit at position -1 make this digit 0 which
result to 70.
Explanation:
It gets the remainder of 1000 over 300 which is equal to 100.
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
Lesson Summary:
In this lesson, you should have learned how to:
• Perform calculations on data using functions
• Modify case and character values.
Terms to Remember!
• Character functions: Accept character input and can return both
character and number values
• CONCAT: Joins values together (You are limited to using two
parameters with CONCAT.) instead used || (double bars) if it
contains two or more columns and character literal string
• DUAL - the table that is owned by the user SYS and can be
accessed by all users.
• INITCAP: Converts the first letter of each word to uppercase and
the remaining letters to lowercase
• INSTR: Finds the numeric position of a named character
• LENGTH: Shows the length of a string as a numeric value
• LOWER: Converts mixed-case or uppercase character strings to
lowercase
• LPAD: Returns an expression left-padded to the length of n
characters with a character expression
• Multiple-row functions – returns one result per group of row.
• MOD: Returns remainder of division
• Number functions: Accept numeric input and return numeric
values
• ROUND: Rounds value to a specified decimal
• RPAD: Returns an expression right-padded to the length of n
characters with a character expression
• Single-row functions - returns one result per row.
• SUBSTR: Extracts a string of determined length
• TRIM: Trims leading or trailing characters (or both) from a
References Textbook:
• Oracle Press (2010). Applied Oracle Security
References:
• Pratt, Philip J. (2010). Database management systems
• Rob, Peter & Coronel, Carlo (2009). Database Management
Systems
• Schwalbe, Kathy (2011). Management of Information Technology
Projects
• Wheeler, Evan (2011). Security Risk Management : Building an
Information Security Risk Management Program from the Ground
Up
Supplementary Video
https://www.youtube.com/watch?v=5rx8Q4x4-qI
https://www.youtube.com/watch?v=WQe-p2F3Kcg
https://www.youtube.com/watch?v=yjXXntX0sh8
Suggested Reading
• SQL Tutorial. In ws3schools, Retrieved from
_____________________________________________________________________________________
14
MODULE OF INSTRUCTION
http://www.w3schools.com/sql/default.asp
• Database management system. In Encyclopedia Britannica,
Retrieved from
http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
• SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
• Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.aspx
• SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
• Tutorialspoint.com
• oracle.com
• apex.oracle.com
Group Function helps how to group rows in a table into smaller sets and how
Multiple-
to specify search criteria for groups of rows.
Row
Function After completing this lesson, the student should be able to:
1. Identify the available group functions
2. Describe the use of group functions
3. Group data by using the GROUP BY clause
4. Use the HAVING clause to include or exclude group function.
Group Function
Group functions are used to operate on sets of rows in order to give one
result per group. These sets may comprise either the entire table or a
table may be split into groups.
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Explanation:
The sample output displays the average and sum YR_PUB of all authors.
Explanation:
The sample output displays the authors LNAME and FNAME that is
first in the alphabet lists for MIN function and last in the alphabet lists
for MAX function.
Explanation:
The sample output displays the author’s standard deviation for STDDEV
function and variance for VARIANCE function applied in ID column.
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
Explanation:
The sample output displays the total number of authors in the table.
Example: Using Count(expr)
SELECT COUNT(TYPE)
FROM AUTHORS
WHERE TYPE='CSIT';
Output:
Explanation:
The sample output displays the total number of authors whose book
TYPE is CSIT.
Explanation:
The sample output displays the number of distinct value that are in
authors table using the YR_PUB column.
Explanation:
The output for default function displays the calculated average based
only on those rows with values in the table in which a valid value is
stored in the YR-PUB column. The average is calculated as the total
YR_PUB to all authors divided by the number of authors with valid
values in YR_PUB column which is equal to 7.
The output for function with NVL displays the calculated average
regardless of whether null values are stored in the YR-PUB column. The
average is calculated as the total YR_PUB to all authors divided by the
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
number of authors with or without valid values in YR_PUB column
which is equal to 8 (including author with NULL YR_PUB).
Explanation:
The sample select the authors book TYPE and count the number of book
TYPE, the group by clause specifies that the values retrieved should be
group per book TYPE and count the type per group. This only means
that there are 4 types of CSIT, 2 for GENED and 2 for BA.
Example: Violation
Output:
In the example sing TYPE column has no function enforce while
COUNT(*) is a function this will cause an error, the reason why column
with no function should be called in GROUP BY Clause.
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
Grouping two (2) columns
To see the results for groups within group use this example.
Example:
SELECT TYPE, YR_PUB, SUM(YR_PUB), AVG(YR_PUB)
FROM AUTHORS
GROUP BY TYPE, YR_PUB;
Output:
Explanation:
The sample displays the authors book TYPE, YR_PUB and get the SUM
and AVG for YR_PUB column then group the data by TYPE and
YR_PUB, since the YR_PUB have more distinct values the select
statement then prioritize the columns with the most number of distinct
values.
You can always use the WHERE condition to restrict the rows returned
by the query.
Example: Group by with WHERE condition
SELECTTYPE, YR_PUB,
COUNT(DISTINCT(YR_PUB)),SUM(YR_PUB), AVG(YR_PUB)
FROM AUTHORS
WHERE YR_PUB > 1995
GROUP BY TYPE, YR_PUB;
Output:
Explanation:
The sample displays the authors book TYPE, YR_PUB and gets the
number of distinct values, SUM and AVG for YR_PUB column where
YR_PUB is greater than 1995, then group the data by TYPE and
YR_PUB.
Having Clause
• You cannot use the WHERE clause to restrict groups.
• You use the HAVING clause to restrict groups.
• You cannot use group functions in the WHERE clause.
• Syntax
SELECT function_name(column), group_function(column)
FROM table_name
[WHERE condition]
[GROUP BY group_by_expr]
[HAVING group Condition]
[ORDER BY column];
• Example: Illegal Queries
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
Explanation:
Sample shows that a WHERE condition cannot have a group function in
it ex: WHERE MAX(ID)>15 to restrict the rows return pure column can
only be applied to WHERE condition. By the time that you wanted to
restricts the rows with group function.
Example: with HAVING CLAUSE
SELECT YR_PUB,
COUNT(DISTINCT(YR_PUB)),SUM(YR_PUB), AVG(YR_PUB)
FROM AUTHORS
GROUP BY YR_PUB
HAVING MAX(ID)>15;
Output:
Explanation:
The sample displays the YR_PUB, the distinct values for YR_PUB,
SUM and AVG then group the data per YR_PUB and restricts the rows
retuned having maximum ID greater than 5;
Complete SELECT
Example:
SELECT TYPE, MIN(FNAME),MAX(ID)
FROM AUTHORS
WHERE ID BETWEEN 10 AND 15
GROUP BY TYPE
HAVING MAX(ID)>= 5
ORDER BY MAX(ID) DESC;
Output:
Explanation:
The sample displays the authors book TYPE, number of distinct values
for YR_PUB, SUM and AVG for YR_PUB column whose ID is in
between the lowest and highest range of 10-15 then group the data per
YR_PUB having maximum ID which is greater than or equal to 15 and
sort the data by maximum ID in descending order.
Explanation:
The sample output displays the maximum average YR_PUB of authors
per book TYPE.
_____________________________________________________________________________________
12
MODULE OF INSTRUCTION
Lesson Summary:
In this lesson, you should have learned how to:
• Use the group functions COUNT, MAX, MIN, SUM, and
AVG
• Include the NULL values using the NVL function
• Write queries that use the GROUP BY clause
• Write queries that use the HAVING clause
• Group date using 2 columns
• And apply Nested Groups
Terms to Remember!
AVG - Average value of N ignoring null values
COUNT - Count the number of rows where expr evaluates to something
other than null
COUNT(DISTINCT expr) – While COUNT(DISTINCT expr) returns
the number of unique, non-null values that are in the column identified
by expr.
COUNT(expr) - In contrast, COUNT(expr) returns the number of non-
null values that are in the column identified by expr.
COUNT(*) - COUNT(*) returns the number of rows in a table that
satisfy the criteria of the SELECT statement, including duplicate rows
and rows containing null values in any of the columns.
Group functions - operate on sets of rows to give one result per group.
MAX - Maximum value of expression ignoring null values
MIN - Minimum value of expression ignoring null values
STDDEV - Standard deviation of N ignoring null values
SUM - Sum values of N ignoring null values
VARIANCE - Variance - of N ignoring null values
Textbook:
References Oracle Press (2010). Applied Oracle Security
References:
Pratt, Philip J. (2010). Database management systems
Rob, Peter & Coronel, Carlo (2009). Database Management Systems
Schwalbe, Kathy (2011). Management of Information Technology
Projects
Wheeler, Evan (2011). Security Risk Management : Building an
Information Security Risk Management Program from the Ground Up
Supplementary Video
https://www.youtube.com/watch?v=5rx8Q4x4-qI&t=6s
https://www.youtube.com/watch?v=WQe-p2F3Kcg
https://www.youtube.com/watch?v=mFxejM58wyM
Suggested Reading
SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
Database management system. In Encyclopedia Britannica, Retrieved
from http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.aspx
SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
_____________________________________________________________________________________
14
MODULE OF INSTRUCTION
Tutorialspoint.com
oracle.com
apex.oracle.com
Sets Operators
Welcome to the 10th and last module of this course Database
Management System 1! For this lesson, it covers the different type
of sets operators that is used to retrieve sets of data from 2 or more
table. Sample and demonstration in this lesson uses the AUTHORS
and BORROWER table as shown below:
Table 1.0 Authors
Sets All set operators either IN ANY or ALL have equal precedence.
When a SELECT statement have multiple set operators, the Oracle
server evaluates the operator from left / top to right / bottom – that is
if there is no parentheses. In order to change the order of evaluation
you may use a parentheses, this is added in the query in order to
specify the order of evaluation in the queries that use the
INTERSECT operator with other set operators.
Set Operators
Set operators combine the results of two or more component
queries into one result. Queries containing set operators are
called compound queries.
_____________________________________________________________________________________
2
MODULE OF INSTRUCTION
Set Operator Guidelines
• The expressions in the SELECT lists must match in number.
• The data type of each column in the second query must match
the data type of its corresponding column in the first query.
• Parentheses can be used to alter the sequence of execution.
• ORDER BY clause can appear only at the very end of the
statement.
Union Operator
The UNION operator returns all rows that are selected by either
query. Use the UNION operator to return all rows from multiple
tables and eliminate any duplicate rows.
Guidelines
• The number of columns being selected must be the same.
• The data types of the columns being selected must be in the
same data type group (such as numeric or character).
• The names of the columns need not be identical.
• UNION operates over all of the columns being selected.
• The NULL values or blank data in the report are not ignored
during duplicate checking.
By default, the output is sorted in ascending order of the columns of
the SELECT clause.
Authors Borrower
Example: Retrieve the record on both tables with the same values
after eliminating duplicated data.
SELECT BOOK, AC_NO
FROM AUTHORS
UNION
SELECT BOOK, AC_NO
FROM BORROWER;
Output:
Explanation:
The UNION operator eliminates any duplicate records. If
records that occur in both the AUTHORS and the BORROWER
tables are identical, the records are displayed only once.
The example retrieves only the record form AUTHORS and
BORROWER table with match values.
_____________________________________________________________________________________
4
MODULE OF INSTRUCTION
Union ALL
UNION ALL operator is use to return all rows from multiple queries
or multiple tables.
Guidelines
The guidelines for UNION and UNION ALL are the same, except
for the following: unlike UNION, in UNION ALL duplicated rows
are not eliminated in the report and the output is not sorted by
default.
Authors Borrower
Example:
SELECT BOOK, AC_NO
FROM AUTHORS
UNION ALL
SELECT BOOK, AC_NO
FROM BORROWER;
Output:
Explanation:
The combination of the two tables totals to 30 rows. The
UNION ALL operator does not eliminate duplicate rows.
UNION returns all distinct rows selected by either query.
UNION ALL returns all rows selected by either query, including
all duplicates.
The examples retrieves the record form AUTHORS table (equal
to 8 values), and retrieves all record from BORROWER table
(equal to 5 rows) which make the output 13 rows.
Intersect Operator:
Use the INTERSECT operator to return all rows that are common to
multiple queries.
_____________________________________________________________________________________
6
MODULE OF INSTRUCTION
Guidelines
• The number of columns and the data types of the columns
being selected by the SELECT statements in the queries
must be identical in all the SELECT statements used in the
query. The names of the columns, however, need not be
identical.
• Reversing the order of the intersected tables does not alter
the result.
• INTERSECT does not ignore NULL values.
Authors Borrower
Example:
SELECT BOOK, AC_NO
FROM AUTHORS
INTERSECT
SELECT BOOK, AC_NO
FROM BORROWER;
Output:
Explanation:
In the example in this slide, the query returns only those records
that have the same values in the selected columns in both tables.
Minus Operator
Use the MINUS operator to return all distinct rows selected by the
first query, but not present in the second query result set (the first
SELECT statement MINUS the second SELECT statement).
Note: The number of columns must be the same and the data types
of the columns being selected by the SELECT statements in the
queries must belong to the same data type group in all the SELECT
statements used in the query. The names of the columns, however,
need not be identical.
Authors Borrower
Example:
SELECT BOOK, AC_NO
FROM AUTHORS
MINUS
SELECT BOOK, AC_NO
FROM BORROWER;
_____________________________________________________________________________________
8
MODULE OF INSTRUCTION
Output:
Explanation:
The output retrieves the record with identical values in AUTHORS
table but not present in the BORROWER table.
Lesson Summary:
In this lesson, you should have learned how to:
• Use the 4 types of SET operator
• Identify the difference between set operator
• Identify the guideline in using set operator.
Terms to Remember!
Intersect - Rows that are common to both queries
Minus - Rows in the first query that are not present in the
second query
Union - Rows from both queries after eliminating duplications
Union All - Rows from both queries including all duplications
Textbook:
References
Oracle Press (2010). Applied Oracle Security
References:
Pratt, Philip J. (2010). Database management systems
Rob, Peter & Coronel, Carlo (2009). Database Management
Systems
Supplementary Video
https://www.youtube.com/watch?v=4bglO4qJZcc
https://www.youtube.com/watch?v=O1KVzYHCKDo
https://www.youtube.com/watch?v=HKZigj4c-DQ
Suggested Reading
SQL Tutorial. In ws3schools, Retrieved from
http://www.w3schools.com/sql/default.asp
Database management system. In Encyclopedia Britannica,
Retrieved from
http://www.britannica.com/EBchecked/topic/152201/database-
management-system-DBMS.
SQL. In Encyclopedia Britannica, Retrieved from
http://www.britannica.com/EBchecked/topic/569684/SQL
Database Administration. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/Database_administration.as
px
SQL. In Encyclopedia.com, Retrieved from
http://www.encyclopedia.com/topic/SQL.aspx
Tutorialspoint.com
_____________________________________________________________________________________
10
MODULE OF INSTRUCTION
oracle.com
apex.oracle.com
• Data – is also known facts that can be recorded and that have
implicit meaning.
Introduction to Database System: Terminologies
1. Data Processing
1. Data independence
2. Efficient data access
3. Data integrity and security
4. Data administration
5. Concurrent access and crash recovery
6. Reduced application development time
Disadvantages of a DBMS
1. Danger of a Overkill
2. Complexity
3. Qualified Personnel
4. Costs
5. Lower Efficiency
Instances, Physical and Logical Schemas
• Logical schema logically describes the database design at the logical level.
Introduction to Oracle
Lesson Objective
Infrastructure Grids
Information Management
Application Development
Features of Oracle Database 11g
Manageability
High availability
Performance
Security
Information integration
Object Relational Database Management System
Transaction control
Data control language (DCL
GRANT COMMIT
REVOKE SAVEPOINT
Lesson Summary:
• Features; and
Requirements
After Choosing the correct version of Oracle Database depending on your Operating
System you will be redirected to this page.
1. Provide your valid Username/ Email and Password.
Downloading Oracle Database: Step 3
When email and Password is successfully validated you will be redirected to this
page.
1. Fill up necessary and required information in the registration form
Downloading Oracle Database: Step 4
1. Click on Continue.
2. Open your personal email for account validation
Downloading Oracle Database: Step 5
Where:
o Tbl_name - is the name of the table
o Column_lists – the lists of columns
o Expression – constraint added on each column (if any).
CREATE TABLE statement cont.
• Example:
CREATE TABLE STUDENTS
(USN_ID NUMBER (6) PRIMARY KEY,
LASTNAME VARCHAR (15) NOT NULL,
FIRSTNAME VARCHAR (15),
COURSE CHAR (4),
YR_LVL CHAR (5));
Data type
1. VARCHAR- Variable-length character data
2. CHAR - Fixed-length character data
3. NUMBER - Variable-length numeric data
4. DATE - Date and time values
5. LONG - Variable-length character data (up to 2 GB
6. CLOB - Character data (up to 4 GB)
7. RAW and LONG RAW - Raw binary data
8. BLOB - Binary data (up to 4 GB)
9. BFILE - Binary data stored in an external file (up to 4 GB)
10. ROWID - A base-64 number system representing the unique address of a row in its
table
Use the ALTER TABLE statement to:
• Add a new column after the table has been
created
• Modify an existing column definition
• Define a default value for the new column
• Drop existing column
• Rename a column
• Change table to a read-only status
Three (3) types of ALTER statement
• Example:
ALTER TABLE STUDENTS
ADD ADDRESS VARCHAR(15);
ALTER to MODIFY
Syntax:
ALTER TABLE tbl_name
MODIFY column_name [new]datatype[new](size);
Example:
ALTER TABLE STUDENTS
Example:
ALTER TABLE STUDENTS
DROP COLUMN ADDRESS;
Rename the table
To rename the table or change the existing
name of the table use the RENAME statement;
Syntax:
RENAME old_tbl_name TO new_tbl_name;
Example:
RENAME STUDENTS TO STUDYANTE;
Truncate Statement
To delete the values in the STUDENTS but leaving
it structure you may use the TRUNCATE
statement.
Syntax:
TRUNCATE TABLE tbl_name;
Example:
TRUNCATE TABLE STUDYANTE;
Dropping a table
Using DROP table statement will lose all the data in the
table and all the indexes associated in it.
Even if a rollback statement is issued the record in the
table will not be restored.
Syntax
DROP TABLE table ;
• Example:
DROP TABLE STUDYANTE;
Lesson Summary:
Syntax:
UPDATE TBL_NAME
SET COLUMN = NEW VALUES
[WHERE CONDITION];
In the syntax:
Tbl_name - Is the name of the table
set column - Is the name of the column in the table to populate
– new_value - Is the corresponding value or subquery for the column
– condition - Identifies the rows to be updated, should apply either
Comparison Condition, Logical Condition or apply a subquery.
Changing Data in a Table cont.
Example: 1 column
UPDATE STUDENTS
SET FIRSTNAME = 'MARVIN'
WHERE USN_ID=502;
• Updating two columns in one UPDATE statement.
– To update to or more columns in one UPDATE statement put a comma (,) after
each column called in the SET clause.
Example:
UPDATE STUDENTS
SET YR_LVL='IRREG', LASTNAME = 'ABIOG'
WHERE USN_ID = 505;
DELETE Statement
SELECT Statement
Lesson Objective
• Where:
o A SELECT clause, which specifies the columns to be displayed
o A FROM clause, which identifies the table containing the
columns that are listed in the SELECT clause
Select (ALL) column
• You can display all rows and column in a table
by using the SELECT followed by (*) asterisk.
• (*) is used to call/display all row in the column.
• Example:
SELECT * FROM AUTHORS
Select SPECIFIC row
• Specifically display a column by using the SELECT statement then separate
with commas.
• In the SELECT clause, specify the columns that you want to be displayed in
the order in which you want them to appear in the output.
• For example, to display the last name before the first name of author in
AUTHORS table use the following example
• Example:
SELECT LNAME, FNAME
FROM AUTHORS;
Writing SQL Statements guidelines
• SQL statements are not case sensitive, a user may use all capital letter
combination of small or uppercase.
• SQL statements can be entered on one or many lines to make the code
readable.
• Keywords cannot be split across lines or abbreviated. Example: SELECT should
be call ad SELECT not SE LECT
• Clauses are usually placed on separate lines for readability and ease of editing
and to make code readable.
• Indents should be used to make code more readable.
• Keywords typically are entered in uppercase; all other words, such as table
names and columns names are entered in lowercase but is not required.
Arithmetic Expressions
• Create expression with number and date values.
• An arithmetic expression can contain column names,
constant numeric values, and the arithmetic
operators.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
Arithmetic Expressions cont.
• Example:
SELECT LNAME, FNAME, BOOK, YR_PUB + 5
FROM AUTHORS;
Rules of Precedence
• If all operators are present, multiplication and division
occur before addition and subtraction.
• Operators of the same priority example: multiplication and
division are evaluated from left to right.
• To override the default precedence or to clarify the
statement use parentheses. ()
• Example: without the parentheses operator
SELECT LNAME, FNAME, BOOK, 10 * YR_PUB + 5
FROM AUTHORS;
Rules of Precedence cont.
• Example: with parentheses operator
SELECT LNAME, FNAME, BOOK, 10 * (YR_PUB + 5)
FROM AUTHORS;
Defining NULL values
• Null is a value that is unavailable, unassigned,
unknown, or inapplicable.
• Null is not the same as zero or a blank space.
• If a row lacks a data value for a particular column, that
value is said to be null or to contain a null.
• Example:
SELECT ID, FNAME, TYPE, YR_PUB
FROM AUTHORS;
Null values is Arithmetic Expression
• If the arithmetic expression contains a null
value, the result would also be null.
• For example, if you attempt to perform
multiplication by zero, you get an error.
However, if you multiply a number by null, the
result is a null or unknown.
• Example:
SELECT LNAME, FNAME, BOOK, 10 * YR_PUB * 5
FROM AUTHORS;
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 the alias.)
• Requires double quotation marks if it contains spaces or special
characters, or if it is case-sensitive
• Example: AS Keyword
SELECT FNAME AS NAME, LNAME AS SURNAME, YR_PUB + 5 AS YEAR
FROM AUTHORS;
A column alias: cont.
• Example: with Double Quotation Mark “”
SELECT FNAME "GIVEN NAME", LNAME "SURNAME", YR_PUB + 5
"YEAR PUBLISHED"
FROM AUTHORS;
FROM AUTHORS;
Concatenation operator:
• Links columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
• Example:
SELECT LNAME||FNAME AS "COMPLETE NAME", BOOK
FROM AUTHORS;
Literal Character Strings
• A literal is either a character, a number, or a date that is included
in the SELECT statement.
• Date and character literal values must be enclosed in single
quotation marks.
• Literal strings of free-format text can be included in the query
result and are treated the same as a column in the SELECT list.
• Each character string is output once for each row returned
• Example:
SELECT LNAME||' , '||FNAME AS "COMPLETE NAME", BOOK
FROM AUTHORS;
Literal Character Strings cont.
• Additional Example: Concatenation two
or more columns:
• Example:
SELECT LNAME||' , '||FNAME|| ' is the author of '
||BOOK as "AUTHORS RECORD"
from AUTHORS
Lesson Summary:
WHERE Statement
Lesson Objective
After completing this lesson, the student should be
able to:
• Use WHERE condition to limit the rows that are
retrieved by a query
• Where:
o A SELECT clause, which specifies the columns to be displayed
o A FROM clause, which identifies the table containing the columns that
are listed in the SELECT clause
o A WHERE is used to identify what specific condition you would like to
add in the select statement to restrict the rows returned by a query.
Where Clause cont.
• The WHERE clause can ne use to compare values in columns, add
literal character string in between the two column, arithmetic
expressions, or functions. It consists of three elements:
• Column name
• Comparison condition
• Column name, constant, or list of values
• Example:
SELECT ID, LNAME, FNAME, BOOK, TYPE
FROM AUTHORS
WHERE TYPE = 'CSIT';
Character Strings and Dates
• If the Select statements contains character strings and date
values always enclosed in single quotation marks.
• Example:
Comparison Condition:
Comparison operators are used in conditions that compare
one expression with another value or expression.
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> or != Not equal to
IN (SET) Match any of the values inside the (SET)
BETWEEN..AND Between two inclusive values
LIKE Match a character pattern
IS NULL Is a NULL value
Using Comparison Operator
Example:
SELECT LNAME, FNAME, BOOK, YR_PUB
FROM AUTHORS
WHERE YR_PUB < 1995;
Example:
SELECT LNAME, FNAME, BOOK, YR_PUB
FROM AUTHORS
WHERE YR_PUB <= 1995;
Range Conditions Using the BETWEEN Operator
Logical Operator
Lesson Objective
After completing this lesson the student should be
able to:
Define the rows returned using the three logical
condition AND, OR and NOT.
Example:
SELECT ID, FNAME, LNAME, TYPE,YR_PUB
FROM AUTHORS
WHERE YR_PUB NOT IN(2010, 1995);
Rules of Precedence
The rules of precedence determine the order in which
expressions are evaluated and calculated.
The table in the slide lists the default order of precedence.
• To override the default order of expression to be calculated
first put parentheses around the expression.
Rules of Precedence cont.
Operator Meaning
1 Arithmetic operators
2 Concatenation operator
3 Comparison conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to
7 NOT logical condition
8 AND logical condition
9 OR logical condition
Rules of Precedence
Example:
SELECT (FNAME||','||LNAME)AS NAME, BOOK, TYPE,AC_NO
FROM AUTHORS
WHERE TYPE='CSIT'
OR AC_NO LIKE'%S'
AND LNAME LIKE 'S%';
• In this example, there are two conditions:
The first condition is that the AC_NO end with capital letter S and the LNAM start with
capital letter S.
The second condition is that the TYPE is CSIT.
Therefore, the SELECT statement reads as follows:
• “Select the row if an author AC_NO ends with letter S and the LNAME starts with letter
S or the TYPE is equal to CSIT.
Rules of Precedence cont.
Example: with parentheses
SELECT (FNAME||','||LNAME)AS NAME, BOOK, TYPE,AC_NO
FROM AUTHORS
WHERE (TYPE='CSIT‘
OR AC_NO LIKE'%S')
AND LNAME LIKE 'S%';
In this example, there are two conditions:
The first condition is that the AC_NO ends with letter S or the TYPE are equal
to CSIT
The second condition is that the LNAME ends with letter S.
“Select the row if an author AC_NO ends with letter S and the LNAME starts
with letter S and the TYPE is equal to CSIT.
Lesson Summary:
Single-Row Function
Lesson Objective
Functions
MOD
Case Character
TRUNC
Manipulation Manipulation
ROUND
Case Conversion function:
Function Result
LOWER(‘Database Management database management system
System’)
UPPER(‘Database Management DATABASE MANAGEMENT
System’) SYSTEM
INITCAP(‘Database Management Database Management System
System’)
• Where:
• LOWER: Converts mixed-case or uppercase character strings to lowercase
• INITCAP: Converts the first letter of each word to uppercase and the
remaining letters to lowercase
Using Case-Conversion function
Example: UPPER
SELECT UPPER(LNAME||','||FNAME) AS "NAME"
FROM AUTHORS
WHERE LNAME LIKE'S%';
Example: lower
SELECT LOWER(LNAME||','||FNAME) AS "NAME"
FROM AUTHORS
WHERE LNAME LIKE'S%';
Example: lower
SELECT INITCAP(LNAME||','||FNAME) AS "NAME“
FROM AUTHORS
WHERE LNAME LIKE'S%';
Character Manipulation Function
Function Result
CONCAT(‘Data’,’Base’) DataBase
SUBSTR(Database System, 1, 8) Database
LENGTH(‘Database’) 8
INSTR(Database,’b’) 5
LPAD(price,6,’$’) $$$360
RPAD(proce,6,’$’) 360$$$
REPLACE(Database, ‘a’,’c’) Dctcbce
TRIM(‘D’ from ‘Database’) atabase
Character Manipulation Function cont.
• Where:
• CONCAT: Joins values together (You are limited to using two parameters with CONCAT.) instead used
|| (double bars) if it contains two or more columns and character literal string
• LPAD: Returns an expression left-padded to the length of n characters with a character expression
• RPAD: Returns an expression right-padded to the length of n characters with a character expression
• TRIM: Trims leading or trailing characters (or both) from a character string (If
trim_character or trim_source is a character literal, you must enclose it within
single quotation marks.)
Using Character Manipulation Function
Example: CONCAT
SELECT CONCAT(LNAME,FNAME), YR_PUB
FROM AUTHORS
Example: SUBSTR
SELECT LNAME, FNAME, BOOK
FROM AUTHORS
WHERE SUBSTR(BOOK,6)='ING';
Using Character Manipulation Function cont.
FROM AUTHORS
FROM AUTHORS
Example: REPLACE
SELECT REPLACE(FNAME,'A','T')
FROM AUTHORS;
Example: TRIM
SELECT BOOK, TRIM('A' FROM BOOK)
FROM AUTHORS;
Number Function
Function Result
ROUND(75. 46,1) 75.5
TRUNC(75.46,1) 75
MOD(100/3) 10
• Where:
• ROUND: Rounds value to a specified decimal
• The DUAL table is useful when you want to return a value only once
(for example, the value of a constant, pseudocolumn, or expression
that is not derived from a table with user data).
FROM DUAL;
Example: TRUNC
SELECT TRUNC(75.475,2),TRUNC(75.475,1),TRUNC(75.475,-1)
FROM DUAL;
Example: MOD
SELECT MOD(1000,300)
FROM DUAL;
Lesson Summary:
Multiple-Row Function
Lesson Objective
SELECT group_function(Column_lists),…
FROM table_name
[WHERE condition];
• Where
You can use the MAX and MIN functions for numeric,
character, and date data types.
Example:
SELECT MIN(LNAME), MAX(LNAME)
FROM AUTHORS;
Using the STDDEV and VARIANCE function
1. COUNT(*) - COUNT(*) returns the number of rows in a table that satisfy the
criteria of the SELECT statement, including duplicate rows and rows containing
null values in any of the columns. If a WHERE clause is included in the SELECT
statement, the function COUNT(*) returns the number of rows that satisfy the
condition in the WHERE clause.
FROM AUTHORS;
FROM AUTHORS
WHERE TYPE='CSIT';
FROM AUTHORS;
Using NVL Function to include NULL values
FROM AUTHORS;
FROM AUTHORS;
Creating groups of data using GROUP BY clause.
All available group functions have treated the table as one group of large
information in order to divide the table of information into smaller group
use the GROUP BY clause.
Syntax:
SELECT function_name(column), group_function(column)
FROM table_name
[WHERE condition]
[GROUP BY group_by_expr]
[ORDER BY column];
Where:
group_by_expr - Specifies the columns whose values determine the basis for grouping rows.
Guidelines in using a GROUP BY clause
• Complete SELECT
• Example:
SELECT TYPE, MIN(FNAME),MAX(ID)
FROM AUTHORS
WHERE ID BETWEEN 10 AND 15
GROUP BY TYPE
HAVING MAX(ID)>= 5
ORDER BY MAX(ID) DESC;
Nesting Group Function
Set Operators
Lesson Objective
Minus Rows in the first query that are not present in the second
query
Set Operator Guidelines
FROM AUTHORS
UNION ALL
FROM BORROWER;
Intersect Operator
Use the INTERSECT operator to return all rows that are common to
multiple queries.
Intersect Guidelines