SQL Day1 Session1

Embed Size (px)

Citation preview

  • 8/6/2019 SQL Day1 Session1

    1/64

    SQL and PL/SQL Essentials

  • 8/6/2019 SQL Day1 Session1

    2/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 22

    ORACLEOak Ridge Automated Computer

    And Logical Engine

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

    Oracle is the name of database management systemdeveloped by Oracle corporation.

  • 8/6/2019 SQL Day1 Session1

    3/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 33

    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 themon the database.

    ORACLE SERVER

    Database

    CLIENT

    CLIENT

    SQL commands

    Result

  • 8/6/2019 SQL Day1 Session1

    4/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 44

    DATABASE ARCHITECTURE

    LOWER LEVEL STRUCTURE PL

    LL

    VL

    ..

    ..

    ..

    ..

    ..

    ..

    ..

    ..

    ..

    U1 U2

    ..

    ..

    ..

    SHADOWPAGE

    T1 T2

  • 8/6/2019 SQL Day1 Session1

    5/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 55

    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.

  • 8/6/2019 SQL Day1 Session1

    6/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 66

    Physical structure

    Data Files It contains the data of the database. Every table that is storedin the database is a part of these files. Only Oracle Server can interpretthese 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)

  • 8/6/2019 SQL Day1 Session1

    7/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 77

    Physical Structure

    Control Files Every Oracle database has a control file. It containsvital 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)

  • 8/6/2019 SQL Day1 Session1

    8/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 88

    Logical Structure

    Logical Structure is independent of Physical structure.Each Oracle database contains the followingcomponents.

    Tablespaces

    Segments

    Extents

    Blocks

  • 8/6/2019 SQL Day1 Session1

    9/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 99

    Tablespace

    Each Database is a collection of tablespaces. For example we

    can use a table space called PAYROLL to store all the datarelated to payroll application.

    Every database contains SYSTEM table space. This is

    automatically created when a database is created. SYSTEMtable space contains the data dictionary tables.

    It is possible to make table space temporarily unavailable bymaking it off-line and make it available again by changing it toon-line. By making a tablespace off-line, DBA can take thebackup.

  • 8/6/2019 SQL Day1 Session1

    10/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1010

    SEGMENTS

    Data into tablespaces comes in the form of segments. Example Tableis a segment

    An Oracle database requires upto 4 types of segments

    Data segments It is used to store data of tables

    Index Segments used to store indexes

    Rollback segments Here Undo information is stored

    Temporary segments Oracle stores Temporary tables

  • 8/6/2019 SQL Day1 Session1

    11/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1111

    Extents

    The storage space is allocated to segments is in the form ofExtents. 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

    Extents

    Tablespace

  • 8/6/2019 SQL Day1 Session1

    12/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1212

    QUERY PROCESS

    CLIENT

    DBSERVER PROCESS

    CLIENTPROCESS

    1

    2

    31 PARSING

    2 EXECUTE

    3 FETCH

  • 8/6/2019 SQL Day1 Session1

    13/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1313

    Oracle Instance

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

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

  • 8/6/2019 SQL Day1 Session1

    14/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1414

    Features & Benefits

    11i

    ORACLE

    INTERNET

    Scalability

    Reliability

    Single Dev Model

    Common Skill Sets

    One ManagementInterface

  • 8/6/2019 SQL Day1 Session1

    15/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1515

    Features

    Oracle Offers a comprehensive high performance

    infrastructure for e-business.It is called Oracle9i.It providesevery thing needed to develop, deploy and manage InternetApplications.

    Benefits

    Scalability from departments to enterprise e-business sitesReliable, available and secure architecture

    One development model, easy development options

    Common skill sets including SQL, PL/SQL,JAVA and XMLOne Management interface for all applications

    9i P d

  • 8/6/2019 SQL Day1 Session1

    16/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1616

    9i Products

    9i

    IAS

    9i

    DATABASE

    There are two products. They provide a complete and simpleinfrastructure for internet applications.

  • 8/6/2019 SQL Day1 Session1

    17/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1717

    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, applicationsand data

  • 8/6/2019 SQL Day1 Session1

    18/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1818

    Oracle9i : OORDBMS

    User-Defined data types and objectsFully 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 andsupport up to 512 petabytes of data ( A peta byte is 1000 terabytes)

  • 8/6/2019 SQL Day1 Session1

    19/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 1919

    Environment

    Oracle uses two types of Environments for executing our SQL

    statements. SQL*plus and iSQL*plus.iSQL*plus is

    An Environment

    Oracle proprietaryKeywords can be abbreviated

    Runs on a browser

    Centrally loaded, does not have to be implemented on eachmachine

  • 8/6/2019 SQL Day1 Session1

    20/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2020

    Tools of Oracle

    SQL * PLUS ISQLPLUS PL/SQL FORMS

    REPORTS

  • 8/6/2019 SQL Day1 Session1

    21/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2121

    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

    D t T

  • 8/6/2019 SQL Day1 Session1

    22/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2222

    Data Types

    Data Type DescriptionVARCHAR2( size ) Variable-length character data

    ( 4000 bytes)

    CHAR (size) Fixed-length character data up to2000 chars

    NUMBER ( p , s) Variable-length numeric data

    DATE Date and time values

    LONG Variable-length character data upto 2GB

    CLOB Character data up to 4 GBRAW and LONG RAW Raw binary data (Raw is 2000 bytes

    and Long Raw is 4 GB)

  • 8/6/2019 SQL Day1 Session1

    23/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2323

    Data Types

    BLOB Binary data up to 4 GB

    BFILE Binary data stored in an external file up to4GB

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

    TIMESTAMP (precision) Date plus time

  • 8/6/2019 SQL Day1 Session1

    24/64

    Writing Basic SQL SELECT Statements

  • 8/6/2019 SQL Day1 Session1

    25/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2525

    Basic SELECT Statement

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

    SELECT identifies what columns FROM identifies which table

  • 8/6/2019 SQL Day1 Session1

    26/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2626

    SELECT *FROM DEPT;

    Selecting All Tables and Columns

    SELECT *FROM TAB;

    DESC[RIBE] DEPT

    SELECT DEPTNO,LOCFROM DEPT;

  • 8/6/2019 SQL Day1 Session1

    27/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2727

    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.

  • 8/6/2019 SQL Day1 Session1

    28/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2828

    Using Arithmetic Operators

    SELECT ENAME, SAL, SAL + 300FROM EMP;

  • 8/6/2019 SQL Day1 Session1

    29/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 2929

    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;

    Null Values

  • 8/6/2019 SQL Day1 Session1

    30/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3030

    SELECT ENAME, 12*SAL*COMM FROM EMP;

    in Arithmetic Expressions

    Arithmetic expressions containing a null value evaluate to null.

  • 8/6/2019 SQL Day1 Session1

    31/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3131

    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 ASkeyword between the column name and alias

    double quotation marks if it contains spaces or special charactersor is case sensitive

    l l

  • 8/6/2019 SQL Day1 Session1

    32/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3232

    Using Column Aliases

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

    SELECT ENAME AS name , COMM commissionFROM EMP;

    C i O

  • 8/6/2019 SQL Day1 Session1

    33/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3333

    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

  • 8/6/2019 SQL Day1 Session1

    34/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3434

    Using the Concatenation Operator

    SELECT ENAME || JOB AS "Employees"

    FROM EMP;

    Lit l Ch t St i g

  • 8/6/2019 SQL Day1 Session1

    35/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3535

    Literal Character Strings

    A literal is a character, a number, or a date included in theSELECT list.

    Date and character literal values must be enclosed within singlequotation marks.

    Each character string is output once for each row returned.

    Using Literal Character Strings

  • 8/6/2019 SQL Day1 Session1

    36/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3636

    Using Literal Character Strings

    SELECT ENAME ||' is a ' ||JOB

    AS "Employee Details"FROM EMP;

    Duplicate Rows

  • 8/6/2019 SQL Day1 Session1

    37/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3737

    Duplicate Rows

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

    SELECT DEPTNOFROM EMP;

    Eliminating Duplicate Rows

  • 8/6/2019 SQL Day1 Session1

    38/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 3838

    Eliminating Duplicate Rows

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

    SELECT DISTINCT DEPTNOFROM EMP;

  • 8/6/2019 SQL Day1 Session1

    39/64

    Restricting and Sorting Data

    Objectives

  • 8/6/2019 SQL Day1 Session1

    40/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 4040

    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

  • 8/6/2019 SQL Day1 Session1

    41/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 4141

    Limiting the Rows Selected

    Restrict the rows returned by using the WHERE clause.

    The WHERE clause follows the FROM clause.

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

    Using the WHERE Clause

  • 8/6/2019 SQL Day1 Session1

    42/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 4242

    SELECT EMPNO, ENAME, JOB, DEPTNO

    FROM EMP

    WHERE DEPTNO = 30 ;

    Character Strings and Dates

  • 8/6/2019 SQL Day1 Session1

    43/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 4343

    Character Strings and Dates

    Character strings and date values are enclosed in single quotationmarks. Character values are case sensitive, and date values are formatsensitive. The default date format is DD-MON-RR.

    SELECT ENAME, JOB, DEPTNO

    FROM EMP

    WHERE ENAME = 'WARD';

    Comparison Conditions

  • 8/6/2019 SQL Day1 Session1

    44/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 4444

    Operator

    =

    >

    >=

    =10000

    AND JOB LIKE '%MAN%';

    Using the OR Operator

  • 8/6/2019 SQL Day1 Session1

    54/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 5454

    OR requires either condition to be true.

    SELECT EMPNO, ENAME, JOB, SAL

    FROM EMP

    WHERE SAL >= 10000

    OR JOB LIKE '%MAN%';

    Using the NOT Operator

  • 8/6/2019 SQL Day1 Session1

    55/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 5555

    SELECT ENAME, JOB

    FROM EMP

    WHERE JOB

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

    Rules of Precedence

  • 8/6/2019 SQL Day1 Session1

    56/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 5656

    Override rules of precedence by using parentheses.

    Order Evaluated Operator1 Arithmetic operators2 Concatenation operator3 Comparison conditions

    4 IS [NOT] NULL , LIKE , [NOT] IN5 [NOT] BETWEEN6 NOT logical condition7 AND logical condition8 OR logical condition

    Rules of Precedence

  • 8/6/2019 SQL Day1 Session1

    57/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 5757

    SELECT ENAME, JOB, SAL

    FROM EMP

    WHERE JOB = 'SALESMAN'

    OR JOB = 'PRESIDENT'

    AND SAL > 15000;

    Rules of Precedence

  • 8/6/2019 SQL Day1 Session1

    58/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 5858

    SELECT ENAME, JOB, SAL

    FROM EMP

    WHERE (JOB = 'SALESMAN'

    OR JOB = 'PRESIDENT')

    AND SAL > 15000;

    Use parentheses to force priority .

    ORDER BY Clause

  • 8/6/2019 SQL Day1 Session1

    59/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 5959

    SELECT ENAME, JOB, DEPTNO, HIREDATE

    FROM EMP

    ORDER BY HIREDATE ;

    Sort rows with the ORDER BY clause ASC: ascending order, default DESC: descending order

    The ORDER BY clause comes last in the SELECT

    statement.

    Sorting in Descending Order

  • 8/6/2019 SQL Day1 Session1

    60/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 6060

    SELECT ENAME, JOB, DEPTNO, HIREDATE

    FROM EMP

    ORDER BY HIREDATE DESC ;

    Sorting by Column Alias

  • 8/6/2019 SQL Day1 Session1

    61/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 6161

    SELECT EMPNO, ENAME, SAL*12 annsal

    FROM EMP

    ORDER BY annsal;

    Sorting by Multiple Columns

  • 8/6/2019 SQL Day1 Session1

    62/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 6262

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

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

    SELECT EMPNO, DEPTNO, SAL

    FROM EMP

    ORDER BY DEPTNO, SAL DESC;

    Summary

  • 8/6/2019 SQL Day1 Session1

    63/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 6363

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

    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

    Exercise

    1) Display the names with single word

  • 8/6/2019 SQL Day1 Session1

    64/64

    BUSINESS INFORMATION MANAGEMENT

    2010 Capgemini. All rights reserved. 6464

    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 5000and 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.