Oracle9i SQL

download Oracle9i SQL

of 312

  • date post

    07-Apr-2018
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

Transcript of Oracle9i SQL

  • 8/6/2019 Oracle9i SQL

    1/312

    Oracle 9i SQLBeing Presented By

    PM KarthickCorporate Trainer Executive

  • 8/6/2019 Oracle9i SQL

    2/312

  • 8/6/2019 Oracle9i SQL

    3/312

    Structure Query Language

    Structured Query

    Language (SQL) is the setof statements with which all

    programs and users access

    data in an Oracle database.

  • 8/6/2019 Oracle9i SQL

    4/312

  • 8/6/2019 Oracle9i SQL

    5/312

    Introduction to ORACLE

  • 8/6/2019 Oracle9i SQL

    6/312

    Objectives

    What is RDBMS ?

    Advantages of SQL and PLSQL

    RDBMS Vs ORDBMS

    Uses and benefits of PL/SQL

  • 8/6/2019 Oracle9i SQL

    7/312

    Relational Database Definition

    A relational database is a collection of relations ortwo-dimensional tables.

    DatabaseDatabase

    DEPTNO DNAME LOC

    10 ACCOUNTING NEW YORK

    20 RESEARCH DALLAS

    30 SALES CHICAGO

    40 OPERATIONS BOSTON

    Table Name:Table Name: DEPTDEPT

    EMPNO ENAME JOB DEPTNO

    7839 KING PRESIDENT 10

    7698 BLAKE MANAGER 30

    7782 CLARK MANAGER 10

    7566 JONES MANAGER 20

    Table Name:Table Name: EMPEMP

  • 8/6/2019 Oracle9i SQL

    8/312

    Data Models

    Entitymodel ofEntitymodel ofclients modelclients model

    Tables on diskTables on disk

    ServerServer

    TablemodelTablemodelof entity modelof entity model

    Model ofModel ofsystemsystem

    in clientsin clientsmindmind

  • 8/6/2019 Oracle9i SQL

    9/312

    Relating Multiple Tables

    EACH AND EVERY ROW ISIDENTIFIEDBY A UNIQUE KEY CALLED PRIMARY KEY

    EMPEMP DEPTDEPT

    Primary keyPrimary key Primary keyPrimary key

    LOGICAL RELATED ROWSSHARED the

    PKEY as FOREIGN KEY.

    Foreign keyForeign key

    EMPNO ENAME JOB DEPTNO

    7839 KING PRESIDENT 10

    7698 BLAKE MANAGER 30

    7782 CLARK MANAGER 10

    7566 JONES MANAGER 20

    DEPTNO DNAME LOC

    10 ACCOUNTING NEW YORK

    20 RESEARCH DALLAS

    30 SALES CHICAGO

    40 OPERATIONS BOSTON

  • 8/6/2019 Oracle9i SQL

    10/312

    Relational DB Properties

    A relational database

    - SQL Statements to be used for

    manipulating the database.- No Physical Pointers to access and store

    relations between Tables.

    -Set of Operators for Functionality

  • 8/6/2019 Oracle9i SQL

    11/312

    RDBMS Using SQL

    DatabaseDatabase

    SQL> SELECT loc2 FROM dept;

    SQL statementSQL statementis enteredis entered

    Message is sentMessage is sentto the Clientto the Client

    Query is sent toQuery is sent toDatabaseDatabase

    LocationLocation--------------------------DallasDallasNew YorkNew YorkSanta ClaraSanta Clara

    DeptDept

  • 8/6/2019 Oracle9i SQL

    12/312

    RDBMS

    User tablesUser tables DataDatadictionarydictionary

    ServerServer

  • 8/6/2019 Oracle9i SQL

    13/312

    ORACLE 9i An Overview

    Objects : User Defined Typesvery much in line to C++ Classes.

    Fully relational compatible

    LOBS / Multimedia Files in Database !

    Oracle Enterprise Manager

  • 8/6/2019 Oracle9i SQL

    14/312

    Why use OOPs Methodology

    SIMPLIFY To SOLVE.

    Objects and Interactions

    They replicate real time environment.

  • 8/6/2019 Oracle9i SQL

    15/312

  • 8/6/2019 Oracle9i SQL

    16/312

    PlatformIndependency

    Data files in the DiskData files in the Disk

    OSOS

    ORACLE SERVERORACLE SERVER

    USER 1USER 1 USER 2USER 2 USER 4USER 4USER 3USER 3

    OSmanages DataOSmanages Data

    filesfiles

    Server simply satisfiesServer simply satisfiesthe requestthe request

  • 8/6/2019 Oracle9i SQL

    17/312

    SQL Statements

    SELECT

    INSERTUPDATEDELETEMERGE

    CREATEALTERDROPRENAMETRUNCATE

    Data retrievalData retrieval

    Data Manipulation Language (DML)Data Manipulation Language (DML)

    Data Definition Language (DDL)Data Definition Language (DDL)

  • 8/6/2019 Oracle9i SQL

    18/312

    SQL Statements

    Transaction Control Language (TCL)Transaction Control Language (TCL)

    Data Control Language (DCL)Data Control Language (DCL)

    COMMITCOMMIT

    ROLLBACKROLLBACK

    SAVEPOINTSAVEPOINT

    GRANTGRANT

    REVOKEREVOKE

  • 8/6/2019 Oracle9i SQL

    19/312

    Objectives

    SQL SELECT statements

    A basic SELECT statement

    SQL statements and SQL*Plus commands

  • 8/6/2019 Oracle9i SQL

    20/312

    Basic SELECT Statement

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

    Select Clause determines what columns

    The From Clause determines which table.

  • 8/6/2019 Oracle9i SQL

    21/312

    Writing SQL Statements

    SQL is not Case Sensitive.Keywords cannot be split or abbreviated.SQL Statements can be split across lines.Clauses are placed in different lines, to

    promote readability.

  • 8/6/2019 Oracle9i SQL

    22/312

  • 8/6/2019 Oracle9i SQL

    23/312

    Selecting Specific Columns

    DEPTNO LOC--------- -------------

    10 NEW YORK20 DALLAS30 CHICAGO40 BOSTON

    SQL> SELECT deptno, loc2 FROM departments;

  • 8/6/2019 Oracle9i SQL

    24/312

    Default Column Headings

    Default justification- Date and Character Data is Left Justified- Numeric Data is Right Justified

    Display Headings in UPPERCASE. Character / Date Columns headings will be

    Truncated. Numerical Columns headings are not truncated.

    Column name can be replaced by the Alias name

  • 8/6/2019 Oracle9i SQL

    25/312

    Arithmetic Expressions

    Basic Arithmetic operators

    Operator

    +

    -

    *

    /

    Description

    Add

    Subtract

    Multiply

    Divide

  • 8/6/2019 Oracle9i SQL

    26/312

    Using Arithmetic Operators

    SQL> SELECT ename, sal, sal+3002 FROM employees;

    ENAME SAL SAL+300

    ---------- --------- ---------KING 5000 5300BLAKE 2850 3150CLARK 2450 2750JONES 2975 3275 MARTIN 1250 1550

    ALLEN 1600 1900...14 rows selected.

  • 8/6/2019 Oracle9i SQL

    27/312

    Operator Precedence

    Parentheses can force precedence

    Multiplication and Division followed by

    Addition and subtraction.

    ** // ++__

  • 8/6/2019 Oracle9i SQL

    28/312

    Operator Precedence

    SQL> SELECT ename, sal, 12*sal+1002 FROM employees;

    ENAME SAL 12*SAL+100

    ---------- --------- ----------KING 5000 60100BLAKE 2850 34300CLARK 2450 29500JONES 2975 35800 MARTIN 1250 15100

    ALLEN 1600 19300...14 rows selected.

  • 8/6/2019 Oracle9i SQL

    29/312

    Using Parentheses

    SQL> SELECT ename, sal, 12*(sal+100)2 FROM employees;

    ENAME SAL 12*(SAL+100)

    ---------- --------- -----------KING 5000 61200BLAKE 2850 35400CLARK 2450 30600JONES 2975 36900 MARTIN 1250 16200

    ...14 rows selected.

  • 8/6/2019 Oracle9i SQL

    30/312

    Defining a Null Value

    NULL is UNASSIGNED Value.

    SQL> SELECT ename, job, comm2 FROM emp;

    ENAME JOB COMM---------- --------- ---------KING PRESIDENTBLAKE MANAGER

    ...TURNER SALESMAN 0

    ...14 rows selected.

  • 8/6/2019 Oracle9i SQL

    31/312

    Null Values in Arithmetic Expr

    NULL as an operand will result NULL

    SQL> select ename NAME, 12*sal+comm2 from emp3 WHERE ename='KING';

    NAME 12*SAL+COMM

    ---------- -----------KING

  • 8/6/2019 Oracle9i SQL

    32/312

    SQL> SELECT ename, sal, comm, (sal*12)+NVL(comm,0)2 FROM emp;

    Using the NVL Function

    ENAME SAL COMM (SAL*12)+NVL(COMM,0)---------- --------- --------- --------------------KING 5000 60000BLAKE 2850 34200CLARK 2450 29400JONES 2975 35700 MARTIN 1250 1400 16400

    ALLEN 1600 300 19500...14 rows selected.

  • 8/6/2019 Oracle9i SQL

    33/312

    Defining Column Alias

    The Heading name is replaced for thecurrent SELECT Statement.

    AS Keyword [ Optional ] between thecolumn name and the actual alias name

    Double Quotation Marks.

  • 8/6/2019 Oracle9i SQL

    34/312

    Using Column Aliases

    SQL> SELECT ename AS name, sal salary2 FROM employees;

    NAME SALARY

    ------------- ---------

    ...

    SQL> SELECT ename "Name",2 sal*12 "Annual Salary"3 FROM employees;

    Name Annual Salary

    ------------- -------------

    ...

  • 8/6/2019 Oracle9i SQL

    35/312

    Concatenation Operator (||)

    Concatenates the Columns of any data

    type.

    A Resultant column will be a Single column.

  • 8/6/2019 Oracle9i SQL

    36/312

  • 8/6/2019 Oracle9i SQL

    37/312

    Literal Character Strings

    Date and character literal values must beenclosed within single quotation marks.

  • 8/6/2019 Oracle9i SQL

    38/312

    Using DISTINCT Clause

    Eliminate duplicate rows by using the

    DISTINCT keyword

    SQL> SELECT DISTINCT deptno

    2 FROM employees;

    DEPTNO---------

    10

    2030

  • 8/6/2019 Oracle9i SQL

    39/312

    Summary

    Use SQL*Plus as an environment to:

    - Execute SQL statements- Edit SQL statements

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

  • 8/6/2019 Oracle9i SQL

    40/312

  • 8/6/2019 Oracle9i SQL

    41/312

    Objectives

    Limit the rows required

    Arrange the rows in a particular order.

  • 8/6/2019 Oracle9i SQL

    42/312

    Using WHERE Clause

    Specify the Selection of rows retrieved bythe WHERE Clause.

    The W