Docsity

Oracle SQL Practicals: A Guide to SQL*Plus and Basic SQL Commands, Study notes of Business Administration

Toggle sidebar
bg1
Page 1 of 4
These practicals will deal with Oracle and the use of one of its interfaces,
SQL*Plus.
Generally we will try to cover everything at much the same pace as in the
Lectures during the week. We will more than likely add in a few things here and
there, which will depend on how things go. Just keep all this in mind for the SQL
exam later in the year.
These notes are detailed, and will get you through a tutorial and revision, but it
will pay off to listen, as we will give some handy hints and additional information.
Also, bring your Lecture notes with you, they cover most of this stuff, and won’t
hurt. Also if you want to get a book, I recommend ā€œThe Complete Referenceā€
from Oracle press. You may as well get the latest version i.e. for Oracle 9i.
This first practical will deal with the following topics :
Basic SQL – How to structure a SQL statement, Syntax, selecting all columns, Using DESC,
Language rules, SQL Buffer, Using DISTINCT, Using ORDER BY, sequencing using ASC and
DESC.
First we will cover the SQL*Plus interface.
SQL*Plus
SQL*plus is often thought of as a kind of interactive report writer. It uses SQL to get information
from the Oracle database, and lets you create polished, well formatted reports by giving you easy
control over titles, column headings, subtotals and totals, reformatting of text and numbers.
As far as you are concerned, this product covers most of what you will be doing with Oracle.
Other Oracle Products include
• Designer – A family of tools to aid the analysis, design and generation of Oracle
applications.
• Developer - the main tools are Forms, Reports, Graphics.
However, if you know what you’re at, SQL*Plus is the only application you need to use Oracle.
In this class, you will be using only the command line interface. The menu is a waste of time.
Docsity.com
pf3
pf4

Partial preview of the text

Download Oracle SQL Practicals: A Guide to SQL*Plus and Basic SQL Commands and more Study notes Business Administration in PDF only on Docsity!

Page 1 of 4

These practicals will deal with Oracle and the use of one of its interfaces,

SQL*Plus.

Generally we will try to cover everything at much the same pace as in the

Lectures during the week. We will more than likely add in a few things here and

there, which will depend on how things go. Just keep all this in mind for the SQL

exam later in the year.

These notes are detailed, and will get you through a tutorial and revision, but it

will pay off to listen, as we will give some handy hints and additional information.

Also, bring your Lecture notes with you, they cover most of this stuff, and won’t

hurt. Also if you want to get a book, I recommend ā€œThe Complete Referenceā€

from Oracle press. You may as well get the latest version i.e. for Oracle 9i.

This first practical will deal with the following topics :

Basic SQL – How to structure a SQL statement, Syntax, selecting all columns, Using DESC, Language rules, SQL Buffer, Using DISTINCT, Using ORDER BY, sequencing using ASC and DESC.

First we will cover the SQL*Plus interface.

SQLPlus* SQL*plus is often thought of as a kind of interactive report writer. It uses SQL to get information from the Oracle database, and lets you create polished, well formatted reports by giving you easy control over titles, column headings, subtotals and totals, reformatting of text and numbers.

As far as you are concerned, this product covers most of what you will be doing with Oracle. Other Oracle Products include

  • Designer – A family of tools to aid the analysis, design and generation of Oracle applications.
  • Developer - the main tools are Forms, Reports, Graphics.

However, if you know what you’re at, SQL*Plus is the only application you need to use Oracle.

In this class, you will be using only the command line interface. The menu is a waste of time.

Page 2 of 4

SQL Syntax Structured Query Language – is a language for querying and carrying out every database operation required.

The table below lists some SQL commands :

COMMAND: DESCRIPTION: SELECT You use this command to retrieve data from the database. It is the most commonly used command. INSERT UPDATE DELETE

You use these commands to enter new rows, change existing rows and remove unwanted rows, respectively from tables in the database. (They are collectively known as DML or Data Manipulation Language ). CREATE ALTER DROP TRUNCATE

You use these commands to set up, change and remove data structures for example, tables. (They are collectively known as DDL or Data Definition Language ).

GRANT REVOKE

You use these commands to give or remove access rights to both the Oracle database and the structures within it.

SQL User Matrix. Many different users may use SQL for different tasks. The matrix shows you who may use SQL, and for what purposes.

Querying Data

Inserting/ Updating/ deleting rows.

Controlling Access

Creating, replacing, altering, dropping objects.

Guarantee Consistency and Integrity.

**Developer * ** * * * User * DBA * * ** * * Designer * * ** * ****

Writing SQL Commands :

You should remember a few simple rules and guidelines when writing SQL commands :

  • SQL commands may be entered on one or many lines
  • Clauses are usually placed on separate lines for readability and ease of editing.
  • Tabs and indents can be used to make code more readable.
  • Command words cannot be split across lines or abbreviated.
  • SQL commands are not case sensitive, unless indicated otherwise.

We will start with the SELECT statement.

Select Statements. To query a table within a database, all you need is SELECT and FROM.

To get information from a table, you need to know the table name and a column name. Imagine a table with the name ALL_OBJECTS. To get the name of the table, you can use SQL*Plus to get Oracle to ā€˜describe’ it to you….

DESC all_tables

Page 4 of 4

Using ORDER BY, ASC and DESC. To order your results from a SQL query, Alphabetically, Numerically, in Ascending or descending order you need to know the following:

SELECT column name(s) FROM table name ORDER BY column name;

Here is an example:

SELECT table_name, owner, num_rows FROM all_tables ORDER BY table_name;

The default when you use ORDER BY is to list results in ascending order, alphabetically or numerically. You do have an option and can get results in descending order if you want. There are two key words that you can use to specify what order you want, ASC for ascending or DESC for descending.

The following query will produce the same results as the last query.

SELECT table_name, owner, num_rows FROM all_tables ORDER BY table_name ASC;

To reverse the order of the results:

SELECT table_name, owner, num_rows FROM all_tables ORDER BY table_name DESC;

Try it with num_rows as well, and so on and so forth…

A Final Note on editing queries within SQLPlus:* SQLPlus can be difficult to use when you first use it. You make a mistake in the spelling of a command or column name or whatever, and sometimes it appears as if the best thing to do is start again. However, every SQL command you type in SQLplus is stored in a buffer, and can be edited while resident in the buffer. I will give you a handout soon that deals exclusively with editing and repairing queries, without all the hassle. This can take a while to get used to, and requires constant repetition before it sinks in, but makes life a lot easier.