+ All Categories
Home > Documents > Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4...

Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4...

Date post: 03-Jan-2016
Category:
Upload: rafe-nelson
View: 218 times
Download: 4 times
Share this document with a friend
32
Using Oracle
Transcript
Page 1: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Using Oracle

Page 2: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Oracle Resides on Certain CPUs

• Computers in the GradLab• Computers in ICL4• Hilly.cs.rit.edu• Holly.cs.rit.edu• Queeg.cs.rit.edu

• Log into one of these machines to work on Oracle

Page 3: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Set up Oracle Environment Variables• source /usr/local/bin/coraenv

– (if using bash, file is oraenv)• ORACLE_SID

– Oracle system identifier

• ORACLE_HOME– Top level directory of the Oracle system hierarchy

• PATH– Path to ORACLE_HOME/bin

• Insert batch version of coraenv at the end of your .cshrc file:– The batch version is on our ClassNotes page in file oracleEnv– Example .cshrc file is on our ClassNotes page in file example.cshrc

Page 4: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Starting sqlplus

sqlplus <student>@csodb SQL*Plus: Release 9.2.0.1.0 - Production on Tue Dec

16 14:56:08 2003 Copyright (c) 1982, 2002, Oracle Corporation. All

rights reserved. Enter password: <studentPassword> Connected to:Oracle9i Release 9.2.0.4.0 - 64bit ProductionJServer Release 9.2.0.4.0 - Production SQL>

Page 5: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Change your password

SQL> passwordChanging password for chrOld password: <old password>New password: <new password>Retype new password: <new password>Password changedSQL>

Page 6: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Executing a file of SQL

SQL> @ fileOfCommands.sql

SQL> @ fileOfCommands

SQL> run fileOfCommands.sql

Page 7: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

“Getting” a file of SQLSQL> get q76.sql 1 select s.name from sailors s 2 where not exists 3 (select s2.name from sailors s2 4 where s.rating > s2.rating 5* and s2.age < 21)SQL> list 1 select s.name from sailors s 2 where not exists 3 (select s2.name from sailors s2 4 where s.rating > s2.rating 5* and s2.age < 21)SQL> /

NAME--------------------JonesMoby

SQL>

Page 8: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Editing the sqlplus bufferLIST

shows the command buffer, and makes the last line in the buffer the "current" line

LIST nprints line n of the command buffer, and makes line n the current line

LIST m nprints lines m through n, and makes line n the current line

INPUT

enters a mode that allows you to input text following the current line; you must terminate the sequence of new lines with a pair of "returns"

CHANGE /old/new replaces the text "old" by "new" in the current line

APPEND text appends "text" to the end of the current line

DEL deletes the current line

Page 9: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Using an editor to modify the sqlplus buffer

Default editor is vi:

SQL> edit

Use a different editor of your choice:

SQL> DEFINE _EDITOR = "emacs"SQL> edit

Page 10: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Recording a sqlplus session

SQL> spool fileForRecording.txtSQL> select * from BasicCustomerData;

NAME ARE PHONE ------------------------- --- -------- Chris Wilkens 206 555-1134 David Smith 303 555-5434 Donald G. Gray 705 555-1234 Fred Smathers 206 555-1234 Jack Jones 585 111 2222 Jeffrey Janes 206 555-1234 Lynda Johnson 703 555-1234 Mary Beth Frederick 303 555-5678

8 rows selected.

SQL> spool offSQL>

Page 11: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Issuing a Unix command from within sqlplus

SQL> !cat fileForRecording.txt SQL> select * from BasicCustomerData;

NAME ARE PHONE ------------------------- --- -------- Chris Wilkens 206 555-1134 David Smith 303 555-5434 Donald G. Gray 705 555-1234 Fred Smathers 206 555-1234 Jack Jones 585 111 2222 Jeffrey Janes 206 555-1234 Lynda Johnson 703 555-1234 Mary Beth Frederick 303 555-5678

8 rows selected.

SQL> spool offSQL>

Page 12: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

sqlplus helpSQL> help topics…SQL> help index…SQL> set pause onSQL> help column

COLUMN ------

Specifies display attributes for a given column, such as: - column heading text - column heading alignment - data format - column data wrapping

Also lists the current display attributes for a single column or all columns.……

Page 13: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Accessing DB metadataSQL> select table_name from user_tables;

TABLE_NAME------------------------------ARTISTART_CUSTOMERCUSTOMERCUSTOMER_ARTIST_INTINVENTORYMDC_CUSTOMERMDC_ORDER_ITEMMDC_SALES_ORDERMDC_SERVICEMI_PURCHASEMI_SHIPMENT

