Oracle_ch2.pptx

download Oracle_ch2.pptx

of 52

Transcript of Oracle_ch2.pptx

  • 8/10/2019 Oracle_ch2.pptx

    1/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Chapter 2Connecting to Oracle 11g

    andBasic SQL SELECT Statements

    Jason C.H. Chen , Ph.D. Professor of MIS

    School of Business, Gonzaga UniversitySpokane, WA 99258 USA

    [email protected]

  • 8/10/2019 Oracle_ch2.pptx

    2/52

    Dr. Chen, Busin ess Database Systems (Oracle)2

    Objectives

    Learn how to connect to Oracle 11g (client/server andPersonal)

    Create the initial database Learn database objects and SQL commands and components Identify keywords, mandatory clauses, and optional clauses in

    a SELECT statement Select and view one/all columns of a table Display multiple columns of a table Use a column alias to clarify the contents of a particular

    column Perform basic arithmetic operations in the SELECT clause Remove duplicate lists using either the DISTINCT or

    UNIQUE keyword

    Use concatenation to combine fields, literals, and other data

  • 8/10/2019 Oracle_ch2.pptx

    3/52

    Dr. Chen, Busin ess Database Systems (Oracle)3

    Connect to Oracle 11g

    There are two ways connecting to and workingwith Oracle 11g

    SQL Plus a command driven facility

    SQL Developer a GUI facility

    Indicate the following link to include java.exe (use Browse ):c:\program Files\java\ jdk1.6.0 \bin\java.exe

  • 8/10/2019 Oracle_ch2.pptx

    4/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Connect to Oracle 11g usingSQL Plus

    Figure 2-1 SQL*Plus Interface

    type exitto quit thesystem

  • 8/10/2019 Oracle_ch2.pptx

    5/52

  • 8/10/2019 Oracle_ch2.pptx

    6/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Connect to Oracle 11g usingSQL Developer for the first time

    Sometimes you need to do the following (also available on the Installation Guide on Bb )

    Now run SQL Developer . You will be prompted to enterthe pathname for your newly installed Java JDK program.

    If the program was installed in C and you run a 64-bitversion of Windows, the destination will be as follows:C:\Program F iles (x86)\Java\jdk1.6.0_26\bin\java.exe . Ifnot you click Browse and find the Java program yourself,

    just make sure you point to the JDK version not the JRE.

  • 8/10/2019 Oracle_ch2.pptx

    7/52Dr. Chen, Busin ess Database Systems (Oracle)

    Connect to Oracle 11g usingSQL Developer

    Figure 2-2 SQL Developer Interface

  • 8/10/2019 Oracle_ch2.pptx

    8/52Dr. Chen, Busin ess Database Systems (Oracle)

    Connect to Oracle 11g usingSQL Developer

    click on

  • 8/10/2019 Oracle_ch2.pptx

    9/52Dr. Chen, Busin ess Database Systems (Oracle)

    #1

    #2

    Complete the information on the Database Connection Screen

    jepson-oracle

    sbaora11

  • 8/10/2019 Oracle_ch2.pptx

    10/52Dr. Chen, Busin ess Database Systems (Oracle)

    Connect to Oracle 11g usingPersonal SQL Developer

    click on TNS

    (password is created during the Oracle installation)

  • 8/10/2019 Oracle_ch2.pptx

    11/52Dr. Chen, Busin ess Database Systems (Oracle)

    Complete the information on the Database Connection Screen(Personal Oracle 11g)

  • 8/10/2019 Oracle_ch2.pptx

    12/52Dr. Chen, Busin ess Database Systems (Oracle)

    12

    YOUR TURNHow to Access Your Oracle11g Account

    User Name: Password: [ case sensitive ]

    jepson-oracle

    sbaora11

  • 8/10/2019 Oracle_ch2.pptx

    13/52Dr. Chen, Busin ess Database Systems (Oracle)

    drag, if output screennot displayed

  • 8/10/2019 Oracle_ch2.pptx

    14/52Dr. Chen, Busin ess Database Systems (Oracle)

    14

    Database Objects and Queries

    An Oracle database consists of multiple user accounts Each user account owns database objects

    Tables Views

    Stored programs, etc. Query: command to perform operation on databaseobject

    Structured Query Language (SQL) Industry standard query language for most of relationaldatabases Consists of about 30 commands

  • 8/10/2019 Oracle_ch2.pptx

    15/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Basic SQL Concepts and Commands

    SQL (Structured Query Language) is used tomanipulate the database.

    There are two basic types of SQL commands:

    DDL commands work with the structure of the objects

    (tables, indexes, views) in the database.DML commands work with the data in the database(i.e.,manipulate the data).

    Reserved words - SQL command words

    Data Definition Language ( DDL )Data Manipulation Language ( DML )

  • 8/10/2019 Oracle_ch2.pptx

    16/52

    Dr. Chen, Busin ess Database Systems (Oracle)16

    SQL Components

    DDL

    DML

    DCL

    D.B.

    ORACLE(SQL Components)

    (Create Table structureand insert database)

  • 8/10/2019 Oracle_ch2.pptx

    17/52

    Dr. Chen, Busin ess Database Systems (Oracle)17

    DDL Commands

    Used to create and modify the structure ofdatabase objects CREATE

    ALTER DROP GRANT REVOKE

    DDL commands execute as soon as they areissued, and do not need to be explicitly saved

  • 8/10/2019 Oracle_ch2.pptx

    18/52

    Dr. Chen, Busin ess Database Systems (Oracle)18

    DML Commands

    Used to insert, view, and modify database data INSERT UPDATE DELETE SELECT

    DML commands need to be explicitly saved orrolled back COMMIT ROLLBACK SAVEPOINT

  • 8/10/2019 Oracle_ch2.pptx

    19/52

    Dr. Chen, Busin ess Database Systems (Oracle)19

    Create the JustLee Database

    Use the provided script to create thedatabase so you can follow the chapterexamples

    Verify table contents (structure) using theDESCRIBE command

  • 8/10/2019 Oracle_ch2.pptx

    20/52

    Dr. Chen, Busin ess Database Systems (Oracle)20

    Scripts

    Scripts are simply files that contain a sequence ofSQL and/or PL/SQL commands that can beexecuted in SQL*Plus.

    Note: SQL and PL/SQL(to be introduced) are two

    languages Oracle uses. SQL*Plus is the tool we use to access Oracle. SQL*Plus can execute both SQL and PL/SQL

    commands plus it has its own commands.

  • 8/10/2019 Oracle_ch2.pptx

    21/52

    Dr. Chen, Busin ess Database Systems (Oracle)21

    Class Exercise

    Create a new folder on c:\ as follows:c:\oradata\chapter2

    Download all files in chapter 2 from Bb (underCourse Documents) into c: \oradata\chapter2\

    Logon to SQL*Plus and type the followingcommand (not on SQL Developer)SQL> start c:\oradata\chapter2\JLDB_Build.sql

    or,SQL> @ c:\oradata\chapter2\JLDB_Build.sqlSQL> SELECT * FROM publisher;

  • 8/10/2019 Oracle_ch2.pptx

    22/52

    Dr. Chen, Busin ess Database Systems (Oracle)22

    Class Exercise (continued)

    A table name only can be created once andsaved in the current database. Therefore, ifyou re-do the previous start command youwill receive error message.

    To avoid the problem, you need DROP thetables from the database before those tables(using the same names) are recreated.

    Type the following command forSQL> start c:\oradata\chapter2\JLDB_Drop.sql

    SQL> SELECT * FROM publisher;

  • 8/10/2019 Oracle_ch2.pptx

    23/52

    Dr. Chen, Busin ess Database Systems (Oracle)23

    Installing Personal Oracle11g

    I created a Discussion Questions on the Bb for you toshare (or post question) your experience on installation ofOracle 11g. Please participate and it will be also your

    part of class performance.

    Please note that if your computer with 64-bit operating

    system such as Windows 7, you cant use the bundledDVD to install Oracle since it is for 32-bit OS. Rather

    you need to download 64-bit Oracle 11g from the Oraclesite ( www.oracle.com ) or type in keyword search ofOracle 11g download.

  • 8/10/2019 Oracle_ch2.pptx

    24/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Figure 2-3 List of existing (created) tables

    How to verify Created Tables and their Structure?

    Figure 2-3 List the structure of the BOOKS table

    Also try to type the same commands on SQL>

  • 8/10/2019 Oracle_ch2.pptx

    25/52

    Dr. Chen, Busin ess Database Systems (Oracle)25

    SELECT Statement Syntax

    SELECT statements are used to retrieve data fromthe database

    A SELECT statement is referred to as a query

    Syntax gives the basic structure, or rules, for acommand

    Optional clauses and keywords are shown in brackets

  • 8/10/2019 Oracle_ch2.pptx

    26/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    SELECT Statement Syntax (continued)

    SELECT and FROM clauses are required

    SELECT clause identifies column(s) FROM clause identifies table(s) Each clause begins with a keyword

    Figure 2-5 Syntax for the SELECT statement

  • 8/10/2019 Oracle_ch2.pptx

    27/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Selecting All Data in a Table

    Substitute an asterisk for the column names in aSELECT clause

    Figure 2-6 Command to select all data in a table

  • 8/10/2019 Oracle_ch2.pptx

    28/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Selecting One Column from a Table

    Enter column name in SELECT clause

    Figure 2-7 Command to select in a single column

    -- CASE is not sensitive-- chapter 2, Figure 2-8; p. 34SELECT TITLE FROM BOOKS;

    SELECT title from books;SELECT title FROM books;SELECT TITLEFROM BOOKS;

  • 8/10/2019 Oracle_ch2.pptx

    29/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Selecting Multiple Columns from a Table

    Separate column names with a comma

    Figure 2-9 Command to select multiple columns from a table

    -- chapter 2, Figure 2-10; p. 36SELECT pubdate, titleFROM books;Figure 2-10 Reversed column sequence in theSELECT clause

  • 8/10/2019 Oracle_ch2.pptx

    30/52

    Dr. Chen, Busin ess Database Systems (Oracle)30

    Operations within the SELECT Statement

    List the alias after the column heading AS keyword is optional

    Enclose in double quotation marks: If it contains blank space(s) If it contains special symbol(s) To retain case

    Column alias can be used for column headings Perform arithmetic operations Suppress duplicates

    Concatenate data

  • 8/10/2019 Oracle_ch2.pptx

    31/52

    Dr. Chen, Busin ess Database Systems (Oracle)31

    Column Alias Example

    Figure 2-11 Using a column alias Figure 2-12 Using a column alias without

    the AS keyword

  • 8/10/2019 Oracle_ch2.pptx

    32/52

    Dr. Chen, Busin ess Database Systems (Oracle)32

    Using Arithmetic Operations

    Arithmetic operations Executed left to right Multiplication and division are solved first Addition and subtraction are solved last Override order with parentheses

  • 8/10/2019 Oracle_ch2.pptx

    33/52

    Dr. Chen, Busin ess Database Systems (Oracle)33

    Example Arithmetic Operation withColumn Alias

    Figure 2-13 Using a column alias for an arithmetic expression

  • 8/10/2019 Oracle_ch2.pptx

    34/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Figure 2-14 NULL values in the Discount column

    The following output is from SQL>

  • 8/10/2019 Oracle_ch2.pptx

    35/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    ISBN TITLE PUBDATE PUBID COST---------- ------------------------------ --------- ---------- ----------

    RETAIL DISCOUNT CATEGORY---------- ---------- ------------1059831198 BODYBUILD IN 10 MINUTES A DAY 21-JAN-05 4 18.75

    30.95 FITNESS

    0401140733 REVENGE OF MICKEY 14-DEC-05 1 14.222 FAMILY LIFE

    4981341710 BUILDING A CAR WITH TOOTHPICKS 18-MAR-06 2 37.859.95 3 CHILDREN

    8843172113 DATABASE IMPLEMENTATION 04-JUN-03 3 31.455.95 COMPUTER

    3437212490 COOKING WITH MUSHROOMS 28-FEB-04 4 12.519.95 COOKING

    3957136468 HOLY GRAIL OF ORACLE 31-DEC-05 3 47.2575.95 3.8 COMPUTER

    The following output is from SQL>

    Question: How can we improve the output?(see chapter 2, Figure 2-14(b) on Ch2Queries.sql)

    We will learn it later and more in the later chapters.

    SQL> select * from books;

  • 8/10/2019 Oracle_ch2.pptx

    36/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Using DISTINCT and UNIQUE Enter DISTINCT or UNIQUE after SELECT keyword to suppress duplicates

    Figure 2-16 List of unduplicated states for customers

    -- chapter 2, Figure 2-16; p. 43-- without DISTINCTSELECT stateFROM customers;

    SQL> SELECT state FROM customers;

    ST--

    FLCAFLIDWA

    NYTXWYCAGAILMAFLWYFLCAMIGANJNJ

    20 rows selected.

  • 8/10/2019 Oracle_ch2.pptx

    37/52

    Dr. Chen, Busin ess Database Systems (Oracle)Figure 2-17 Unduplicated list of cities for customers

    More example

    -- chapter 2, Figure 2-17; p. 44

    SELECT DISTINCT state, cityFROM customers;

  • 8/10/2019 Oracle_ch2.pptx

    38/52

    Dr. Chen, Busin ess Database Systems (Oracle)38

    Using Concatenation

    You can combine data

    with a string literal Use the concatenation

    operator, ||

    It allows the use ofcolumn aliases

    Figure 2-18 Concatenation of two columns

  • 8/10/2019 Oracle_ch2.pptx

    39/52

    Dr. Chen, Busin ess Database Systems (Oracle)39

    Concatenation Examples with Alternate name

    Figure 2-20 Using a column alias for concatenated valuesFigure 2-219 Using a string literal in concatenation

  • 8/10/2019 Oracle_ch2.pptx

    40/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    SQL> select * from books;

    ISBN TITLE PUBDATE PUBID COST

    ---------- ------------------------------ --------- ---------- ----------RETAIL DISCOUNT CATEGORY---------- ---------- ------------1059831198 BODYBUILD IN 10 MINUTES A DAY 21-JAN-05 4 18.75

    30.95 FITNESS

    0401140733 REVENGE OF MICKEY 14-DEC-05 1 14.222 FAMILY LIFE

    4981341710 BUILDING A CAR WITH TOOTHPICKS 18-MAR-06 2 37.859.95 3 CHILDREN

    8843172113 DATABASE IMPLEMENTATION 04-JUN-03 3 31.455.95 COMPUTER

    3437212490 COOKING WITH MUSHROOMS 28-FEB-04 4 12.519.95 COOKING

    3957136468 HOLY GRAIL OF ORACLE 31-DEC-05 3 47.2575.95 3.8 COMPUTER

    The following output is from SQL>

    Question: How can we produce a readable output format?

  • 8/10/2019 Oracle_ch2.pptx

    41/52

    Dr. Chen, Busin ess Database Systems (Oracle)41

    Formatting Output in SQL*Plus

    To change default column headings: Specify alternate column headings : SELECT fieldname1

    " heading1_text" , fieldname2 " heading2_text" , ... Use an alias for column headings: SELECT fieldname1 AS

    alias_name1...

    Use column statement To change SQL*Plus line and page size settings

    Select Options/Environment on menu bar Modify linesize and pagesize to desired values

    You may also type the following commands at SQL>

    SQL> SET LINESIZE 80 SQL> SET PAGESIZE 45

    Please download chapter 14 (pdf file on Bb) for morecommands on formatting output.

  • 8/10/2019 Oracle_ch2.pptx

    42/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Advanced SQL Commandsclear screen;

    column format a15 wrapcolumn format a15 wordcolumn format a20 heading description for the field column format a15 truncatecolumn format $999.99

    -- note: default is wrap-- formatting output (try it on the SQL>)-- chapter 2, Figure 2-14(b); p. 41clear screenSET LINESIZE 80SET PAGESIZE 45column title format a12 heading 'Book Title' WORDcolumn PubID format 99 heading 'ID'column Cost format $999.99column retail format $999.99

    SELECT *

    FROM books;

    Here is a better output after formatting output:

  • 8/10/2019 Oracle_ch2.pptx

    43/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    ISBN Book Title PUBDATE ID COST RETAIL DISCOUNT CATEGORY---------- ------------ --------- --- -------- -------- ---------- ------------1059831198 BODYBUILD IN 21-JAN-05 4 $18.75 $30.95 FITNESS

    10 MINUTESA DAY

    0401140733 REVENGE OF M 14-DEC-05 1 $14.20 $22.00 FAMILY LIFE

    ICKEY4981341710 BUILDING A C 18-MAR-06 2 $37.80 $59.95 3 CHILDRENAR WITH TOOTHPICKS

    8843172113 DATABASE IMP 04-JUN-03 3 $31.40 $55.95 COMPUTERLEMENTATION

    3437212490 COOKING WITH 28-FEB-04 4 $12.50 $19.95 COOKINGMUSHROOMS

    3957136468 HOLY GRAIL O 31-DEC-05 3 $47.25 $75.95 3.8 COMPUTERF ORACLE

    1915762492 HANDCRANKED 21-JAN-05 3 $21.80 $25.00 COMPUTER

    COMPUTERS9959789321 E-BUSINESS T 01-MAR-06 2 $37.90 $54.50 COMPUTER

    HE EASY WAY2491748320 PAINLESS CHI 17-JUL-04 5 $48.00 $89.95 4.5 FAMILY LIFE

    LD-REARING0299282519 THE WOK WAY 11-SEP-04 4 $19.00 $28.75 COOKING

    TO COOK8117949391 BIG BEAR AND 08-NOV-05 5 $5.32 $8.95 CHILDREN

    LITTLE DOVE0132149871 HOW TO GET F 11-NOV-06 4 $17.85 $29.95 1.5 SELF HELP

    ASTER PIZZA9247381001 HOW TO MANAG 09-MAY-03 1 $15.40 $31.95 BUSINESS

    E THE MANAGER

    ISBN Book Title PUBDATE ID COST RETAIL DISCOUNT CATEGORY---------- ------------ --------- --- -------- -------- ---------- ------------2147428890 SHORTEST POE 01-MAY-05 5 $21.85 $39.95 LITERATURE

    MS

    14 rows selected.

    Here is a better output after formatting output:

    Instr ction on Creating a Script file and sa e as * sql file

  • 8/10/2019 Oracle_ch2.pptx

    44/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Step 1. Create SQL commands using Notepad

    Instruction on Creating a Script file and save as *.sql file

    Step 2. Click File then Save As..

    Instruction on Creating a Script file and save as * sql file (cont )

  • 8/10/2019 Oracle_ch2.pptx

    45/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Instruction on Creating a Script file and save as *.sql file (cont.)

    Step 3. Save AS Screen

    Step 4. Change from Text Document ( *.txt) to

    All Files (*.*) on Save as type box

    Instruction on Creating a Script file and save as * sql file (cont )

  • 8/10/2019 Oracle_ch2.pptx

    46/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Instruction on Creating a Script file and save as *.sql file (cont.)

    Step 5. Enter a right file name (Oracle_Ch2_Chen_Jason.sql)

    Then spool it onto the file of Oracle_ch2_Spool_Chen_Jason.txt How? See next

  • 8/10/2019 Oracle_ch2.pptx

    47/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Break !Hands-On Assignments

    In class exercise (p.54)- #2- see spool instruction on the next slide

    HW (Odd numbers only (pp.54-55), but do #1 at the end as it will affect therest of problems)

    - run the following two commands:Start c:\oradata\chapter2\JLDB_Drop.sqlStart c:\oradata\chapter2\JLDB_Build.sqlOutput should be formatted in a very good manner.

    Scripting file name: Oracle_ch2_Lname_Fname.sql

    Spooled file name: Oracle_ch2_Spool_Lname_Fname.txtEmail me with the spooled file using attachment to:[email protected] subject title ofbmis441_Oracle_ch2

    k d

  • 8/10/2019 Oracle_ch2.pptx

    48/52

    Dr. Chen, Busin ess Database Systems (Oracle)

    Homework - Hands-On Assignments

    Read and Practice all examples on Chapters 2 1. Run two script files: JLDB_Drop.sql and

    JLDB_Build.sql 2. Read Oracle assignment and create a script fileOracle_ch2_Lname_Fname.sql for odd numberquestions on Hands -on Assignments. Use appropriateCOLUMN statements to produce readable outputs

    3. Execute and test one problem a time (do #1 at the

    end, why?) and make sure they are all runningsuccessfully. Include class and personal information. 4. When you done, spool the script files (see next slide

    for spooling instructions) and email the file(Oracle_ch2_Spool_Lname_Fname.txt) to me by themidnight before the next class. Turn in a hardcopy tome in the class.

    Email me with one attachment(Oracle_ch2_Spool_Lname_Fname.) to:

    [email protected] subject title ofbmis441_Oracle_ch2

  • 8/10/2019 Oracle_ch2.pptx

    49/52

    Dr. Chen, Busin ess Database Systems (Oracle)49

    How to Spool your Script and Output FilesAfter you tested the script file of Oracle_ch2_Lname_Fname.sql successfully,

    follow the instructions below to spool both script and output files:

    Step 0. Run the following two script files from SQL*Plus (since you havecreated JLDB tables) Start c:\oradata\chapter2\JLDB_Drop.sql Start c:\oradata\chapter2\JLDB_Build.sql

    1. type the following on SQL> Spool c:\oradata\ Oracle_ch2_Spool_Lname_Fname.txt (make sure your name is entered)

    2. open Oracle_ch2_Lname_Fname.sql that you already tested 3. copy and paste all the SQL commands (including all comments) to the

    SQL*PLUS 4. type Spool Off on the SQL>

    The output should contain your personal information, all SQL commands andtheir solution on the .txt file and saved in C: drive (oradata\ folder)Email me the spooled file (.txt) with attachment to:[email protected] subject title ofbmis441_Oracle_ch2

  • 8/10/2019 Oracle_ch2.pptx

    50/52

    Dr. Chen, Busin ess Database Systems (Oracle)50

    Summary

    A basic query in Oracle 11 g SQL includes theSELECT and FROM clauses, the only mandatoryclauses in a SELECT statement

    To view all columns in the table, specify anasterisk (*) or list all of the column namesindividually in the SELECT clause

    To display a specific column or set of columns,list the column names in the SELECT clause (inthe order in which you want them to appear)

    When listing column names in the SELECTclause, a comma must separate column names

  • 8/10/2019 Oracle_ch2.pptx

    51/52

    Dr. Chen, Busin ess Database Systems (Oracle)51

    Summary (continued)

    A column alias can be used to clarify the contentsof a particular column; if the alias containsspaces or special symbols, or if you want todisplay the column with any lowercase letters,

    you must enclose the column alias in doublequotation marks (" ") Indicate the table name following the FROM

    keyword

    Basic arithmetic operations can be performed inthe SELECT clause NULL values indicate an absence of a value

  • 8/10/2019 Oracle_ch2.pptx

    52/52

    Summary (continued)

    To remove duplicate listings, include either theDISTINCT or UNIQUE keyword

    To specify which table contains the desiredcolumns, you must list the name of the table afterthe keyword FROM

    Use vertical bars (||) to combine, orconcatenate, fields, literals, and other data

    52