Professional Documents
Culture Documents
Filing cabinet
Copyright Oracle Corporation, 1998. All rights reserved.
I-3
BLAKE CLARK
JONES
MANAGER MANAGER
30
40
OPERATIONS
BOSTON
Data Models
Server
Tables on disk
I-5
Copyright Oracle Corporation, 1998. All rights reserved.
assigned to
Scenario . . . Assign one or more employees to a department . . . . . . Some departments do not yet have assigned employees . . .
I-6
Copyright Oracle Corporation, 1998. All rights reserved.
assigned to
composed of
Unique Identifier (UID) Primary marked with # Secondary marked with (#)
I-7
Copyright Oracle Corporation, 1998. All rights reserved.
3
MGR HIREDATE SAL
4
COMM DEPTNO ----------10 30 10 20 1400 300 30 30 30 30 500 30 20 20
------------- ------------ --------------------- -------- ---------------- ----------- -------------7839 KING 7698 BLAKE 7782 CLARK 7566 JONES 7654 MARTIN 7499 ALLEN PRESIDENT MANAGER MANAGER MANAGER SALESMAN SALESMAN 7839 7839 7839 7698 7698 7698 7698 7698 7566 7902 17-NOV-81 01-MAY-81 09-JUN-81 02-APR-81 28-SEP-81 20-FEB-81 08-SEP-81 03-DEC-81 22-FEB-81 03-DEC-81 17-DEC-80 5000 2850 2450 2975 1250 1600 1500 950 1250 3000 800
7844 TURNER SALESMAN 7900 JAMES 7521 WARD 7902 FORD 7369 SMITH CLERK SALESMAN ANALYST CLERK
7788
7876
SCOTT
ADAMS MILLER
ANALYST
CLERK CLERK
7566
7788 7782
09-DEC-82
12-JAN-83 23-JAN-82
3000
1100 1300
20
20 10
1
I-8
7934
You can logically relate data from multiple tables using foreign keys (FK).
Table Name: EMP
EMPNO DEPTNO 7839 10 7698 ENAME KING BLAKE JOB PRESIDENT MANAGER 30
7782 10 7566
CLARK
JONES
MANAGER
MANAGER 20
Primary key
I-9
Foreign key
Primary key
I-10
Database
Data is displayed
LOC ------------NEW YORK DALLAS CHICAGO BOSTON
I-11
SQL Statements
SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME TRUNCATE COMMIT ROLLBACK SAVEPOINT GRANT REVOKE
I-12
Data retrieval
Transaction control
What Is PL/SQL?
PL/SQL is an extension to SQL with design features of programming languages. Data manipulation and query statements of SQL are included within procedural units of code.
I-13
PL/SQL Environment
PL/SQL engine PL/SQL block PL/SQL block PL/SQL SQL Procedural Statement Executor
I-14
Benefits of PL/SQL
It is portable. You can declare identifiers. You can program with procedural language control structures. It can handle errors.
I-15
Server
SQL*Plus
Query Results
Buffer
SQL scripts
I-16
Copyright Oracle Corporation, 1998. All rights reserved.
SQL*Plus An environment Oracle proprietary Keywords can be abbreviated Commands do not allow manipulation of values in the database
SQL buffer
SQL*Plus commands
SQL*Plus buffer
Overview of SQL*Plus
Log in to SQL*Plus.
Logging In to SQL*Plus
From Windows environment:
1400 300 0
DEPT
DEPTNO --------10 20 30 40
7521 DNAMEWARD 7902 FORD -------------7369 SMITH ACCOUNTING 7788 SCOTT RESEARCH 7876 SALESADAMS 7934 MILLER OPERATIONS
SALESMAN LOC ANALYST ---------CLERK NEW YORK ANALYST DALLAS CLERK CHICAGO CLERK BOSTON
1250 500 30 SALGRADE 3000 20 800 20 GRADE LOSAL HISAL 3000 --------- --------20 --------1100 1 20 700 1200 1300 2 10 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999
I-20
I-21
I-22
Table 1
Join
Table 1
Table 1
I-24
Table 2
I-25
I-27
I-28
I-29
Arithmetic Expressions
Create expressions on NUMBER and DATE data types by using arithmetic operators.
Operator + Description Add Subtract
*
/
Multiply
Divide
I-30
I-31
Operator Precedence
* / +
Operator Precedence
SQL> SELECT ename, sal, 12*sal+100 2 FROM emp; ENAME SAL 12*SAL+100 ---------- --------- ---------KING 5000 60100 BLAKE 2850 34300 CLARK 2450 29500 JONES 2975 35800 MARTIN 1250 15100 ALLEN 1600 19300 ... 14 rows selected.
I-33
Using Parentheses
SQL> SELECT ename, sal, 12*(sal+100) 2 FROM emp; ENAME SAL 12*(SAL+100) ---------- --------- ----------KING 5000 61200 BLAKE 2850 35400 CLARK 2450 30600 JONES 2975 36900 MARTIN 1250 16200 ... 14 rows selected.
I-34
ENAME JOB COMM ---------- --------- --------KING PRESIDENT BLAKE MANAGER ... TURNER SALESMAN 0 ... 14 rows selected.
I-35
Copyright Oracle Corporation, 1998. All rights reserved.
I-36
...
I-38
Copyright Oracle Corporation, 1998. All rights reserved.
Concatenation Operator
Concatenates columns or character strings to other columns Is represented by two vertical bars (||) Creates a resultant column that is a character expression
I-39
Employees ------------------KINGPRESIDENT BLAKEMANAGER CLARKMANAGER JONESMANAGER MARTINSALESMAN ALLENSALESMAN ... 14 rows selected.
I-40
Copyright Oracle Corporation, 1998. All rights reserved.
I-41
I-42
Duplicate Rows
The default display of queries is all rows, including duplicate rows.
SQL> SELECT deptno 2 FROM emp;
I-43
DEPTNO --------10 20 30
I-44
L[IST] m n
R[UN] n
n text
0 text
I-46
Copyright Oracle Corporation, 1998. All rights reserved.
EDIT filename
SPOOL filename EXIT
I-47
Copyright Oracle Corporation, 1998. All rights reserved.
Practice Overview
Selecting all data from different tables. Describing the structure of tables. Performing arithmetic calculations and specifying column names. Using SQL*Plus editor.
I-48
EMP
EMPNO ENAME JOB ... DEPTNO 10 10 10
I-54
I-55
I-56
I-57
Comparison Operators
Operator
= > >= < <= <>
Meaning
Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
I-58
I-59
I-60
ENAME SAL ---------- --------MARTIN 1250 TURNER 1500 WARD 1250 ADAMS 1100 MILLER 1300
Lower limit
Higher limit
I-61
ENAME SAL MGR ---------- --------- --------FORD 3000 7566 SMITH 800 7902 SCOTT 3000 7566 ADAMS 1100 7788
I-62
I-63
I-65
Logical Operators
Operator AND OR Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following condition is FALSE
NOT
I-66
I-67
EMPNO ENAME JOB SAL --------- ---------- --------- --------7839 7698 7782 7566 7654 KING BLAKE CLARK JONES MARTIN PRESIDENT MANAGER MANAGER MANAGER SALESMAN 5000 2850 2450 2975 1250
I-69
Rules of Precedence
Order Evaluated 1 2 3 4 Operator All comparison operators NOT AND OR
Rules of Precedence
SQL> 2 3 4 5 SELECT FROM WHERE OR AND ename, job, sal emp job='SALESMAN' job='PRESIDENT' sal>1500;
JOB SAL --------- --------PRESIDENT 5000 SALESMAN 1250 SALESMAN 1600 SALESMAN 1500 SALESMAN 1250
I-71
Rules of Precedence
Use parentheses to force priority.
SQL> 2 3 4 5 SELECT FROM WHERE OR AND ename, job, sal emp (job='SALESMAN' job='PRESIDENT') sal>1500;
I-72
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.
I-73
Copyright Oracle Corporation, 1998. All rights reserved.
Summary
SELECT FROM [WHERE [ORDER BY [DISTINCT] {*, column [alias], ...} table condition(s)] {column, expr, alias} [ASC|DESC]];
I-77
Practice Overview
Selecting data and changing the order of rows displayed Restricting rows by using the WHERE clause Using the double-quotation-marks in column aliases
I-78