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

ORACLE

Introduction

ORACLE

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.

Tools of Oracle

SQL * PLUS ISQLPLUS PL/SQL FORMS REPORTS

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

Data Types
Data Type VARCHAR2(size) bytes) CHAR (size) chars NUMBER (p,s) DATE LONG CLOB RAW and LONG RAW Description Variable-length character data(4000 default is 1 Fixed-length character data up to 2000 Default is 1 Variable-length numeric data. Maximum we can store upto 38 digits. 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 2 GB. Allows to store pictures)

Data Types

BLOB BFILE

Binary data up to 4 GB Binary data stored in an external file up to 4GB

NVARCHAR2(size)

Variable-length character data(4000 bytes/chars) depending upon National Character Set TIMESTAMP (precision) Date plus time

Writing Basic SQL SELECT Statements

Basic SELECT Statement


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

SELECT identifies what columns FROM identifies which table

Selecting All Tables and Columns

SELECT * FROM TAB; SELECT * FROM DEPT;

DESC[RIBE] DEPT

SELECT DEPTNO,LOC FROM DEPT;

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.

Using Arithmetic Operators

SELECT ENAME, SAL, SAL + 300 FROM EMP;

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;

Arithmetic expressions containing a null value evaluate to null.


SELECT ENAME, 12*SAL*COMM FROM EMP;

Null Values in Arithmetic Expressions

Defining a Column Alias


A column alias: Renames a column heading Is useful with calculations Immediately follows the column name there can also be the optional AS keyword between the column name and alias Requires double quotation marks if it contains spaces or special characters or is case sensitive

Using Column Aliases


SELECT ENAME AS name , COMM commission FROM EMP;

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

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

Using the Concatenation Operator

SELECT FROM EMP;

ENAME || JOB AS "Employees"

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.

Using Literal Character Strings

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

Duplicate Rows
The default display of queries is all rows, including duplicate rows.
SELECT DEPTNO FROM EMP;

Eliminating Duplicate Rows


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

SELECT DISTINCT DEPTNO FROM EMP;

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

Limiting the Rows Selected

Restrict the rows returned by using the WHERE clause.


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

SELECT FROM [WHERE

The WHERE clause follows the FROM clause.

Using the WHERE Clause

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

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';

Comparison Conditions
Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to

Using Comparison Conditions

SELECT ENAME, SAL FROM WHERE EMP SAL <= 3000;

Other Comparison Conditions


Operator BETWEEN ...AND... Meaning Between two values (inclusive),

IN(set)
LIKE IS NULL

Match any of a list of values


Match a character pattern Is a null value

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

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 );

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%';

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.

Using the NULL Conditions


Test for nulls with the IS NULL operator.
SELECT ENAME, MGR
FROM WHERE EMP MGR IS NULL;

Logical Conditions

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

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%';

Using the OR Operator


OR requires either condition to be true.

SELECT EMPNO, ENAME, JOB, SAL


FROM WHERE EMP SAL >= 10000

OR

JOB LIKE '%MAN%';

Using the NOT Operator

SELECT ENAME, JOB FROM WHERE EMP JOB NOTIN(MANAGER','CLERK','SALESMAN');

Rules of Precedence
Order Evaluated Operator 1 Arithmetic operators 2Concatenation operator 3Comparison conditions 4IS [NOT] NULL, LIKE, [NOT] IN 5[NOT] BETWEEN 6NOT logical condition 7AND logical condition 8OR logical condition Override rules of precedence by using parentheses.

Rules of Precedence
Use parentheses to force priority.

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

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 ;

Sorting in Descending Order


SELECT FROM ENAME, JOB, DEPTNO, HIREDATE EMP

ORDER BY HIREDATE DESC ;

Sorting by Column Alias

SELECT EMPNO, ENAME, SAL*12 annsal FROM EMP

ORDER BY annsal;

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.

Rules of Precedence

SELECT ENAME, JOB, SAL


FROM WHERE EMP JOB = 'SALESMAN'

OR
AND

JOB = 'PRESIDENT'
SAL > 15000;

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)DisplayallthenameswithsecondletterasA 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.

Вам также может понравиться