34
Oracle Tutorial (CS4347) • TA Information – Name: Lidong Wu – Email: [email protected] – Office Hour: Tu. 3pm-4pm & Fr. 4pm-5pm @ ECSS 4.209

Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: [email protected] [email protected] –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Embed Size (px)

Citation preview

Page 1: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Oracle Tutorial (CS4347)

• TA Information– Name: Lidong Wu

– Email: [email protected]

– Office Hour: Tu. 3pm-4pm & Fr. 4pm-5pm

@ ECSS 4.209

Page 2: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Outlines

1. Connect Oracle Account

2. SQL Commands

Page 3: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Connect Oracle Account

Page 4: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle Account(win.)

1. You are assumed to have an account in the Unix system at UTD.

2. Oracle is available only on the machine csoracle.utdallas.edu. NOT APACHE.Login to csoracle at first.(either using PuTTY on

windows, or ssh within Mac or Linux. )

Page 5: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle Account (cont.)3a. Use putty to login csoracle.utdallas.edu

The free download is available at http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

• The hostname is csoracle.utdallas.edu

• The port is 22• The Connection type is SSH.• Enter a Saved Session Name,

then click save• Open

Page 6: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle Account (cont.)3b. Oracle SQL Developer

The free download is available at http://www.oracle.com/technology/software/products/sql/index.html.

• Enter a Connection Name, then enter your netId and password

• Set Role is default, connection type is basic

• The hostname is csoracle.utdallas.edu

• The port is 1521• The SID is student.• Click test; the status should

show up as Success.• Connect

Page 7: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle Account (cont.)

4. Run "oam.pl“ The account name is same as your netid; Don’t use the special characters for your password,

just use the letters and digitals.

Page 8: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle Account

5. Setup environment variables: Use "ps" command to check the login shell

you are using.

{csoracle:~} ps PID TTY TIME CMD 8974 pts/4 0:00 bash{csoracle:~}

Page 9: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle AccountEdit the configuration file• (csh/tcsh):add the following lines to your ".login" file:

• (sh/ksh/bash): ".bash_profile" for bash and ".profile" for sh/ksh:

* bash: add the following lines to your ".bash_profile" file

Logout the machine csoracle.utdallas.edu and login again

Run "oam.pl" if you want to change the password

if [ -f /oracle/env.sh ]then. /oracle/env.sh;fi

if ( -f /oracle/env.csh ) then source /oracle/env.csh endif

Page 10: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Run sqlplus

{csoracle:~} sqlplus

SQL*Plus: Release 11.1.0.6.0 - Production on Thu Sep 9 14:08:47 2010

Copyright (c) 1982, 2007, Oracle. All rights reserved.

Enter user-name: (netID)Enter password: (what you just set up)

Connected to:Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit ProductionWith the Oracle Label Security and Data Mining options

SQL>

Page 11: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Setup Oracle Account(Mac /Linux)

1. Use SSH to login csoracle.utdallas.edu Choose ssh protocol and csoracle.utdallas.edu as host

name

Page 12: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

If such an error occurs

Contact #2760#Email: [email protected]

SQL*Plus: Release 8.1.7.0.0 - Production on Tue Sep 2 19:06:55 2003

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

Enter user-name: yxh038100Enter password:ERROR: ORA-12541: TNS:no listenerEnter user-name:

Page 13: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

SQL Command

• create

• drop

• alter

• insert

• update

• delete

• select

• Commit

Page 14: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Table & View

Basic Structure of Table: Attribution1 Attribution2 Attribution3 … Tuple1: Attr_value Attr_value Attr_value Tuple2: Attr_value Attr_value Attr_value …Example of Relation between Table Table1(Department): Attr1(DeptNo), Attr2(DeptName), … Table2(Employee): Attr1(SSN), Attr2(DNo), …

View is a mirror of Table, no record is stored in the view. From view, you could only access a subset of data in tables.

Page 15: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Create tables

Grammar: create table Table_Name (Attribution_Name1 attribution_type,

Attribution_Name2 attribution_type, …

primary key (Attribution_Name) foreign key (Attribution_Name) reference ( Table_Name2)

);

create view View_Name as select Attribution_Name1, Attribution_Name2,… from Table_Name where condition;

Page 16: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Create tables (Cont.)

SQL> create table Merchandize (ItemID int,

ItemType char(15), Price real, primary key (ItemID));

Table created.

