Beruflich Dokumente
Kultur Dokumente
Filing cabinet
Copyright Oracle Corporation, 1998. All rights reserved.
I-3
I-4
Data Models
Server
Tables on disk
I-5 Copyright Oracle Corporation, 1998. All rights reserved.
assigned to composed of
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 --------------------- -------- ---------------- ----------- -------------PRESIDENT MANAGER MANAGER MANAGER SALESMAN SALESMAN 7839 7839 7839 7698 7698 7698 7698 7698 7566 7902 7566 7788 7782 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 09-DEC-82 12-JAN-83 23-JAN-82 5000 2850 2450 2975 1250 1600 1500 950 1250 3000 800 3000 1100 1300 500 1400 300
4
COMM DEPTNO ----------10 30 10 20 30 30 30 30 30 20 20 20 20 10
7839 KING 7698 BLAKE 7782 CLARK 7566 JONES 7654 MARTIN 7499 ALLEN
7844 TURNER SALESMAN 7900 JAMES 7521 WARD 7902 FORD 7369 7788 7876 SMITH SCOTT ADAMS MILLER CLERK SALESMAN ANALYST CLERK ANALYST CLERK CLERK
1
I-8
7934
Primary key
I-9
Foreign key
Primary key
I-10
Data is displayed
LOC ------------NEW YORK DALLAS CHICAGO BOSTON I-11 Copyright Oracle Corporation, 1998. All rights reserved.
SQL Statements
SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME TRUNCATE COMMIT ROLLBACK SAVEPOINT GRANT REVOKE
I-12
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*Plus commands SQL*Plus buffer
SQL buffer
Overview of SQL*Plus
Log in to SQL*Plus. Describe the table structure. Edit your SQL statement. Execute SQL from SQL*Plus. Save SQL statements to files and append SQL statements to files. Execute saved files. Load commands from file to buffer to edit.
I-18 Copyright Oracle Corporation, 1998. All rights reserved.
Logging In to SQL*Plus
From Windows environment:
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
* / +
Multiplication and division take priority over addition and subtraction. Operators of the same priority are evaluated from left to right. Parentheses are used to force prioritized evaluation and to clarify statements.
I-32 Copyright Oracle Corporation, 1998. All rights reserved.
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
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; DEPTNO --------10 30 10 20 ... 14 rows selected.
I-43
DEPTNO --------10 20 30
I-44
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
I-49
I-50
I-51
I-52
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
I-61
ENAME SAL MGR ---------- --------- --------FORD 3000 7566 SMITH 800 7902 SCOTT 3000 7566 ADAMS 1100 7788
Copyright Oracle Corporation, 1998. All rights reserved.
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.
I-76
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
I-79
I-80
I-81
I-82