


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Page 1 of 4
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
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 :
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.