0% found this document useful (0 votes)
179 views

Oracle/SQL Tutorial: Starting A Session

This document provides an overview of using SQLPlus to interact with Oracle databases. It discusses getting an Oracle account, starting SQLPlus sessions, changing passwords, performing basic data definition and manipulation operations like creating tables and inserting records, and more advanced topics like joins, subqueries, and complex queries. The document serves as a tutorial for learning SQL and using Oracle databases through SQLPlus.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Oracle/SQL Tutorial: Starting A Session

This document provides an overview of using SQLPlus to interact with Oracle databases. It discusses getting an Oracle account, starting SQLPlus sessions, changing passwords, performing basic data definition and manipulation operations like creating tables and inserting records, and more advanced topics like joins, subqueries, and complex queries. The document serves as a tutorial for learning SQL and using Oracle databases through SQLPlus.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Oracle/SQL Tutorial

The Oracle Relational Database Management System (RDBMS) employs the Structured
Query Language (SQL) to perform queries on databases. This tutorial focuses on the basics of
using SQLPlus in CAEN’s UNIX operating system environments.

SQLPlus is Oracle’s SQL interpreter. With it, you can perform queries, manipulate data, and execute
simple report formatting calls. Its simplicity makes it a good application to start off with when
learning SQL.

In order to use Oracle, you will need to (1) have a CAEN Oracle account, and (2) set up your
environment so that the Oracle applications (i.e., SQLPlus) can find the Oracle home directory and
server. To obtain an Oracle account, use the Contact CAEN feature on CAEN’s website to request
one.

Starting a Session
Once you have a personal or class account, execute the following command from the UNIX
command prompt to set up your environment for Oracle usage:

source /usr/caen/oracle/local/muscle

This adds a directory to your PATH variable and sets other relevant environment variables. To
invoke SQLPlus, simply type sqlplus at the UNIX shell prompt. The introduction banner will scroll
by and you will be prompted for your user name and password. After logging in, the SQL> prompt
appears.

Changing your Password


When you receive your Oracle account, CAEN will set a password for you. This password is
temporary and should be changed immediately. Oracle passwords are just as important as your AFS
password so they should not be shared. Your Oracle password should NEVER be the same as your
AFS password, because Oracle is not as secure as CAEN’s login.

You can change your password from the UNIX command prompt by using the command
opasswd. Do not use non-alphanumeric characters in your password, as Oracle treats a number of
them in a special manner.

If you would like to change it through SQLPlus, run sqlplus and enter the command:

alter user uniqname identified by password ;


You may need to put the password in double-quotes (“) if it contains special characters. Also,
changing your password with the sqlplus command causes your password to display on the screen.
Once you have changed your password, type exit in sqlplus and type clear at the UNIX shell
prompt to clear your screen.

Elements, Data Definition, and Basic Data Manipulation


Oracle is based on the Relational Model of database design in which databases are built of entities
and relationships. Roughly, an entity can be thought of as a table with rows and columns. A table is
one of the many schema objects in a database. There are also views, indices, and sequences. The
columns of a table have types associated with them. Oracle provides many types such as varchar2
(strings), number, and date. The following sequence of commands will demonstrate data definition
as well as other SQL tricks to you.

/* Help function */
help describe
/* Tabs — view of user-owned tables */
select * from tabs;
set pagesize 900
/* Table creation */
help create table
create table pup (
pupno number(2) not null,
name varchar2(10),
knlno number(2)
);
/* Finding the description of a table */
desc pup
create table x (
val date
);
select table_name from tabs;
/* Deleting a table */
drop table x;
/* Accessible tables — also see all_tables */
desc accessible_tables
select table_name from accessible_tables;
/* Keep record of your session */
spool sqltutorial
/* Creating a copy of a table from another schema */
create table kennel as select * from sqltut.kennel;
desc kennel
/* Creating a sequence */
create sequence pup_seq start with 10 increment by 1;
/* Creating a view */
create view puppynames as (
select name from pup
);
desc puppynames
/* Table DUAL — the dummy table */
desc dual
select user, sysdate, pup_seq.nextval from dual;
/* Insertion and sequence usage */
insert into pup (pupno, name, knlno)
values (pup_seq.currval, ‘fido’, 2);
insert into pup
values (pup_seq.nextval, ‘spot’, 3);
select * from pup;
/* Insertion as copy from another table */
insert into pup select * from sqltut.pup;
select * from pup;
/* Committing changes — don’t forget rollback */
commit;
/* Simple queries */
select * from pup where name = ‘spot’;
select * from pup where name in (‘spot’, ‘fido’);
select * from pup where pupno > 3;
select * from pup where pupno between 2 and 4;
select * from pup where name like ‘sp%’;

