command-line oracle

46
Command-line Oracle Logon to your ORACLE account using the instructions contained in this slideshow. Create the tables with your last name in place of Smith (no spaces in table names)

Upload: alida

Post on 13-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

Command-line Oracle. Logon to your ORACLE account using the instructions contained in this slideshow. Create the tables with your last name in place of Smith (no spaces in table names). Click on Start and enter cmd into search and hit Enter. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Command-line Oracle

Command-line Oracle

Logon to your ORACLE account using the instructions contained in this slideshow. Create the tables with your last name in place of Smith (no spaces in table names)

Page 2: Command-line Oracle

Click on Start and enter cmd into search and hit Enter.

Page 3: Command-line Oracle

Click on the menu at the top and choose Properties from the drop-down list

Page 4: Command-line Oracle

Under the Colors tab, change the background color to white.

Page 5: Command-line Oracle

Under the Colors tab, change the screen text color to black.

Page 6: Command-line Oracle

Under the Layout tab, make the widths 120. Click OK.

Page 7: Command-line Oracle

Use the title command, to give your Command Window a title.

Page 8: Command-line Oracle

Enter the sqlplus command

sqlplus scott/[email protected]:1521/ORCL

In the example above scott is the username. Your username should be your usual La Salle username.

In the example above tiger is the password. Your password should be the first three letters of your username followed by an underscore followed by your La Salle id number (e.g. smi_1234567)

Page 9: Command-line Oracle

Result so far.

Page 10: Command-line Oracle

Start Notepad by entering notepad into the start search box

Page 11: Command-line Oracle

Enter the SQL DDL (Data Definition Language) code for creating a table

Page 12: Command-line Oracle

DDL Reminder

• CREATE TABLE is the SQL for making a new table, it should have a unique name

• CHAR(6) means that the ArtistID must be 6 characters long

• NOT NULL means that each record must have an ArtistID

• VARCHAR(30) means that the ArtFName can be up to 30 characters long

Page 13: Command-line Oracle

Copy the code written in Notepad and use the command prompt’s Edit/Paste to enter the code into SQLPLUS

Page 14: Command-line Oracle

Recommendation

• I recommend making a txt file with examples of all of the different types of SQL commands including the log on.

• You will be able to use it on any test we have involving Oracle. It will speed things up.

Page 15: Command-line Oracle

Code to get rid of table if you made a mistake

Page 16: Command-line Oracle

You can also change your table structure using the ALTER TABLE SQL command

Page 17: Command-line Oracle

The command describe table_name shows the fields and their and types.

Page 18: Command-line Oracle

Use the SQL command INSERT to add a new record.

Page 19: Command-line Oracle

Use SQL SELECT command to see record in table

Page 20: Command-line Oracle

Use SQL’s set linesize and set pagesize

Page 21: Command-line Oracle

SQL Display commands

• SET LINESIZE 300 makes the width of the line displaying the results of an SQL command 300 characters long

• SET PAGESIZE 0 makes the height of a page displaying the results of an SQL command “infinitely long”.

Page 22: Command-line Oracle

Insert another record

Page 23: Command-line Oracle

Create another table.

Page 24: Command-line Oracle

CREATE TABLE Smith_WorkOfArt(WorkID CHAR(6) NOT NULL, WorkTitle VARCHAR(60),WorkMedium VARCHAR(30),ArtistID CHAR(6),PRIMARY KEY (WorkID),FOREIGN KEY (ArtistID) REFERENCES

Smith_Artist);

Page 25: Command-line Oracle

SQL DDL to establish foreign key

• The last lineFOREIGN KEY (ArtistID) REFERENCES Smith_Artist);

establishes that the field ArtistID in the SmithWorkOrArt table is a foreign key that comes from the table Smith_Artist.

Page 26: Command-line Oracle

Enter data into WorkOfArt table

Page 27: Command-line Oracle

Query to show art works

Page 28: Command-line Oracle

Query joining artworks and artists

SELECT Smith_WorkOfArt.WorkTitle, Smith_Artist.ArtFName, Smith_Artist.ArtLName FROM Smith_WorkOfArt INNER JOIN Smith_Artist ON Smith_WorkOfArt.ArtistID = Smith_Artist.ArtistID;

Page 29: Command-line Oracle

A variation on the query joining artworks and artists

SELECT Smith_WorkOfArt.WorkTitle, Smith_Artist.ArtFName, Smith_Artist.ArtLName FROM Smith_WorkOfArt, Smith_Artist WHERE Smith_WorkOfArt.ArtistID = Smith_Artist.ArtistID;

Instead of INNER JOIN between table names and ON before condition, one can use a comma between table names and a WHERE before condition

Page 30: Command-line Oracle

Exit for SQL

Page 31: Command-line Oracle

The SELECT * FROM TABshows the user’s tables

Page 32: Command-line Oracle

Insert another record of artist data and Close (Not Exit)

Page 33: Command-line Oracle

Get back in and query the artist table, where’s Paul Gauguin?

Page 34: Command-line Oracle

INSERT again, then COMMIT, then close

Page 35: Command-line Oracle

This time Paul Gauguin survives

Page 36: Command-line Oracle

INSERT a new artist, then ROLLBACK, then query artists table

Page 37: Command-line Oracle

INSERT two artists, ROLLBACK, then query artist table

Both new rows disappear, rollback gets rid of things up to the last commit.

Page 38: Command-line Oracle

Another version of insert

This version of an insert allows you to name specific fields for the input data rather than relying on the order of the fields.

Page 39: Command-line Oracle

Naming the fields allows you to change their order

Above we enter the last name first, but the data goes in correctly because we named the fields.

Page 40: Command-line Oracle

Create a table, rollback, do a select query on the tab table

FAKE appears despite rollback, creates cannot be rolled back.

Page 41: Command-line Oracle

Redo ArtWork-Artist join query as RIGHT JOIN

Recall a RIGHT JOIN is a type of OUTER JOIN and will keep records that don’t have a math in the other table – in this case we have no art work by Paul Gauguin but he appears in the query results above.

Page 42: Command-line Oracle

Second version of outer join

In this version you place (+) next to the field that you may not have. We may not have the ArtistID in the WorkOfArt table, but we still want to keep the artist.

Page 43: Command-line Oracle

Place a few INSERT commands and a COMMIT into a Notepad file and save it with an sql extension

Page 44: Command-line Oracle

Go to Control Panel (Classic View) Folder Option and under View tab make sure Hide extensions is not checked

Page 45: Command-line Oracle

To run the script type the @ symbol by the path of the sql script file, make a screen capture

Page 46: Command-line Oracle

Also make a screen capture of an outer join after running your insert script. Send a document with the two screen captures.