SQL> create view MerType as select ItemType from Merchandize where ItemID = 1;View created.

Page 17: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Create tables (Cont.)

SQL> create table Electronics(ItemID int check (ItemID <= 10), BrandName char(15), Category char(15), primary key (ItemID), foreign key (ItemID) references Merchandize);

Table created.

Page 18: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Alter Table

SQL>alter table Electronics add ItemSize int;Table altered.

Structure of Electronics: Previous: ItemID, BrandName, Category Current: ItemID, BrandName, Category, ItemSize

Page 19: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Insert record

SQL>insert into Merchandize(ItemID, ItemType, Price)values(1, 'Electronics', 39.99);1 Row created.

Update records

SQL>update Merchandize set Price=9.99 where ItemID=1 and ItemType= 'Electronics';1 Row updated.

Let’s insert a few more records now…

Page 20: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

select

SQL>select ItemID, ItemType, Price from Merchandize where price > 39.99;

SQL>select Merchandize.Price, Electronics.Category from Merchandize, Electronics where Merchandize.ItemID = Electronics.ItemID;

Page 21: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Delete all records

SQL>delete from Merchandize;1 Row deleted.

SQL>delete from Merchandize where price > 9.99;

Delete specific record(s)

Page 22: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Drop Table

SQL>drop table Merchandize;Table dropped.

Page 23: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Other useful commands

• List all tables:

• List all fields in a table

• Submit the work

SQL>select * from tab;

SQL>describe mytable;

SQL>commit;

Page 24: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Other useful commands(cont.)

• List only distinct (different) values :

• Sorts the records in ascending order by default:

SQL> select distinct ItemType from Merchandize;

SQL> select * from Merchandizeorder by ItemType;

Page 25: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Other useful commands(cont.)

• Returns the largest value of the selected column:

SQL> select max(Price) as HighestPrice from Merchandize;

Page 26: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Store & Run commands in sql files

• Edit your sql commands and save it as a file, the file extension must be .sql , the file name should end with ".sql “

• Put that file in your sqlplus working directory (it depends from where you run your sqlplus command).

• Run @myscript after login the sqlplus system

Page 27: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Examples of sql file

• Example 1: One sql command in a file

• The Table "Merchandize" will be created

>vi create.sql

create table Merchandize (ItemID char(6), ItemType char(15), Price real, primary key(ItemID));

SQL>@create;

Page 28: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Examples of sql file

• Example 2: many sql commands in one file

• Two records will insert into the table "Merchandize", then list all the records of the table " Merchandize ".

>vi myscript.sql

insert into Merchandize(ItemID, ItemType, Price) values(2,'Electronics', 39.99); insert into Merchandize(ItemID, ItemType, Price) values(4, 'Tools', 19.99);select * from Merchandize;

SQL>@myscript;

Page 29: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Save the output• Use sql command:

• All output result will be redirected to a.lst• Close output redirection: "spool off"

SQL>spool a

SQL>spool off

Page 30: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Purge Recyclebin

• Use sql command to delete the garbage:

SQL> BIN$4qjxjq0zKpPgQLAKDFxDqQ==$0 TABLE BIN$4qjxjq0mKpPgQLAKDFxDqQ==$0 TABLE BIN$4qjxjq0gKpPgQLAKDFxDqQ==$0 TABLE

SQL>purge recyclebin

Page 31: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Check the error reason

• Example:

• Logout sqlplus and run "oerr ora 942" in linux

• System will output the cause of this error

>oerr ora 942

SQL> drop table merchandize1;drop table merchandize1 *ERROR at line 1:ORA-00942: table or view does not exist

Page 32: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Review

• If you can not execute sqlplus, – Make sure you login in csoracle NOT apache– Make sure you have set oracle environment correctly

• If you can create oracle account but can not access oracle server,– Contact System administrator

• #2760• [email protected]

– TA is NOT oracle administrator.

Page 33: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Students outside the UTD’s network

• Login apache.utdallas.edu

• Type in “ssh csoracle.utdallas.edu”

Page 34: Oracle Tutorial (CS4347) TA Information –Name: Lidong Wu –Email: lidong.wu@utdallas.edu lidong.wu@utdallas.edu –Office Hour:Tu. 3pm-4pm & Fr. 4pm-5pm @

Useful links

• SQL Demo in Oracle– http://sqlzoo.net/

– http://www.w3schools.com/sql/default.asp