Command-line Oracle

Post on 13-Jan-2016

34 views 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 of 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)

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

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

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

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

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

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

Enter the sqlplus command

sqlplus scott/tiger@csc-srv1.lasalle.edu: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)

Result so far.

Start Notepad by entering notepad into the start search box

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

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

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

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.

Code to get rid of table if you made a mistake

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

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

Use the SQL command INSERT to add a new record.

Use SQL SELECT command to see record in table

Use SQL’s set linesize and set pagesize

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”.

Insert another record

Create another table.

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);

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.

Enter data into WorkOfArt table

Query to show art works

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;

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

Exit for SQL

The SELECT * FROM TABshows the user’s tables

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

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

INSERT again, then COMMIT, then close

This time Paul Gauguin survives

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

INSERT two artists, ROLLBACK, then query artist table

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

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.

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.

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

FAKE appears despite rollback, creates cannot be rolled back.

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.

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.

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

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

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

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