Page 14: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Accessing DB metadata (cont.)

SQL> describe artist; Name Null? Type -------------------------- ---------------------------- ARTISTID NOT NULL NUMBER(9) NAME NOT NULL CHAR(25) NATIONALITY CHAR(30) BIRTHDATE NUMBER(4) DECEASEDDATE NUMBER(4)

SQL>

Page 15: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Help with Oracle errorshttp://otn.oracle.com/pls/db92/db92.error_search

Page 16: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Setting linesize (default = 80)SQL> select * from artist;

ARTISTID NAME NATIONALITY BIRTHDATE---------- ------------------------- ------------------------------ ----------DECEASEDDATE------------ 1 Miro Spanish 1870 1950

2 Kandinsky Russian 1854 1900

SQL> set linesize 150;SQL> select * from artist;

ARTISTID NAME NATIONALITY BIRTHDATE DECEASEDDATE---------- ------------------------- -------------------- ---------- ------------ 1 Miro Spanish 1870 1950 2 Kandinsky Russian 1854 1900

Page 17: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Setting pagesize(default = 24 lines)

Turns off all headings, titles and page-breaks:

SQL> set pagesize 0

Sets pagesize to 50 lines per page:

SQL> set pagesize 50

Page 18: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Setting column format

SQL> column nationality format A8;SQL> select * from artist;

<12345678> ARTISTID NAME NATIONAL BIRTHDATE DECEASEDDATE---------- ------------------------- -------- ---------- ------------ 1 Miro Spanish 1870 1950

2 Kandinsky Russian 1854 1900

Page 19: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Setting personal defaults with login.sql

Contents of file login.sql in sqlplus starting directory:

set pagesize 0set linesize 190define _editor=viset serveroutput on

Page 20: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Most common Oracle data types• Varchar2

– Same as Varchar

• Char• Number( precision, scale )

– Precision = no. of digits

– Scale = no. of digits to right of decimal

• Date– DD-Mon-YY default format– TO_DATE( ’02/12/1948’, ‘MM/DD/YYYY’ )

Page 21: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

CREATE TABLE

CREATE TABLE SAILORS (SailorID NUMBER(4) Not Null, Name Char(20) Not Null, Rating NUMBER(2), Age NUMBER(3), CONSTRAINT SailorsPK PRIMARY KEY (SailorID));

CREATE TABLE SAILORS (SailorID NUMBER(4) Not Null CONSTRAINT SailorsPK PRIMARY KEY , Name Char(20) Not Null, Rating NUMBER(2), Age NUMBER(3),);

Using table-level constraints:

Using column-level constraints:

Page 22: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Adding constraints to tables

ALTER TABLE SAILORS ADD CONSTRAINT SailorsAK UNIQUE Name;

Viewing constraints

SELECT CONSTRAINT_NAME, TABLE_NAME FROM USER_CONSTRAINTS ORDER BY TABLE_NAME;

SELECT CONSTRAINT_NAME, COLUMN_NAME FROM USER_CONS_COLUMNS WHERE TABLE_NAME = ‘Sailors’;

Page 23: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Creating an Oracle Sequence(for use as surrogate key)

DROP SEQUENCE CustomerSeq;DROP SEQUENCE ArtistSeq;DROP SEQUENCE WorkSeq;DROP SEQUENCE TransSeq;

CREATE SEQUENCE CustomerSeq START WITH 1000;CREATE SEQUENCE ArtistSeq START WITH 1;CREATE SEQUENCE WorkSeq START WITH 500;CREATE SEQUENCE TransSeq INCREMENT BY 10 START WITH 100;

Page 24: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Adding data to tablesInsert Into TRANSACTION Values (TransSeq.nextval, '27-FEB-1974', 8750, '18-MAR-1974', 18500, 20000, 1003, 500);

Insert Into ARTIST Values (ArtistSeq.nextval, 'Klee', 'German', 1900, null);

Insert Into ARTIST Values( ArtistSeq.nextval, &name, &nationality, &birthdate, &deceaseddate);Enter value for name: MagooEnter value for nationality: RussianEnter value for birthdate: 1939Enter value for deceaseddate: NULL/

Using substitution variables (&)

Page 25: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

25

A note on Oracle’s table ‘DUAL’DUAL is a table owned by SYS that has only 1 row, and only 1 column called ‘dummy’. The single field contains the single character X.

To understand the SQL, note the following:SQL> select * from tab1;

ENO----------101102103

Now if you select an expression, say 1, from tab1SQL> select 1 from tab1;

1----------111

If you select an expression a+b from tab1SQL> select 'a+b' from tab1;

