Вы находитесь на странице: 1из 53

 Synonyms: Provide alternative names for database

 Database links: Provide links between distributed
National Institute of Electronics

Limiting the Rows Selected

• Restrict the rows returned by using the WHERE

SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];

• The WHERE clause follows the FROM clause.

5 Lecture No 3: Restricting and Sorting Data

National Institute of Electronics

Using the WHERE Clause

SELECT employee_id, last_name, job_id, department_id

FROM employees
WHERE department_id = 100 ;

6 Lecture No 3: Restricting and Sorting Data

Databases and Instances

 In Oracle, the term database refers to the physical storage of

information, and the term instance refers to the software
executing on the server that provides access to the
information in the database.
 The instance runs on the computer or server; the
database is stored on the disks attached to the server.
 The database is physical: it consists of files stored on disks.
 The instance is logical: it consists of in-memory structures
and processes on the server.
Databases and Instances
 An instance can be part of one and only one database,
although multiple instances can be part of the same
 In Oracle terms, if the instance is up, data can
flow in and out of the database. The physical state of the
database is changing.
 If the instance is down, users cannot access the database
even though it still exists physically.
Using the AVG and SUM Functions
Two Types of SQL Functions
Single-Row Functions
 Manipulate data items.
 Accept arguments and return one value.
 Return one result per row.
 May modify the data type.
 Can be nested.
Oracle uses an area of shared memory called the System Global Area (SGA)
and a private memory area for each process called the Program Global Area
System global area: The SGA is an area of shared memory that is used to store
data and control information for one Oracle instance. The SGA is allocated
when the Oracle instance starts and deallocated when the Oracle instance
shuts down. An the System Global Area is composed of multiple areas. These
include a database buffer cache, a shared pool, and a redo log buffer and also
possibly a Java pool, a large pool, and a Streams pool.
Program global area :The PGA is an area of shared memory that is used to
storedata and control information for the Oracle server processes. The size
and content of the
PGA depends on the Oracle server options installed.
 The System Global Area SGA, can also possibly contain a
Java pool, a large pool, and a Streams pool.
Conditional Expressions
 Provide the use of IF-THEN-ELSE logic within a
SQL statement.
 Use two methods:
 CASE expression
 DECODE function.
CASE Expression
Using the CASE Expression
DECODE Function
Using the DECODE Function
• The condition that determines whether the
action should be executed. The condition is
• optional but, if specified, the action will be
executed only if the condition is true.
Comparison Conditions
Using Comparison Conditions
Using the BETWEEN Condition
Using the IN Condition
Using the LIKE Condition

 Use the LIKE condition to perform wildcard

searches of valid search string values.
 Search conditions can contain either literal
characters or numbers:
 % denotes zero or many characters.
 _denotes one character.
Using the LIKE Condition
Using the NULL Conditions
Adding a New Row to a Table
INSERT Statement Syntax
Inserting New Rows
 Insert a new row containing values for each
 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 in single
quotation marks.
Inserting Rows with Null Values
Inserting Special Values
Inserting Specific Date Values
Copying Rows from Another Table
Changing Data in a Table
UPDATE Statement Syntax
Updating Rows in a Table
Updating Two Columns with a Subquery
Updating Rows Based on Another Table
Removing a Row from a Table
Using a Subquery in an INSERT
Using a Subquery in an INSERT
Foreign KEYS
 Each row of data in a table is uniquely
identified by a Primary Key (PK).
 You can logically relate data from multiple
tables using Foreign Keys (FK).
• To represent the relationship between two
tables or relations. This is accomplished
through the use of foreign keys.
• A foreign key is an attribute (possibly
composite) in a relation that serves as the
primary key of another relation.
consider the relations EMPLOYEE1 and DEPARTMENT:

The attribute DeptName is a foreign key in EMPLOYEE1.

It allows a user to associate any employee with the
department to which he or she is assigned.
Some authors emphasize the fact that anan attribute is
a foreign key by using a dashed underline, like this:
attribute is a foreign key by using a dashed underline,
like this:
DELETE Statement
Deleting Rows from a Table
Deleting Rows Based
on Another Table
TRUNCATE Statement
 Removes all rows from a table, leaving the table
empty and the table structure intact
 Is a data definition language (DDL) statement
rather than a DML statement; cannot easily be
 Syntax:
Advantages of COMMIT
and ROLLBACK Statements
 With COMMIT and ROLLBACK statements, you
 Ensure data consistency.
 Preview data changes before making changes
 Group logically related operations.
Rolling Back Changes to a Marker

 Create a marker in a current transaction by using

the SAVEPOINT statement.
 Roll back to that marker by using the ROLLBACK
TO SAVEPOINT statement.
Implicit Transaction Processing
 An automatic commit occurs under the following
 DDL statement is issued
 DCL statement is issued
 Normal exit from iSQL*Plus, without explicitly
issuing COMMIT or ROLLBACK statements
 An automatic rollback occurs under an
abnormal termination of iSQL*Plus or a system
State of the Data
 The previous state of the data can be recovered.
 The current user can review the results of the
DML operations by using the SELECT statement.
 Other users cannot view the results of the DML
statements by the current user.
 The affected rows are locked; other users cannot
change the data in the affected rows.
State of the Data After COMMIT
 Data changes are made permanent in the
 The previous state of the data is permanently
 All users can view the results.
 Locks on the affected rows are released; those
rows are available for other users to manipulate.
 All savepoints are erased.
Committing Data
State of the Data After ROLLBACK
State of the Data After ROLLBACK