Вы находитесь на странице: 1из 5

MIS761 Enterprise Data Management & Security

SQL Workshop Week 2

Dept. of Information Systems & Business Analytics

AIM: - To develop familiarity with basic SQL information and query commands.

TASKS: These exercises are based on the Pine Valley Furniture Company database. The sample
database has been loaded into an Oracle SQL database hosted at Deakin. All the table names have
been prefaced with “L_” to distinguish them from any other tables you may make later.

Task 1 – Accessing the database

To access the database you need to make a secure (SSH)


connection to the Deakin server interactive.deakin.edu.au

Windows users can use the PuTTY application already installed


on on-campus labs.

To find it type “PuTTY” into the search field next to windows


button.
Then select PuTTY Desktop app as shown to the left (not PuTTY
gen). If a warning dialog pops up answer “Yes”.

PuTTY is also available online for download from Deakin Software Library if
you would like to have it setup on your own PC:
http://software.deakin.edu.au/?s=PuTTY

Once PuTTY is running, a configuration window pops up. Under


“Session”, set connection settings as follows: (Host Name: interactive.deakin.edu.au)

Page 1
Apple/Mac Users:
Mac users should use the terminal application. On Deakin MacOsX machines this is at
applications → utilities → terminal.

Once in terminal type in the command


ssh <userid>@interactive.deakin.edu.au
Of course you should replace <userid> with your actual Deakin user id. So, if your userid
is janeh, then you would use the command ssh janeh@interactive.deakin.edu.au

Then the system will ask for your password. Type in your central university password and
press Enter.
Now you should be displayed with the following menu:

After logging in select the menu entry “SQLPlus to SSID.” You should see:

You will need to re-enter your password. NOTE: Oracle does not move the cursor as you
type the password. But it is listening! Just type the password and press Enter. This process
should be trouble free, however if your password contains anything other than upper and
lower case letters and numbers it may cause problems.
• If you have trouble with your password, change it to one which does not contain
anything other than upper and lower case letters and numbers at
https://www.deakin.edu.au/password/change/ after you change the password, it will
take some minutes to update.

Further help is available at http://www.deakin.edu.au/its/publications/document/helpsoftware-ssid-unix.pdf


Page 2
Task 2 – The structure of the database

A database is defined as a collection of tables which relate to a particular application. In


order to use the database it is useful to know exactly what tables exist. Enter the command
SELECT table_name FROM user_tables;

You should get a response similar to the following (may not be exact):

Note that the table names may differ slightly between students.

• You’ve probably worked out by now that there’s a lot of repetitive typing in this level of
database querying. You might like to keep a copy of a text editor (such as notepad)
open to copy and paste commands you use a lot.

Similarly, one can describe individual tables. This can be very useful if you’ve forgotten
exactly what you defined in the first place, or where you’ve forgotten the type of an
attribute (or similar lapse in memory).

Let’s describe the table L_customer. This is done using the command
DESC L_customer;

You should get a response like the following:

The information you receive includes the following:


• The name of each column in the table;
• Its data type and the width of each column in terms of the maximum number of
characters a field may contain; and
• Whether Null values are allowed or not;

Page 3
Task 3 – Entering and editing SQL commands

We need to not only describe our database, but we also need to submit commands to either
change the databases content or structure, or to execute queries (questions). To do this we
need to enter SQL queries.

Enter the following query:

SELECT * FROM L_customer;


• Note that all queries must end in a semicolon ‘;’ if you enter a query and nothing
happens, check for the semicolon and press enter again!

You’ll notice the output looks messy as database rows are wrapped around into the following
line. To solve this issue set the line width to 132 characters (default is 80):
set linesize 132;

Now try displaying the L_customer table again:


SELECT * FROM L_customer;
You should see a table that is easier to read.
Add the phrase order by 2 to the query and run it again:
SELECT * FROM L_customer order by 2;

Notice that this time the information is sorted by customer_name (the second column). The
same effect can be obtained by using the phrase order by customer_name. Try it and check.

Task 4 – Selecting information from one table

You are now ready to try out some SQL queries yourself. If you don’t finish these in your lab
session, it is up to you to finish them before next time. Initially we will concentrate on typing
queries and receiving results. Later you will be expected to formulate the queries yourself. In
this task we will concentrate on one table – customer.

Select all data from customer

SELECT customer_id, customer_name, customer_address, customer_city,


customer_state, postal_code
FROM L_customer;

Select all data from customer (alternative)


SELECT *
FROM L_customer;

Select all data from customer (selecting only some columns)


SELECT customer_name, customer_city, customer_state
FROM L_customer;

Select all data from customer (selecting another column order)


SELECT customer_state, customer_city, customer_name
FROM L_customer;

Page 4
Select states recorded (note duplicates in resulting table)
SELECT customer_state
FROM L_customer;

Select states recorded (removing duplicates)


SELECT DISTINCT customer_state
FROM L_customer;

At this point, you might have noticed that I’m using a bit of a convention on how to write an
SQL query:
SELECT <columns>
FROM <tables>

Writing the queries this way, helps to understand what you are doing.

The output from these queries is more than a little messy, but remember that you are
working at the real database coalface – there are other programs and output interfaces
which make the output look better. Most serious applications (DSO, Star, bank systems) and
some not so serious (Facebook, YouTube) have SQL behind them. We can do some basic
preparing of the data using SQL though:

It is possible to force the output so that it is sorted by particular column(s) using the
ORDER BY clause.

SELECT customer_name, customer_state


FROM L_customer
ORDER BY customer_name;

Ordering rows for a resulting table (alternative – ascending is the default; ‘1’ says column 1
of the resulting table)

SELECT customer_name, customer_state


FROM L_customer
ORDER BY 1 ASC;

Ordering rows for a resulting table (descending)


SELECT customer_name, customer_state
FROM L_customer
ORDER BY customer_state DESC;

Ordering rows using several columns


SELECT *
FROM L_customer
ORDER BY customer_state, customer_city, postal_code;

Formulate SQL queries for the following-


1. Display the name, state, and city (in that order) for each customer.
2. For each customer list the name, state, city, and postal code. Order the results on
postal code.
3. Assignment Question (Your tutor cannot help with this question):
For each customer list the name, city and state in that order. The results should be
ordered by the city in the reverse alphabetical order (Z to A).
Take a screenshot (Press Alt+PrintScreen in Windows) ensuring that the SQL command as
well as the resulting table is clearly captured. Save it for the assignment.

Page 5

Вам также может понравиться