Operators/Functions/Expressions/Conditions and Data Manipulation


Like a programming language, SQL has a set of operators, functions, conditions, etc. When used in
conjunction with data manipulation functions such as select, update, insert, and delete they can be
powerful tools.

/* Set operations on tables */


select * from sqltut.baddogs;
select * from pup union select * from sqltut.baddogs;
select name from pup intersect select name from sqltut.baddogs;
/* Mathematical operations */
select abs(-25) “Absolute Val” from dual;
select ceil(13.8) “Ceiling” from dual;
select exp(1) from dual;
select floor(13.8) from dual;
select log(10, 100) “log base 10 of 100” from dual;
select 23+57 from dual;
select mod(35, 2) from dual;
select sqrt(25) + power(2,3) - round(4.645432, 1) from dual;
/* Formatting operations */
select upper(name) from pup;
select lpad(‘Page 1’, 15, ‘*_’) from dual;
select rtrim(‘Lots ‘)||’NOMORE’ from dual;
select substr(name, 1, 2), name from pup;
select translate(name, ‘aeiou’, ‘XXXXX’) from pup;
select initcap(name), length(name) from pup;
/* Counting rows */
select count(*) from pup;
REM Questions:
REM How can you find the names of the tables owned by user SYSTEM
REM and accessible to you?
REM How can you find the number of accessible tables owned by ‘SYS’?
/* Date operations */
select add_months(sysdate, 6) from dual;
select months_between(’12-DEC-82', ’03-JUL-99') from dual;
select to_char(sysdate, ‘Month DD, YYYY’) “Today” from dual;
select sysdate - to_date(‘9/1/95’, ‘MM/DD/YY’) from dual;
select max(pupno), min(pupno), avg(pupno), sum(pupno) from pup;
select * from sqltut.baddogs;
select nvl(name, ‘NO NAME’) from sqltut.baddogs;
/* Deleting and Updating */
create table test as select * from sqltut.test;
commit;
select * from test;
delete from test where string like ‘%tube%’;
select * from test;
update test set string = string||’XX’;
select * from test;
update test set string = initcap(string);
update test set string = UPPER(string) where string like ‘%ea%’;
select * from test;
rollback;
select * from test;
drop table test;
REM Questions:
REM How would you pretty-print (capitalize the first letter) of
REM the current user’s login id preceded by 20 spaces?
REM
REM How do you find the number of years between July 1, 1943 and
REM today?

Subqueries, Joins, and Complex Queries


/* Typical join */
select p.pupno, p.name, p.knlno, k.knlno, k.name
from pup p, kennel k
where p.knlno = k.knlno;
/* View based on join */
create view pupken (puppy, kennel) as (
select p.name, k.name
from pup p, kennel k
where p.knlno = k.knlno
);
/* Useful table */
select * from user_views;
select * from pupken;
/* Outer Joins */
select * from sqltut.owners;
select * from pup;
select nvl(owner, ‘Not Known’), name
from pup, sqltut.owners o
where pup.pupno = o.pupno(+);
/* Embedded SQL */
select distinct(owner), p.name
from sqltut.owners o, pup p
where p.pupno = o.pupno and
p.knlno in (select knlno
from kennel
where name like ‘%o%’);
/* Building with both */
select nvl(owner, ‘Not Known’), name
from sqltut.owners o, pup p
where p.pupno = o.pupno(+) and
p.knlno in (select knlno
from kennel
where name like ‘%o%’);
/* Stop spooling */
spool off

Additional Help
On-line manuals can be found at the CAEN web site:

http://www.engin.umich.edu/caen/wls/software/oracle/

andOracle’s:

http://www.oracle.com/

For questions not answered in these resources, email [email protected].

You might also like