'A+---a+ba+ba+b

Since DUAL has only 1 row, we can convenientlyUse it to return single values:

SQL> select SYSDATE from DUAL;

SYSDATE---------08-APR-05

SQL> select 25000*.25 from DUAL;

25000*.25--------- 6250

SQL> select CustomerID.nextVal from DUAL;

NEXTVAL--------- 1020

Adapted from Indira Aramandla on http://forums1.itrc.hp.com/service/forums

Page 26: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Single-row Character FunctionsUPPER(‘Reynolds’) ‘REYNOLDS’LOWER(‘Reynolds’) ‘reynolds’INITCAP(‘carl reynolds’) ‘Carl Reynolds’

SELECT name FROM Customer WHERE UPPER(state) = ‘NY’;

SUBSTR(‘Carl Henry’, 7, 4) ‘enry’INSTR(‘Reynolds’, ‘o’) 5TRIM(‘o’ FROM ‘oh no’) ‘h n’LTRIM(‘00047.45’, ‘0’) ‘47.45’RTRIM(‘foo’, ‘o’) ‘f’LPAD(‘Main Point’, 12, ‘*’) ‘**Main Point’RPAD(‘Carl’, 10, ‘ ‘) || ‘R’ ‘Carl R’REPLACE(‘Carl Henry Reynolds’, ‘Henry’, ‘H’) ‘Carl H Reynolds’

SQL> select RPAD('Carl', 10, ' ') || 'R' from dual;

RPAD('CARL'-----------Carl R

Page 27: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Single-row Numeric Functions ROUND(3.141, 2) 3.14CEIL(3.141) 4FLOOR(3.141) 3TRUNC(99.999, 1) 99.9POWER(5, 3) 125ABS(-14) 14MOD(12, 5) 2SIGN(-48.4) -1SIGN(15) 1SIGN(0) 0

SQL> select ceil(3.141) from dual;

CEIL(3.141)----------- 4

Page 28: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Date FunctionsEXTRACT(year|month|day FROM date)EXTRACT(year FROM SYSDATE) 2006

Date1 – Date2 Number of days between

SQL> select to_date('12-dec-05') - to_date('31-oct-05') from dual;

TO_DATE('12-DEC-05')-TO_DATE('31-OCT-05')----------------------------------------- 42

ROUND(date, ‘MONTH’|’YEAR’)

SQL> select sysdate, round(sysdate,'month') from dual;

SYSDATE ROUND(SYS--------- ---------18-JAN-06 01-FEB-06

Page 29: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Displaying dates

TO_CHAR(number|date, ‘format’)

1 select sysdate, to_char(sysdate, 'Q') as quarter, 2 to_char(sysdate, 'DAY') as day, 3 to_char(sysdate, 'DDTH') as th, 4* to_char(sysdate, 'DDD') as DOY from dualSQL> /

SYSDATE Q DAY TH DOY--------- - --------- ---- ---18-JAN-06 1 WEDNESDAY 18TH 018

Page 30: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Dealing with NULL values

NVL(column, replacementValue)NVL(payoff, 0)NVL(hire_date, ’01-JAN-2006’)

NVL2(column, ifNotNull, ifNull)

SQL> select name, nvl2(deceaseddate, 'dead', 'alive') from artist;

NAME NVL2(------------------------- -----Miro deadKandinsky deadFrings deadKlee aliveMoos alive

Page 31: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Creating Aliases for Long Names

• CREATE SYNONYM <syn> FOR <whatever>;CREATE SYNONYM customer FOR jbr2389.customer;

Now:

SELECT * FROM customer

Simpler than other form:

SELECT * FROM jbr2389.customer

• DROP SYNONYM <whatever>;DROP SYNONYM customer;

Page 32: Using Oracle. Oracle Resides on Certain CPUs Computers in the GradLab Computers in ICL4 Hilly.cs.rit.edu Holly.cs.rit.edu Queeg.cs.rit.edu Log into one.

Remember:Set serveroutput on

• With serveroutput off (default):

• With serveroutput on:

• Was it successful or not?!?!?!

SQL> Execute Record_Sale( 'Dick Cheney', 'Cezanne', 'kindergarten', '8', 3550, :vReturn);

PL/SQL procedure successfully completed.

SQL> Execute Record_Sale( 'Dick Cheney', 'Cezanne', 'kindergarten', '8', 3550, :vReturn);Entered stored procedureLooking up CustomerIDLooking up ArtistIDFinding Work recordFinding Transaction recordTesting to see if a Transaction record was foundNo valid Transaction record exists. Transaction not completed.

PL/SQL procedure successfully completed.


Recommended