Upload
beverly-wade
View
214
Download
0
Embed Size (px)
Citation preview
Oracle Tutorial (CS4347)
• TA Information– Name: Lidong Wu
– Email: [email protected]
– Office Hour: Tu. 3pm-4pm & Fr. 4pm-5pm
@ ECSS 4.209
Outlines
1. Connect Oracle Account
2. SQL Commands
Connect Oracle Account
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. )
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
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
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.
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:~}
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
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>
Setup Oracle Account(Mac /Linux)
1. Use SSH to login csoracle.utdallas.edu Choose ssh protocol and csoracle.utdallas.edu as host
name
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:
SQL Command
• create
• drop
• alter
• insert
• update
• delete
• select
• Commit
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.
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;
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.
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.
Alter Table
SQL>alter table Electronics add ItemSize int;Table altered.
Structure of Electronics: Previous: ItemID, BrandName, Category Current: ItemID, BrandName, Category, ItemSize
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…
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;
Delete all records
SQL>delete from Merchandize;1 Row deleted.
SQL>delete from Merchandize where price > 9.99;
Delete specific record(s)
Drop Table
SQL>drop table Merchandize;Table dropped.
Other useful commands
• List all tables:
• List all fields in a table
• Submit the work
SQL>select * from tab;
SQL>describe mytable;
SQL>commit;
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;
Other useful commands(cont.)
• Returns the largest value of the selected column:
SQL> select max(Price) as HighestPrice from Merchandize;
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
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;
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;
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
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
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
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.
Students outside the UTD’s network
• Login apache.utdallas.edu
• Type in “ssh csoracle.utdallas.edu”
Useful links
• SQL Demo in Oracle– http://sqlzoo.net/
– http://www.w3schools.com/sql/default.asp