Sie sind auf Seite 1von 64

SQL and PL/SQL Essentials

ORACLE Oak Ridge Automated Computer And Logical Engine

It is a DBMS, which manages a large amount of data in a multi user environment so that many users concurrently access the data. It Also provides security and Recovery. It stores and manages data using relational model.

Oracle is the name of database management system developed by Oracle corporation.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 2 2

ORACLE
Oracle server manages data in the database. Users access Oracle server using SQL commands. So Oracle server receives SQL commands from users and execute them on the database.

SQL commands CLIENT Result Database CLIENT ORACLE SERVER

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 3 3

DATABASE ARCHITECTURE

U1

U2

VL
SHADOW PAGE .. .. ..

.. .. ..

.. .. ..

.. .. ..

LL

T1

T2

LOWER LEVEL STRUCTURE

PL

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 4 4

Physical Level

The Physical structure of the database is placed in Physical level. It is physically a set of operating system files. There are 3 types. Data Files Redo log files Control files

These files automatically creates when database is created.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 5 5

Physical structure

Data Files It contains the data of the database. Every table that is stored in the database is a part of these files. Only Oracle Server can interpret these data files.

Redo Log Files Every database has a set of two or more Redo Log files. The set of redo log files is known as databases redo log.

Redo Log files are used in failure recovery. All changes made to the database are written to redo log file.. ( filenames redo01.log)

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 6 6

Physical Structure

Control Files Every Oracle database has a control file. It contains vital data regarding the database. It contains ( Extension of file is ctl) Database Name Names and locations of data files and redo log files.

Path Oracle\oradata\orcl ( to see all the 3 types of files)

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 7 7

Logical Structure

Logical Structure is independent of Physical structure. Each Oracle database contains the following components. Tablespaces Segments Extents Blocks

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 8 8

Tablespace Each Database is a collection of tablespaces. For example we can use a table space called PAYROLL to store all the data related to payroll application.

Every database contains SYSTEM table space. This is automatically created when a database is created. SYSTEM table space contains the data dictionary tables.

It is possible to make table space temporarily unavailable by making it off-line and make it available again by changing it to on-line. By making a tablespace off-line, DBA can take the backup.
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 9 9

SEGMENTS

Data into tablespaces comes in the form of segments. Example Table is a segment An Oracle database requires upto 4 types of segments

Data segments
Index Segments

It is used to store data of tables


used to store indexes

Rollback segments Here Undo information is stored


Temporary segments Oracle stores Temporary tables

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 10 10

Extents The storage space is allocated to segments is in the form of Extents. Each Tablespace contains 65536 data files N number of such Table spaces creates a database. An extent is made with in a data file N Number of continuous db blocks make up an Extent
Table Table Segment

Table space

Extents
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 11 11

QUERY PROCESS

1 2 3

PARSING EXECUTE FETCH

3 2
SERVER PROCESS

DB

CLIENT PROCESS

CLIENT

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 12 12

Oracle Instance

Every oracle database is associated with an Oracle Instance. Every time a database is started, a memory area called System Global Area(SGA) or Shared Global Area is allocated and one or more processes are started.

The combination of SGA and Oracle processes is called as Oracle Instance.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 13 13

Features & Benefits

ORACLE Scalability One Management Interface

11i
INTERNET Reliability Common Skill Sets

Single Dev Model

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 14 14

Features Oracle Offers a comprehensive high performance infrastructure for e-business.It is called Oracle9i.It provides every thing needed to develop, deploy and manage Internet Applications. Benefits

Scalability from departments to enterprise e-business sites


Reliable, available and secure architecture One development model, easy development options Common skill sets including SQL, PL/SQL,JAVA and XML One Management interface for all applications
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 15 15

9i Products There are two products. They provide a complete and simple infrastructure for internet applications.

IAS

DATABASE

9i

9i

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 16 16

Application Server

9i Application server runs all the applications and 9i database stores our data.

Oracle 9i Application server runs


Portals or web sites Java Transactional Applications Provides integration between users, applications and data

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 17 17

Oracle9i : OORDBMS

User-Defined data types and objects Fully compatible with relational database Support of multimedia and Large objects It also support client server and web based applications

Oracle 9i can scale tens of thousands of concurrent users and support up to 512 petabytes of data ( A peta byte is 1000 tera bytes)

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 18 18

