oracle

28
SQL Tutorial •CPSC 608 •Database System

Upload: tsbarath

Post on 15-Nov-2014

441 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Oracle

SQL Tutorial

•CPSC 608

•Database System

Page 2: Oracle

Connecting to Oracle

• Your username is identical to your UNIX account

• To access your Oracle account, you need to change the initial password assigned to by the system

• To access Oracle you need to be logged onto a Solaris 2.5 workstation

• You need to add the following line :

source /usr/local/bin/SystemLogin in your .login file

Page 3: Oracle

Connecting to Oracle

To generate a new password, enter the command “orapasswd” at the unix prompt.

This program will generate a new Oracle password for you

>orapasswd

The new password for

unix-username@oracledatabase is WU8vn23r

Page 4: Oracle

Connecting to Oracle

When connecting to SQL use the full Oracle login as given by the orapasswd command as follows:

>sqlplus

SQL*Plus: Release 8.1.5.0.0 - Production on Wed Feb 9 18:54:47 2000

(c) Copyright 1999 Oracle Corporation. All rights reserved.

Enter user-name: mua9330@oracledatabase

Enter password:

Connected to:

Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production

With the Partitioning and Java options

PL/SQL Release 8.1.5.0.0 - Production

SQL>

Page 5: Oracle

Connecting to Oracle

You can change the password to your own liking by typing the following command at the command prompt

SQL> alter user <unix-username> identified by <new - password>

You are now ready to begin work in SQL environment

To exit just type quit or exit at the command prompt

SQL> exit

Note: When a user exits the system automatically commits all changes

Page 6: Oracle

On line help

• Oraview online help in Xwindows environment

At the prompt the user can enter two types of command

• SQL statements to access database

• SQL*Plus commands,for editing and storing SQL statements and formatting query results.

To get online help for SQL*Plus commands

SQL> help [topic]

Page 7: Oracle

Some Useful SQL commands

To set pause at the beginning of the each page

SQL> set pause on;

To view operating system files without getting out of SQL

SQL>!ls

Page 8: Oracle

Database Relation/Table Operations

• Create table

• describe

• alter table

• insert into

• drop table

Page 9: Oracle

Create table

create table Course(

Cno char(3) not null,

Cname varchar2(22),

Cdescp varchar2(25),

Cred number(5),

Clabfee number(5,2),

Cdept char(3)

);

Page 10: Oracle

Important Data type

char(n): fixed -length character string of length n. Max = 255

varchar2(n): variable length character string having a maximum of n characters. Max = 2000

date: holds a date field. Usually specified in string format, such as ‘12 - DEC- 1999’

number(n,d): real value occupying n spaces with d digits after the decimal point.

Integer(n): integer value occupying n spaces.

Page 11: Oracle

Example

create table Weather(

city varchar2(13),

noon number(3,1),

midnight number(3,1),

precipitation number,

primary key (city,precipitation)

);

create table Zipcodes(

zip number(5) primary key,

city varchar2(30)

);

Page 12: Oracle

Example

create table Customer(

cno number(5) primary key,

cname varchar2(30),

street varchar2(30),

zip number(5) refference zipcodes,

phone char(12)

);

Page 13: Oracle

describe• SQL> create table Course(• Cno char(3) not null,• Cname varchar2(22),• Cdescp varchar2(25),• Cred number(5),• Clabfee number(5,2),• Cdept char(3)• );

• Table created.

• SQL> describe Course• Name Null? Type• ----------------------------------------- -------- ----------------------------• CNO NOT NULL CHAR(3)• CNAME VARCHAR2(22)• CDESCP VARCHAR2(25)• CRED NUMBER(5)• CLABFEE NUMBER(5,2)• CDEPT CHAR(3)•

Page 14: Oracle

Table Modifying• Alter table Course modify(

Cdept char(6)

);

• alter table Course

drop Cdescp;

• drop table Course;

• To see the list of all the tables that you have created

select table_name, table_type from user_catalog;

• To insert a row into a table

insert into Course values

(‘614’, ‘Computer Architecture’, ‘Pipelining and memory’, 3,150.00, ‘th’);

Page 15: Oracle

Deletion

• To delete a particular row from the table

delete from Course where Cname = ‘intro to cs’

• To update a particular column value for a particular row

update Course set Clabfee = clabfee*10 where cdept = ‘phil’

Page 16: Oracle

The select statement

• Basic form

select <attribute list>

from < table list>

where <condition>

Page 17: Oracle

SQL queries

Query 1: Display entire course table.

Select *

from Course;

Page 18: Oracle

Note

The changes made to the database using insert, delete and update can be reversed if necessary. Oracle SQL provides two commands :

Commit & rollback.

The changes made to the database can be made permanent by using commit. The changes made to the database since last commit can be reversed using roll back.

There are some actions that force commit. One is exit. Others are create table, drop table, alter table

Page 19: Oracle

Comparison Operators

• = equal to

• < less than

• > greater than

• <= less than or equal to

• >= greater than or equal to

• !=, ^=,<> not equal

Page 20: Oracle

Query

Display all information about any course with a zero labfee.

Select *

from Course

where Clabfee=0.00

Page 21: Oracle

Query

Display all information about courses having course name values which follows “Database” in alphabetic sequence.

select *

from course

where Cname > ‘Database’;

Page 22: Oracle

Sorting the result Table

Display the entire Course table. Sort the output display by the clabfee values

select *

from Course

order by Clabfee;

Page 23: Oracle

Sorting the result Table

Display the department id and name of every course. Sort the result by department id. Within each department, sort by course name.

Select Cdept, Cname

from Course

order by Cdept, Cname;

Page 24: Oracle

Boolean Connectors: AND - OR - NOT

Display all information about any three credit philosophy course which has a lab fee which is strictly between zero and one hundred dollars.

Select *

from Course

where Clabfee > 0 and Clabfee < 1000 and Cdept = ‘phil’ and Cred = 3;

Page 25: Oracle

Logical test against a list of values

Display the course number, description and credit for any course which is worth 2, 6, or 9 credits.

Select Cno, Cdescp, Cred

from Course

where Cred in (2,6,9);

Page 26: Oracle

Logical test against a list of values

Display the course name and lab fee of any course with a lab fee less than $100 or greater than $200.

Select Cname, Clabfee

from Course

where Clabfee not between 100 and 200;

Page 27: Oracle

Pattern Matching

When you wish to retrieve information from rows having similar but not necessarily equal, values in a given column.

The format is:

where column-name like pattern

Two special wildcard

• “-” : represent exactly one character

• “%” : represent character string of any length (including zero)

Page 28: Oracle

Query

Display the course number and name of all introductory course.

Select Cno, Cname

from Course

where Cname like ‘intro%’;