Assure Ecommerce and Retail Operations Uptime with ThousandEyes
SQL- Data Base
1.
2. Course Outlines:
1. Data Base Fundamentals.
2. PL-SQL.
3. Oracle 10g Forms.
4. Oracle 10g Reports.
Mohannad Al-Hanahnah
3. Basic Definitions
• Database: A collection of related data.
• Data: Known facts that can be recorded and have an implicit
meaning.
• Mini-world: Some part of the real world about which data is
stored in a database. For example, student grades and
transcripts at a university.
• Database Management System (DBMS): A software
package/ system to facilitate the creation and maintenance
of a computerized database.
• Database System: The DBMS software together with the
data itself. Sometimes, the applications are also included.
4. Types of Databases and Database
Applications
• Numeric and Textual Databases
• Multimedia Databases
• Geographic Information Systems (GIS)
• Data Warehouses
• Real-time and Active Databases
Ex: Weather Database, Multimedia DB
Mohannad Al-Hanahnah
5. Typical DBMS Functionality
• Define a database : in terms of data types,
structures and constraints.
• Construct or Load the Database on a secondary
storage medium.
• Manipulating the database : querying, generating
reports, insertions, deletions and modifications to
its content.
• Concurrent Processing and Sharing by a set of
users and programs – yet, keeping all data valid
and consistent.
Mohannad Al-Hanahnah
6. Typical DBMS Functionality
Other features:
– Protection or Security measures to prevent
unauthorized access
– “Active” processing to take internal actions
on data
– Presentation and Visualization of data
Mohannad Al-Hanahnah
8. Example of a Database
• Part of a UNIVERSITY environment.
• Entities (Tables):
– STUDENTs
– COURSEs
– SECTIONs (of COURSEs)
– (academic) DEPARTMENTs
– INSTRUCTORs
Note: The above could be expressed in the ENTITY-
RELATIONSHIP data model.
Mohannad Al-Hanahnah
9. • Some relationships:
– SECTIONs are of specific COURSEs
– STUDENTs take SECTIONs
– COURSEs have prerequisite COURSEs
– INSTRUCTORs teach SECTIONs
– COURSEs are offered by DEPARTMENTs
– STUDENTs major in DEPARTMENTs
Note: The above could be expressed in the ENTITY-
RELATIONSHIP data model.
Mohannad Al-Hanahnah
10. Main Characteristics of the Database
Approach
• Data Abstraction: A data model is used to
hide storage details and present the users
with a conceptual view of the database.
• Support of multiple views of the data: Each
user may see a different view of the database,
which describes only the data of interest to
that user.
Mohannad Al-Hanahnah
11. • Sharing of data and multiuser transaction
processing : allowing a set of concurrent users to
retrieve and to update the database.
Concurrency control within the DBMS guarantees
that each transaction is correctly executed or
completely aborted. OLTP (Online Transaction
Processing) is a major part of database
applications.
Mohannad Al-Hanahnah
12. Database Users
Users may be divided into those who actually
use and control the content (called “Actors
on the Scene”) and those who enable the
database to be developed and the DBMS
software to be designed and implemented
(called “Workers Behind the Scene”).
Mohannad Al-Hanahnah
13. Database Users Types:
– Database administrators: responsible for authorizing
access to the database, for co-ordinating and
monitoring its use, acquiring software, and hardware
resources, controlling its use and monitoring efficiency
of operations.
– Database Designers: responsible to define the
content, the structure, the constraints, and functions
or transactions against the database. They must
communicate with the end-users and understand their
needs.
– End-users: they use the data for queries, reports and
some of them actually update the database content.
Mohannad Al-Hanahnah
14. Categories of End-users
• Casual : access database occasionally when
needed
• Parametric : they make up a large section of the
end-user population. They use previously well-
defined functions in the form of “canned
transactions” against the database. Examples are
bank-tellers or reservation clerks who do this
activity for an entire shift of operations.
Mohannad Al-Hanahnah
15. • Sophisticated : these include business analysts,
scientists, engineers, others thoroughly familiar
with the system capabilities. Many use tools in the
form of software packages that work closely with
the stored database.
• Stand-alone : mostly maintain personal databases
using ready-to-use packaged applications. An
example is a tax program user that creates his or
her own internal database.
Mohannad Al-Hanahnah
16. Advantages of Using the Database
Approach
• Controlling redundancy in data storage and in
development and maintenance efforts.
• Sharing of data among multiple users.
• Restricting unauthorized access to data.
• Providing persistent storage for program
Objects.
• Providing Storage Structures for efficient
Query Processing.
Mohannad Al-Hanahnah
17. • Providing backup and recovery services.
• Representing complex relationships among data.
• Enforcing integrity constraints on the database.
• Flexibility to change data structures: database
structure may evolve as new requirements are
defined.
• Availability of up-to-date information – very
important for on-line transaction systems such as
airline, hotel, car reservations.
• Economies of scale: by consolidating data and
applications across departments wasteful overlap of
resources and personnel can be avoided.
Mohannad Al-Hanahnah
18. Historical Development of Database
Technology
• Early Database Applications: The Hierarchical and
Network Models were introduced in mid 1960’s
and dominated during the seventies. A bulk of the
worldwide database processing still occurs using
these models.
• Relational Model based Systems: The model that
was originally introduced in 1970 was heavily
researched and experimented with in IBM and the
universities. Relational DBMS Products emerged in
the 1980’s.
Mohannad Al-Hanahnah
19. Data on the Web and E-commerce Applications:
Web contains data in HTML (Hypertext markup
language) with links among pages. This has given rise
to a new set of applications and E-commerce is using
new standards like XML (eXtended Markup
Language).
Mohannad Al-Hanahnah
20. Extending Database Capabilities
• New functionality is being added to DBMSs
in the following areas:
– Scientific Applications
– Image
– Audio and Video
Mohannad Al-Hanahnah
21. Data Models
• Data Model: A set of concepts to describe the
structure of a database, and certain constraints
that the database should obey.
• Data Model Operations: Operations for specifying
database retrievals and updates by referring to
the concepts of the data model. Operations on
the data model may include basic operations and
user-defined operations.
Mohannad Al-Hanahnah
22. Categories of data models
• Conceptual (high-level, semantic) data models:
Provide concepts that are close to the way many
users perceive data. (Also called entity-based or
object-based data models.)
• Physical (low-level, internal) data models: Provide
concepts that describe details of how data is stored in
the computer.
• Implementation (representational) data models:
Provide concepts that fall between the above two,
balancing user views with some computer storage
details.
Mohannad Al-Hanahnah
23. •Database Schema: The description of a database.
Includes descriptions of the database structure and
the constraints that should hold on the database.
•Schema Diagram: A diagrammatic display of (some
aspects of) a database schema.
Mohannad Al-Hanahnah
24. Three-Schema Architecture
• Defines DBMS schemas at three levels:
• Internal schema at the internal level to describe physical
storage structures and access paths. Typically uses a
physical data model.
• Conceptual schema at the conceptual level to describe the
structure and constraints for the whole database for a
community of users. Uses a conceptual or an
implementation data model.
• External schemas at the external level to describe the
various user views. Usually uses the same data model as
the conceptual level.
Mohannad Al-Hanahnah
25. DBMS Languages
• Data Definition Language (DDL): Used by the DBA
and database designers to specify the conceptual
schema of a database. In many DBMSs, the DDL is
also used to define internal and external schemas
(views). In some DBMSs, separate storage
definition language (SDL) and view definition
language (VDL) are used to define internal and
external schemas.
Mohannad Al-Hanahnah
26. • Data Manipulation Language (DML): Used to
specify database retrievals and updates.
• High Level or Non-procedural Languages:
e.g., SQL, are set-oriented and specify what
data to retrieve than how to retrieve. Also
called declarative languages.
Mohannad Al-Hanahnah
27. DEFINITION SUMMARY
Informal Terms Formal Terms
Table Relation
Column Attribute/Domain
Row Tuple
Values in a column Domain
Table Definition Schema of a Relation
Mohannad Al-Hanahnah
28. Relational Keys
• Primary Key
– Candidate key selected to identify records uniquely within
table.
• Alternate Keys
– Candidate keys that are not selected to be primary key.
• Foreign Key
– Column, or set of columns, within one table that matches candidate
key (primary key) of some (possibly same) table.
• Secondary key
– Key used strictly for data retrieval purposes.
– Does not necessarily yield a unique number.
Mohannad Al-Hanahnah
31. Example COMPANY Database
• The company is organized into DEPARTMENTs. Each department
has a name, number and an employee who manages the
department. We keep track of the start date of the department
manager.
• Each department controls a number of PROJECTs. Each project has
a name, number and is located at a single location.
• We store each EMPLOYEE’s social security number, address,
salary, sex, and birthdate. Each employee works for one
department but may work on several projects. We keep track of
the number of hours per week that an employee currently works
on each project. We also keep track of the direct supervisor of
each employee.
• Each employee may have a number of DEPENDENTs. For each
dependent, we keep track of their name, sex, birthdate, and
relationship to employee.
Mohannad Al-Hanahnah
35. SQL
• Main language for relational DBMSs.
• Main characteristics:
– relatively easy to learn;
– non-procedural - you specify what information
you require, rather than how to get it.
– Easy to learn, consists of standard English words
like SELECT, INSERT, and UPDATE.
• SQL is a transform-oriented language with 2 major
components:
– a DDL for defining database structure;
– a DML for retrieving and updating data.
Mohannad Al-Hanahnah
36. Objectives of SQL
• Ideally database language should let user:
– create database and table structures.
– perform basic tasks like insert, update,
delete;
– perform both simple and complex queries.
• Must perform these tasks with minimal user
effort.
Mohannad Al-Hanahnah
37. SELECT Data retrieval
INSERT
UPDATE
Data Manipulation Language (DML)
DELETE
CREATE
ALTER
DROP
RENAME Data Definition Language (DDL)
TRUNCATE
COMMIT
ROLLBACK
Transaction control
SAVEPOINT
GRANT Data Control Language (DCL)
REVOKE
Mohannad Al-Hanahnah
38. Writing SQL Commands
• SQL statement consists of reserved words and user-defined
words.
• Most components of an SQL statement are case insensitive,
except for literal character data.
• All non-numeric literals must be enclosed in single quotes (eg.
‘London’).
• All numeric literals must not be enclosed in quotes (eg. 650.00).
• SQL statements are not case sensitive.
• SQL statements can be on one or more lines.
• Keywords cannot be abbreviated or split across lines.
• Clauses are usually placed on separate lines.
• Tabs and indents are used to enhance readability.
Mohannad Al-Hanahnah
39. Logging In to SQL*Plus
– From Windows environment:
Mohannad Al-Hanahnah
40. Naming Conventions
• Must begin with a letter
• Can be 1–30 characters long
• Must contain only A–Z, a–z, 0–9, _, $, and #
• Must not duplicate the name of another object
owned by the same user
• Must not be an Oracle Server reserved word
Mohannad Al-Hanahnah
41. CREATE Command
– You must have :
• CREATE TABLE privilege
• A storage area
CREATE TABLE tableName
(columnName1 columnType(size),
.
.
columnNamen columnType(size))
– You specify:
• Table name
• Column name, column datatype, and column size
Mohannad Al-Hanahnah
42. Creating Tables
SQL> CREATE TABLE dept
2 (deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13));
Table created.
SQL> DESCRIBE dept
Name Null? Type
--------------------------- -------- ---------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
Mohannad Al-Hanahnah
43. Datatypes
Datatype Description
VARCHAR2(size) Variable-length character data
CHAR(size) Fixed-length character data
NUMBER(p,s) Variable-length numeric data
DATE Date and time values
LONG Variable-length character data
up to 2 gigabytes
CLOB Single-byte character data up to 4
gigabytes
RAW and LONG RAW Raw binary data
BLOB Binary data up to 4 gigabytes
BFILE Binary data stored in an external
file; up to 4 gigabytes
Mohannad Al-Hanahnah
44. Dropping a Table
– All data and structure in the table is deleted.
– Any pending transactions are committed.
– All indexes are dropped.
– You cannot roll back this statement.
SQL> DROP TABLE dept30;
Table dropped.
Mohannad Al-Hanahnah
45. Renaming a Table
SQL> RENAME dept2 TO deptartment
Table renamed.
Mohannad Al-Hanahnah
46. Alter a Table
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]...);
SQL> ALTER TABLE dept30
2 ADD (job VARCHAR2(9));
Table altered.
Mohannad Al-Hanahnah
47. SQL> ALTER TABLE dept2
2 MODIFY ( dname VARCHAR2(20) );
Table altered .
For changing the column size
SQL> ALTER TABLE dept2
2 DROP COLUMN REGION ;
Table altered .
For deleting column
Mohannad Al-Hanahnah
48. What Are Constraints?
– Constraints enforce rules at the table level.
– Constraints prevent the deletion of a table if there
are dependencies.
– The following constraint types are valid in Oracle:
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
Mohannad Al-Hanahnah
51. The NOT NULL Constraint
• Ensures that null values are not permitted for
the column
• Defined only at the column level
SQL> CREATE TABLE emp(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL);
Mohannad Al-Hanahnah
52. The UNIQUE Key Constraint
UNIQUE key constraint
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
• Defined at either the table level or the column level
SQL> CREATE TABLE dept(
2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE(dname));
Mohannad Al-Hanahnah
53. The PRIMARY KEY Constraint
• Entity integrity supported by PRIMARY KEY
clause.
• Defined at either the table level or the column
level
SQL> CREATE TABLE dept(
2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE (dname),
6 CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
Mohannad Al-Hanahnah
54. FOREIGN KEY & Referential Integrity
• Use FOREIGN KEY clause to define any foreign
keys in the table.
• SQL rejects any INSERT or UPDATE that attempts
to create a FK value in child table without
matching PK value in parent table.
• The action SQL takes for any UPDATE or DELETE
that attempts to update or delete a PK value in
the parent table with some matching rows in
child table is dependent upon specified
referential action.
Mohannad Al-Hanahnah
55. The FOREIGN KEY Constraint
DEPT
PRIMARY DEPTNO DNAME LOC
KEY ------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
...
EMP
EMPNO ENAME JOB ... COMM DEPTNO FOREIGN
KEY
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
... Not allowed
(DEPTNO 9 does
Insert into not exist in the
DEPT table)
7571 FORD MANAGER ... 200 9
7571 FORD MANAGER ... 200 20 Allowed
Mohannad Al-Hanahnah
56. The FOREIGN KEY Constraint
• Defined at either the table level or the column
level
SQL> CREATE TABLE emp(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL,
10 CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno)
11 REFERENCES dept (deptno));
Mohannad Al-Hanahnah
57. Data Manipulation Language
– A DML statement is executed when you:
• Add new rows to a table
• Modify existing rows in a table
• Remove existing rows from a table
Mohannad Al-Hanahnah
58. Adding a New Row to a Table
50 DEVELOPMENT DETROIT
New row
“…insert a new row
DEPT
into DEPT table…”
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS DEPT
30 SALES CHICAGO DEPTNO DNAME LOC
40 OPERATIONS BOSTON ------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 DEVELOPMENT DETROIT
Mohannad Al-Hanahnah
59. The INSERT Statement
– Add new rows to a table by using the INSERT
statement.
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
– Only one row is inserted at a time with this syntax.
SQL> INSERT INTO dept (deptno, dname, loc)
2 VALUES (50, 'DEVELOPMENT', 'DETROIT');
1 row created.
Mohannad Al-Hanahnah
60. Changing Data in a Table
EMP
EMPNO ENAME JOB ... DEPTNO
“…update a row
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30 in EMP table…”
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
EMP
EMPNO ENAME JOB ... DEPTNO
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
7782 CLARK MANAGER 20
10
7566 JONES MANAGER 20
...
Mohannad Al-Hanahnah
61. The UPDATE Statement
– Modify existing rows with the UPDATE statement.
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
– Update more than one row at a time, if required.
Mohannad Al-Hanahnah
62. Updating Rows in a Table
– Specific row or rows are modified when you
specify the WHERE clause.
SQL> UPDATE emp
2 SET deptno = 20
3 WHERE empno = 7782;
1 row updated.
– All rows in the table are modified if you omit the
WHERE clause.
SQL> UPDATE employee
2 SET deptno = 20;
14 rows updated.
Mohannad Al-Hanahnah
63. Updating Rows:
Integrity Constraint Error
SQL> UPDATE emp
2 SET deptno = 55
3 WHERE deptno = 10;
UPDATE emp
*
ERROR at line 1:
ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK)
violated - parent key not found
Mohannad Al-Hanahnah
64. Removing a Row from a Table
DEPT
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS “…delete a row
30 SALES CHICAGO
40 OPERATIONS BOSTON from DEPT table…”
50 DEVELOPMENT DETROIT
60 MIS DEPT
...
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
60 MIS
...
Mohannad Al-Hanahnah
65. The DELETE Statement
• You can remove existing rows from a table by using the DELETE
statement.
DELETE [FROM] table
[WHERE condition];
– Specific rows are deleted when you specify the WHERE
clause.
SQL> DELETE FROM department
2 WHERE dname = 'DEVELOPMENT';
1 row deleted.
– All rows in the table are deleted if you omit the WHERE
clause
SQL> DELETE FROM department;
4 rows deleted.
Mohannad Al-Hanahnah
66. Deleting Rows:
Integrity Constraint Error
SQL> DELETE FROM dept
2 WHERE deptno = 10;
DELETE FROM dept
*
ERROR at line 1:
ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK)
violated - child record found
Mohannad Al-Hanahnah
67. Basic SELECT Statement
– SELECT identifies what columns.
– FROM identifies which table.
SELECT [DISTINCT] {*, column [alias],...}
FROM table;
SQL> SELECT *
2 FROM dept;
DEPTNO DNAME LOC
--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS Mohannad Al-Hanahnah
BOSTON
68. Selecting Specific Columns
SQL> SELECT deptno, loc
2 FROM dept;
DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON
SQL> select ename, 12*sal+comm
2 from emp
3 WHERE ename='KING';
ENAME 12*SAL+COMM
---------- -----------
KING Mohannad Al-Hanahnah
69. Using Column Aliases
SQL> SELECT ename AS name, sal salary
2 FROM emp;
NAME SALARY
------------- ---------
...
SQL> SELECT ename "Name",
2 sal*12 "Annual Salary"
3 FROM emp;
Name Annual Salary
------------- -------------
...
Mohannad Al-Hanahnah
70. Duplicate Rows
• The default display of queries is all rows, including duplicate
rows.
SQL> SELECT deptno
2 FROM emp;
DEPTNO
---------
10
30
10
20
...
14 rows selected.
Mohannad Al-Hanahnah
71. Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT
keyword in the SELECT clause.
SQL> SELECT DISTINCT deptno
2 FROM emp;
DEPTNO
---------
10
20
30
Mohannad Al-Hanahnah
72. Limiting Rows Selected
– Restrict the rows returned by using the WHERE clause.
SELECT [DISTINCT] {*| column [alias], ...}
FROM table
[WHERE condition(s)];
– The WHERE clause follows the FROM clause.
SQL> SELECT ename, job, deptno
2 FROM emp
3 WHERE job='CLERK';
ENAME JOB DEPTNO
---------- --------- ---------
JAMES CLERK 30
SMITH CLERK 20
ADAMS CLERK 20
Mohannad Al-Hanahnah
MILLER CLERK 10
73. Comparison Operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
SQL> SELECT ename, sal, comm
2 FROM emp
3 WHERE sal<=comm;
ENAME SAL COMM
---------- --------- ---------
MARTIN 1250 Mohannad Al-Hanahnah
1400
74. Other Comparison Operators
Operator Meaning
BETWEEN Between two values (inclusive)
...AND...
IN(list) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
SQL> SELECT empno, ename, sal, mgr
2 FROM emp
3 WHERE mgr IN (7902, 7566, 7788);
EMPNO ENAME SAL MGR
--------- ---------- --------- ---------
7902 FORD 3000 7566
7369 SMITH 800 7902
7788 SCOTT 3000 7566
Mohannad Al-Hanahnah
7876 ADAMS 1100 7788
75. Using the BETWEEN Operator
SQL> SELECT ename, sal
2 FROM emp
3 WHERE sal BETWEEN 1000 AND 1500;
ENAME SAL
---------- ---------
MARTIN 1250 Lower Higher
TURNER 1500 limit limit
WARD 1250
ADAMS 1100
MILLER 1300
Using the IS NULL Operator
SQL> SELECT ename, mgr
2 FROM emp
3 WHERE mgr IS NULL;
ENAME MGR
---------- ---------
KING
Mohannad Al-Hanahnah
76. Using the LIKE Operator
SQL> SELECT ename
2 FROM emp
3 WHERE ename LIKE '_A%';
ENAME
----------
MARTIN
JAMES
WARD
SQL> SELECT ename
2 FROM emp
3 WHERE ename LIKE 'S%';
Mohannad Al-Hanahnah
77. Logical Operators
Operator Meaning
AND Returns TRUE if both component
conditions are TRUE
OR Returns TRUE if either component
condition is TRUE
NOT Returns TRUE if the following condition is
FALSE
SQL> SELECT empno, ename, job, sal
2 FROM emp
3 WHERE sal>=1100
4 AND job='CLERK';
EMPNO ENAME JOB SAL
--------- ---------- --------- ---------
7876 ADAMS CLERK 1100
7934 MILLER CLERK
Mohannad Al-Hanahnah 1300
78. Using the OR Operator
SQL> SELECT empno, ename, job, sal
2 FROM emp
3 WHERE sal>=1100
4 OR job='CLERK';
EMPNO ENAME JOB SAL
--------- ---------- --------- ---------
7839 KING PRESIDENT 5000
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
...
7900 JAMES CLERK 950
...
14 rows selected.
Mohannad Al-Hanahnah
79. Using the NOT Operator
SQL> SELECT ename, job
2 FROM emp
3 WHERE job NOT IN ('CLERK','MANAGER','ANALYST');
ENAME JOB
---------- ---------
KING PRESIDENT
MARTIN SALESMAN
ALLEN SALESMAN
TURNER SALESMAN
WARD SALESMAN
Mohannad Al-Hanahnah
80. ORDER BY Clause
– Sort rows with the ORDER BY clause
• ASC: ascending order, default
• DESC: descending order
– The ORDER BY clause comes last in the SELECT statement.
SQL> SELECT ename, job, deptno, hiredate
2 FROM emp
3 ORDER BY hiredate;
ENAME JOB DEPTNO HIREDATE
---------- --------- --------- ---------
SMITH CLERK 20 17-DEC-80
ALLEN SALESMAN 30 20-FEB-81
...
14 rows selected.
Mohannad Al-Hanahnah
81. ORDER BY Clause
– Sort rows with the ORDER BY clause
• ASC: ascending order, default
• DESC: descending order
– The ORDER BY clause comes last in the SELECT statement.
SQL> SELECT ename, job, deptno, hiredate
2 FROM emp
3 ORDER BY hiredate;
ENAME JOB DEPTNO HIREDATE
---------- --------- --------- ---------
SMITH CLERK 20 17-DEC-80
ALLEN SALESMAN 30 20-FEB-81
...
14 rows selected.
Mohannad Al-Hanahnah
82. Sorting in Descending Order
SQL> SELECT ename, job, deptno, hiredate
2 FROM emp
3 ORDER BY hiredate DESC;
ENAME JOB DEPTNO HIREDATE
---------- --------- --------- ---------
ADAMS CLERK 20 12-JAN-83
SCOTT ANALYST 20 09-DEC-82
MILLER CLERK 10 23-JAN-82
JAMES CLERK 30 03-DEC-81
FORD ANALYST 20 03-DEC-81
KING PRESIDENT 10 17-NOV-81
MARTIN SALESMAN 30 28-SEP-81
...
14 rows selected.
Mohannad Al-Hanahnah
83. Obtaining Data from Multiple Tables
EMP DEPT
EMPNO ENAME ... DEPTNO DEPTNO DNAME LOC
------ ----- ... ------ ------ ---------- --------
7839 KING ... 10 10 ACCOUNTING NEW YORK
7698 BLAKE ... 30 20 RESEARCH DALLAS
... 30 SALES CHICAGO
7934 MILLER ... 10 40 OPERATIONS BOSTON
EMPNO DEPTNO LOC
----- ------- --------
7839 10 NEW YORK
7698 30 CHICAGO
7782 10 NEW YORK
7566 20 DALLAS
7654 30 CHICAGO
7499 30 CHICAGO
...
14 rows selected.
Mohannad Al-Hanahnah
84. What Is a Join?
• Use a join to query data from more than one
table.
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
– Write the join condition in the WHERE clause.
– Prefix the column name with the table name
when the same column name appears in more
than one table.
Mohannad Al-Hanahnah
85. Types of Join?
1. Equijoin. الربط بالتساوي
2. Non-Equijoin. الربط بعدم التساوي
3. Outer Join. الربط الخارجي
4. Self Join. الربط الداخلي في نفس الجدول
We can do join through SELECT statement and using
WHERE.
Mohannad Al-Hanahnah
86. What Is an Equijoin?
EMP DEPT
EMPNO ENAME DEPTNO DEPTNO DNAME LOC
------ ------- ------- ------- ---------- --------
7839 KING 10 10 ACCOUNTING NEW YORK
7698 BLAKE 30 30 SALES CHICAGO
7782 CLARK 10 10 ACCOUNTING NEW YORK
7566 JONES 20 20 RESEARCH DALLAS
7654 MARTIN 30 30 SALES CHICAGO
7499 ALLEN 30 30 SALES CHICAGO
7844 TURNER 30 30 SALES CHICAGO
7900 JAMES 30 30 SALES CHICAGO
7521 WARD 30 30 SALES CHICAGO
7902 FORD 20 20 RESEARCH DALLAS
7369 SMITH 20 20 RESEARCH DALLAS
... ...
14 rows selected. 14 rows selected.
Foreign key Primary key
Mohannad Al-Hanahnah
87. Retrieving Records
with Equijoins
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno;
EMPNO ENAME DEPTNO DEPTNO LOC
----- ------ ------ ------ ---------
7839 KING 10 10 NEW YORK
7698 BLAKE 30 30 CHICAGO
7782 CLARK 10 10 NEW YORK
7566 JONES 20 20 DALLAS
...
14 rows selected.
Mohannad Al-Hanahnah
88. Using Table Aliases
• Simplify queries by using table aliases.
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno;
SQL> SELECT e.empno, e.ename, e.deptno,
2 d.deptno, d.loc
3 FROM emp e, dept d
4 WHERE e.deptno=d.deptno;
Mohannad Al-Hanahnah
89. Non-Equijoins
EMP SALGRADE
EMPNO ENAME SAL GRADE LOSAL HISAL
------ ------- ------ ----- ----- ------
7839 KING 5000 1 700 1200
7698 BLAKE 2850 2 1201 1400
7782 CLARK 2450 3 1401 2000
7566 JONES 2975 4 2001 3000
7654 MARTIN 1250 5 3001 9999
7499 ALLEN 1600
7844 TURNER 1500
7900 JAMES 950
... “salary in the EMP
14 rows selected. table is between
low salary and high
salary in the SALGRADE
table”
Mohannad Al-Hanahnah
90. Retrieving Records
with Non-Equijoins
SQL> SELECT e.ename, e.sal, s.grade
2 FROM emp e, salgrade s
3 WHERE e.sal
4 BETWEEN s.losal AND s.hisal;
ENAME SAL GRADE
---------- --------- ---------
JAMES 950 1
SMITH 800 1
ADAMS 1100 1
...
14 rows selected.
Mohannad Al-Hanahnah
91. Outer Joins
EMP DEPT
ENAME DEPTNO DEPTNO DNAME
----- ------ ------ ----------
KING 10 10 ACCOUNTING
BLAKE 30 30 SALES
CLARK 10 10 ACCOUNTING
JONES 20 20 RESEARCH
... ...
40 OPERATIONS
No employee in the
OPERATIONS department
Mohannad Al-Hanahnah
92. Outer Joins
– You use an outer join to also see rows that do not
usually meet the join condition.
– Outer join operator is the plus sign (+).
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column(+) = table2.column;
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column = table2.column(+);
Mohannad Al-Hanahnah
93. Using Outer Joins
SQL> SELECT e.ename, d.deptno, d.dname
2 FROM emp e, dept d
3 WHERE e.deptno(+) = d.deptno
4 ORDER BY e.deptno;
ENAME DEPTNO DNAME
---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.
Mohannad Al-Hanahnah
94. Self Joins
EMP (WORKER) EMP (MANAGER)
EMPNO ENAME MGR EMPNO ENAME
----- ------ ---- ----- --------
7839 KING
7698 BLAKE 7839 7839 KING
7782 CLARK 7839 7839 KING
7566 JONES 7839 7839 KING
7654 MARTIN 7698 7698 BLAKE
7499 ALLEN 7698 7698 BLAKE
“MGR in the WORKER table is equal to EMPNO in the
MANAGER table”
Mohannad Al-Hanahnah
95. Joining a Table to Itself
SQL> SELECT worker.ename||' works for '||manager.ename
2 FROM emp worker, emp manager
3 WHERE worker.mgr = manager.empno;
WORKER.ENAME||'WORKSFOR'||MANAG
-------------------------------
BLAKE works for KING
CLARK works for KING
JONES works for KING
MARTIN works for BLAKE
...
13 rows selected.
Mohannad Al-Hanahnah
96. What Is a View?
EMP Table
EMPNO ENAME
EMPNOENAME JOB
JOB MGR HIREDATE
MGR HIREDATE SAL SAL COMM
COMM DEPTNO DEPTNO
------------- --------- --------- -------------- -------
----- ------- --------- --------- ------ ----- ----- -------
7839 KING
7839 KING PRESIDENT
PRESIDENT 17-NOV-81
17-NOV-81 5000 5000 10 10
7698 BLAKE
7782 CLARK MANAGER7839 09-JUN-81
MANAGER 7839 01-MAY-81
1500 2850
300 10 30
7782 CLARK
7934 MILLER MANAGER7782 23-JAN-82
CLERK 7839 09-JUN-81
1300 2450 10 10
7566 JONES
7566 JONES MANAGER7839 02-APR-81
MANAGER 7839 02-APR-81
2975 2975 20 20
EMPVU10 View ANALYST 7566 09-DEC-82 3000 1250
7654 MARTIN SALESMAN
7788 SCOTT 7698 28-SEP-81 1400
20 30
7499 ALLEN CLERK
7876 ADAMS SALESMAN 7698 20-FEB-81 1600
7788 12-JAN-83 1100 300
20 30
EMPNO 7844 TURNER SALESMAN 17-DEC-80 800
ENAME JOB 7698 08-SEP-81
------ 7369 SMITH CLERK ----------- 1500
-------- 7902 20 0 30
7900 JAMES ANALYST
7902 FORD CLERK 7566 03-DEC-81
7698 03-DEC-81
3000 950 20 30
7839 7698 BLAKE MANAGERPRESIDENT 2850 1250
KING
7521 WARD SALESMAN 7698 22-FEB-81
7839 01-MAY-81 500
30 30
7782 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
CLARK ANALYST 7566 03-DEC-81 3000
7902 FORD MANAGER 30 20
7934 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
MILLER CLERK
7369 SMITH
CLERK 17-DEC-80 800
7902 30 20
7788 SCOTT
7844 TURNER ANALYST 7566 09-DEC-82
SALESMAN 7698 08-SEP-81 1500 3000
0 30 20
7876 ADAMS
7900 JAMES CLERK
CLERK 7788 12-JAN-83
7698 03-DEC-81 950 1100 30 20
7934 MILLER SALESMAN
7521 WARD CLERK 7782 23-JAN-82
7698 22-FEB-81 1250 1300
500 30 10
Mohannad Al-Hanahnah
97. Why Use Views?
– To restrict database access
– To make complex queries easy
– To allow data independence
– To present different views of the same data
Mohannad Al-Hanahnah
98. Creating a View
– You embed a subquery within the CREATE VIEW
statement.
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY]
– The subquery can contain complex SELECT syntax.
– The subquery cannot contain an ORDER BY clause.
Mohannad Al-Hanahnah
99. Creating a View
– Create a view, EMPVU10, that contains details of
employees in department 10.
SQL> CREATE VIEW empvu10
2 AS SELECT empno, ename, job
3 FROM emp
4 WHERE deptno = 10;
View created.
SQL> DESCRIBE empvu10
Mohannad Al-Hanahnah
100. Creating a View
– Create a view by using column aliases in the
subquery.
SQL> CREATE VIEW salvu30
2 AS SELECT empno EMPLOYEE_NUMBER, ename NAME,
3 sal SALARY
4 FROM emp
5 WHERE deptno = 30;
View created.
– Select the columns from this view by the given
alias names.
Mohannad Al-Hanahnah
101. Retrieving Data from a View
SQL> SELECT *
2 FROM salvu30;
EMPLOYEE_NUMBER NAME SALARY
--------------- ---------- ---------
7698 BLAKE 2850
7654 MARTIN 1250
7499 ALLEN 1600
7844 TURNER 1500
7900 JAMES 950
7521 WARD 1250
6 rows selected.
Mohannad Al-Hanahnah
102. Removing a View
• Remove a view without losing data because a
view is based on underlying tables in the
database.
DROP VIEW view;
SQL> DROP VIEW empvu10;
View dropped.
Mohannad Al-Hanahnah
104. Testing a Form: Starting OC4J
– On NT, run batch file to start
OC4J: startinst.bat.
– OC4J starts in DOS window:
• Minimize window
• Closing window aborts OC4J
– Run batch file to stop OC4J: stopinst.bat.
– Start menu: Programs > Oracle Developer Suite –
iDS10g > Forms Developer > Start [Shutdown] OC4J
Instance.
Mohannad Al-Hanahnah
105. Running a Form
Oracle Forms Services deployment:
Browser URL
Java Applet
Mohannad Al-Hanahnah
106. Running a Form: Browser
How do I
access this application?
http://summit.com:8889/forms90/f90servlet?form=customers.fmx&userid=
http://summit.com:8889/forms90/f90servlet
?form=customers.fmx&userid=username/password@database
&buffer_records=NO&debug_messages=NO&array=YES
&query_only=NO
Mohannad Al-Hanahnah
107. Starting a Run-Time Session
Client Tier Middle Tier: Application Server
1 Web Browser Web Server
Static HTML files
URLhttp://summit.com:8889/forms90/f90 2 OC4J
or HTTP Server
Forms Services
3 Forms Servlet
Forms Listener Servlet
Forms Runtime Engine
Forms Application Executables
FMX files MMX files PLX files DB
Mohannad Al-Hanahnah
108. Starting a Run-Time Session
Client Tier Middle Tier: Application Server
Web Browser Web Server
Static HTML files
URLhttp://summit.com:8889/forms90/f90 OC4J
4 or HTTP Server
Forms Services
Forms Servlet
Applet started 5 Forms Listener Servlet 6
Forms Runtime Engine
Forms Application Executables
FMX files MMX files PLX files DB
Mohannad Al-Hanahnah
109. Starting a Run-Time Session
Client Tier Middle Tier: Application Server
Web Browser Web Server
Static HTML files
URLhttp://summit.com:8889/forms90/f90 OC4J
7 or HTTP Server
Forms Services
Forms Servlet
8
Forms Listener Servlet 8
Forms Runtime Engine
Forms Application Executables
FMX files MMX files PLX files DB
Mohannad Al-Hanahnah
110. What You See at Run Time
3
1 4
5 2
Mohannad Al-Hanahnah