Environment
Oracle uses two types of Environments for executing our SQL statements. SQL*plus and iSQL*plus. iSQL*plus is An Environment

Oracle proprietary
Keywords can be abbreviated Runs on a browser

Centrally loaded, does not have to be implemented on each machine

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 19 19

Tools of Oracle

SQL * PLUS ISQLPLUS PL/SQL FORMS REPORTS

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 20 20

SQL commands Data Definition Language (DDL) : CREATE, ALTER, DROP, TRUNCATE Data Manipulation Language (DML) : INSERT, UPDATE, DELETE Transaction Control Language (TCL) : COMMIT, ROLLBACK, SAVEPOINT

Data Retrieval Language (DRL) : SELECT


Data Control Language (DCL) : GRANT, REVOKE
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 21 21

Data Types
Data Type VARCHAR2(size) CHAR (size) NUMBER (p,s) DATE LONG CLOB RAW and LONG RAW Description Variable-length character data (4000 bytes) Fixed-length character data up to 2000 chars Variable-length numeric data Date and time values Variable-length character data upto 2 GB Character data up to 4 GB Raw binary data (Raw is 2000 bytes and Long Raw is 4 GB)
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 22 22

Data Types
BLOB

Binary data up to 4 GB

BFILE

Binary data stored in an external file up to 4GB


Variable-length character 4000bytes/chars) depending upon National Character Set

NVARCHAR2(size)

TIMESTAMP (precision) Date plus time

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 23 23

Writing Basic SQL SELECT Statements

Basic SELECT Statement

SELECT FROM

*|{[DISTINCT] column|expression [alias],...} table;

SELECT identifies what columns FROM identifies which table

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 25 25

Selecting All Tables and Columns


SELECT * FROM TAB; SELECT * FROM DEPT;

DESC[RIBE] DEPT

SELECT DEPTNO,LOC FROM DEPT;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 26 26

Writing SQL Statements 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.

Indents are used to enhance readability.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 27 27

Using Arithmetic Operators

SELECT ENAME, SAL, SAL + 300 FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 28 28

Defining a Null Value

null is a value that is unavailable, unassigned, unknown, or inapplicable. A null is not the same as zero or a blank space.

SELECT ENAME, JOB, SAL, COMM FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 29 29

Null Values in Arithmetic Expressions

Arithmetic expressions containing a null value evaluate to null.

SELECT ENAME, 12*SAL*COMM FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 30 30

Defining a Column Alias

A column alias: Renames a column heading

Is useful with calculations


follows the column name - there can also be the optional AS keyword between the column name and alias double quotation marks if it contains spaces or special characters or is case sensitive
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 31 31

Using Column Aliases

SELECT ENAME AS name , COMM commission FROM EMP;

SELECT EENAME "Name", SAL*12 "Annual Salary" FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 32 32

Concatenation Operator

A 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

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 33 33

Using the Concatenation Operator

SELECT FROM

ENAME || JOB AS "Employees" EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 34 34

Literal Character Strings

A literal is a character, a number, or a date included in the SELECT list. Date and character literal values must be enclosed within single quotation marks. Each character string is output once for each row returned.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 35 35

Using Literal Character Strings

SELECT ENAME ||' is a ' ||JOB AS "Employee Details" FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 36 36

Duplicate Rows

The default display of queries is all rows, including duplicate rows.

SELECT DEPTNO FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 37 37

Eliminating Duplicate Rows

Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.

SELECT DISTINCT DEPTNO FROM EMP;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 38 38

Restricting and Sorting Data

Objectives

After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 40 40

Limiting the Rows Selected

Restrict the rows returned by using the WHERE clause.


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

The WHERE clause follows the FROM clause.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 41 41

Using the WHERE Clause

SELECT EMPNO, ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO = 30 ;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 42 42

Character Strings and Dates Character strings and date values are enclosed in single quotation marks. Character values are case sensitive, and date values are format sensitive. The default date format is DD-MON-RR.
SELECT ENAME, JOB, DEPTNO FROM WHERE EMP ENAME = 'WARD';

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 43 43

Comparison Conditions

Operator = >

Meaning Equal to Greater than

>=
< <=

Greater than or equal to


Less than Less than or equal to

<>

Not equal to

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 44 44

Using Comparison Conditions

SELECT ENAME, SAL

FROM
WHERE

EMP
SAL <= 3000;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 45 45

Other Comparison Conditions

Operator BETWEEN ...AND... IN(set) LIKE IS NULL

Meaning Between two values (inclusive),

Match any of a list of values Match a character pattern Is a null value

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 46 46

Using the BETWEEN Condition

Use the BETWEEN condition to display rows based on a range of values.


SELECT ENAME, SAL FROM WHERE EMP SAL BETWEEN 2500 AND 3500;

Lower limit

Upper limit

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 47 47

Using the IN Condition

Use the IN membership condition to test for values in a list.

SELECT EMPNO, ENAME, SAL, FROM WHERE EMP

MGR

MGR IN (7900, 7566,7982 );

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 48 48

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.
SELECT FROM WHERE ENAME EMP ENAME LIKE 'S%';

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 49 49

Using the LIKE Condition

You can combine pattern-matching characters.


SELECT ENAME

FROM
WHERE

EMP
ENAME LIKE '_o%';

You can use the ESCAPE identifier to search for the actual % and _ symbols.
BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 50 50

Using the NULL Conditions

Test for nulls with the IS NULL operator.


SELECT ENAME, MGR FROM WHERE EMP MGR IS NULL;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 51 51

Logical Conditions

Operator AND

Meaning Returns TRUE if both component conditions are true

OR

Returns TRUE if either component condition is true


Returns TRUE if the following condition is false

NOT

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 52 52

Using the AND Operator

AND requires both conditions to be true.


SELECT EMPNO, ENAME, JOB, SAL FROM WHERE AND EMP SAL >=10000 JOB LIKE '%MAN%';

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 53 53

Using the OR Operator

OR requires either condition to be true.

SELECT EMPNO, ENAME, JOB, SAL FROM WHERE OR EMP SAL >= 10000 JOB LIKE '%MAN%';

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 54 54

Using the NOT Operator

SELECT ENAME, JOB

FROM

EMP

WHERE

JOB

NOT IN (MANAGER', 'CLERK', 'SALESMAN');

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 55 55

Rules of Precedence

Order Evaluated 1 2 3 4 5 6 7 8

Operator Arithmetic operators Concatenation operator Comparison conditions IS [NOT] NULL, LIKE, [NOT] IN [NOT] BETWEEN NOT logical condition AND logical condition OR logical condition

Override rules of precedence by using parentheses.


BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 56 56

Rules of Precedence

SELECT ENAME, JOB, SAL FROM WHERE OR AND EMP JOB = 'SALESMAN' JOB = 'PRESIDENT' SAL > 15000;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 57 57

Rules of Precedence

Use parentheses to force priority.


SELECT ENAME, JOB, SAL
FROM WHERE EMP (JOB = 'SALESMAN'

OR
AND

JOB = 'PRESIDENT')
SAL > 15000;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 58 58

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.


SELECT FROM ENAME, JOB, DEPTNO, HIREDATE EMP

ORDER BY HIREDATE ;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 59 59

Sorting in Descending Order

SELECT

ENAME, JOB, DEPTNO, HIREDATE

FROM

EMP

ORDER BY HIREDATE DESC ;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 60 60

Sorting by Column Alias

SELECT EMPNO, ENAME, SAL*12 annsal


FROM EMP

ORDER BY annsal;

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 61 61

Sorting by Multiple Columns

The order of ORDER BY list is the order of sort.


SELECT EMPNO, DEPTNO, SAL

FROM

EMP

ORDER BY DEPTNO, SAL DESC;

You can sort by a column that is not in the SELECT list.


BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 62 62

Summary In this lesson, you should have learned how to:

Use the WHERE clause to restrict rows of output Use the comparison conditions

Use the BETWEEN, IN, LIKE, and NULL conditions


Apply the logical AND, OR, and NOT operators

Use the ORDER BY clause to sort rows of output


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

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 63 63

Exercise 1) Display the names with single word 2) Display the names with two words 3) Display the names with exactly three words 4) Display all the salaries beginning with digit 2 5) Display all the names with second letter as A 6) Display the employees who joined between any two given dates 7) Display the names and experience of all the employees 8) How many employees does not draw salary between 5000 and 10000 9) Display all the names whose names contain underscore(_) 10)Calculate the experience in years for each employee and display along with their names, in descending order.

BUSINESS INFORMATION MANAGEMENT 2010 Capgemini. All rights reserved. 64 64

Das könnte Ihnen auch gefallen