Sei sulla pagina 1di 8

A Short Oracle Tutorial For Beginners

Introduction
This is just a brief Oracle tutorial for beginners, to provide a short history of databases and
Oracle's role in them, explain relational theory and give you an idea on how relational databases
work with a few examples. There is also a very brief discussion of object-oriented design as it
applies to databases.

Time and space don't permit an in-depth

discussion of all the features available in Oracle, but if you would like to learn more just contact
us and ask for our free Oracle tutorial mini-course.

History of Databases - From Trees To Objects


The storage and management of data is probably the biggest headache for all businesses. It has
been so for a long while and is likely to continue for a long while too. As companies try to store
more and more details about their customers and their buying habits and as regulatory
requirements for storing more data for longer, so companies will need to store and manage more
and more data. The only way this can be done at a reasonable cost is by the use of computers.

In the late 1960s/early 1970s, specialised data management software appeared - the first database
management systems (DBMS). These early DBMS were either hierarchical (tree) or network
(CODASYL) databases. These early systems were very complex and inflexible and so it adding
new applications or reorganising the data was very difficult and time-consuming.

In 1970 the relational data model was defined by E.F. Codd (see "A Relational Model of Data for
Large Shared Data Banks" Comm. ACM. 13 (June 6, 1970), 377-387). This delivered a solution
to the problems of tree and network databases due to the concept of normalisation which
involves the separation of the logical and physical representation of data.
In 1974 IBM started a project called System/R to prove the theory of relational databases. This
led to the development of a query language called SEQUEL (Structured English Query
Language) later renamed to Structured Query Language (SQL) for legal reasons and now the
query language of all databases.

In 1978 a prototype System/R implementation was evaluated at a number of IBM customer sites.
By 1979 the project finished with the conclusion that relational databases were a feasible
commercial product.

IBM's research into relational databases had also come to the attention of a group of engineers in
California who were so convinced of the potential that they formed a company called Relational
Software, Inc. in 1977 to build such a database. Their product was called Oracle and the first
version for VAX/VMS was released in 1979, thereby becoming the first commercial rdbms,
beating IBM to market by 2 years.

In the 1980s the company was renamed to Oracle Corporation. Throughout the 1980s, new
features were added and performance improved as the price of hardware came down and Oracle
became the largest independent rdbms vendors. By 1985 they boasted of having more than 1000
installations.

As relational databases became accepted, companies wanted to expand their use to store images,
spreadsheets, etc. which can't be described in 2-dimensional terms. This led to the Oracle
database becoming an object-relational hybrid in version 8.0, i.e. a relational database with
object extensions, enabling you to have the best of both worlds.

This Oracle tutorial continues in part 2 with an explanation of relational databses including a few
examples.

What is a relational database?


As mentioned before, a relational database is based on the separation and independence of the
the logical and physical representations of the data. This provides enormous flexibility and
means you can store the data physically in any way without affecting how the data is presented
to the end user. The separation

of physical and logical layers means that you can change either layer without affecting the other.
A relational database can be regarded as a set of 2-dimensional tables which are known as
"relations" in relational database theory. Each table has rows ("tuples") and columns
("domains"). The relationships between the tables is defined by one table having a column with
the same meaning (but not necessarily value) as a column in another table.

For example consider a database with just 2 tables :

emp(id number
,name varchar2(30)
,job_title varchar2(20)
,dept_id number)

holding employee information and

dept(id number
,name varchar2(30))

holding department information.

There is an implied relationship between these tables because emp has a column called dept_id
which is the same as the id column in dept. In Oracle this is usually implemented by what's
called a foreign-key relationship which prevents values being stored that are not present in the
referenced table.

Relational databases obtain their flexibility from being based on set theory (also known as
relational calculus) which enables sets or relations to be combined in various ways, including:

 join/intersection
 union (i.e. the sum of 2 sets);
 exclusive "OR" (i.e. the difference between 2 sets)
 and outer-join which is a combination of intersecting and exclusive or ing.

The intersection or join between 2 sets (in this case, tables) produces only those elements that
exist in both sets.

Therefore, if we join Emp and Dept on department id, we will be left with only those employees
who work for a department that is in the dept table and only those departments which have
employees who are in the emp table.

The union produces the sum of the tables - meaning all records in Emp and all records in Dept.
and this may be with or without duplicates.

Let's use the following data to provide specific examples:

Emp
Id Name Dept Id
1 Bill Smith 3
2 Mike Lewis 2
3 Ray Charles 3
4 Andy Mallory 4
Mandy
5 6
Randall
6 Allison White 1

Dept

Id Name
1 HR
2 IT
3 Marketing
4 Sales
5 Finance

The join of Emp and Dept. on the department id would produce the following result:

Emp.Id Emp.Name Dept.Id Dept.Name


1 Bill Smith 3 Marketing
2 Mike Lewis 2 IT
3 Ray Charles 3 Marketing
4 Andy Mallory 4 Sales
6 Allison White 1 HR

The union of Emp and Dept. would produce the following results
Id Name
1 Bill Smith
2 Mike Lewis
3 Ray Charles
4 Andy Mallory
5 Mandy Randall
1 HR
2 IT
3 Marketing
4 Sales
5 Finance

The union operator is only allowed when the number and data types of the columns in the 2 sets
are the same. It is not normally be used to combine sub sections from one or more tables rather
than entire tables.

There are other operators and variations but there isn't the space or the time to provide full details
in this short Oracle tutorial.

The later versions of Oracle (Oracle 8 onwards) support both relational and object-oriented
features. The relational features are more prominent at the moment, but this is beginning to
change. In this context an object has both attributes and methods (programs stored with the
object that performs a certain action or task) and in a true object-oriented database would belong
to a class and would allow multilevel inheritance.

SQL Commands:
SQL commands are instructions used to communicate with the database to perform specific task
that work with data. SQL commands can be used not only for searching the database but also to
perform various other functions like, for example, you can create tables, add data to tables, or
modify data, drop the table, set permissions for users. SQL commands are grouped into four
major categories depending on their functionality:

 Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are CREATE,
ALTER, DROP, RENAME, and TRUNCATE.
 Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving, modifying, and deleting data. These commands are SELECT, INSERT,
UPDATE, and DELETE.
 Transaction Control Language (TCL) - These SQL commands are used for managing
changes affecting the data. These commands are COMMIT, ROLLBACK, and
SAVEPOINT.
 Data Control Language (DCL) - These SQL commands are used for providing security
to database objects. These commands are GRANT and REVOKE.

SQL SELECT Statement


The most commonly used SQL command is SELECT statement. The SQL SELECT statement is
used to query or retrieve data from a table in the database. A query may retrieve information
from specified columns or from all of the columns in the table. To create a simple SQL SELECT
Statement, you must specify the column(s) name and the table name. The whole query is called
SQL SELECT Statement.

Syntax of SQL SELECT Statement:

SELECT column_list FROM table-name


[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];

 table-name is the name of the table from which the information is retrieved.
 column_list includes one or more columns from which data is retrieved.
 The code within the brackets is optional.

database table student_details;

id first_name last_name age subject games

100 Rahul Sharma 10 Science Cricket

101 Anjali Bhagwat 12 Maths Football

102 Stephen Fleming 09 Science Cricket

103 Shekar Gowda 18 Maths Badminton

104 Priya Chandra 15 Economics Chess

NOTE: These database tables are used here for better explanation of SQL commands. In reality,
the tables can have different columns and different data.

For example, consider the table student_details. To select the first name of all the students the
query would be like:
SELECT first_name FROM student_details;

NOTE: The commands are not case sensitive. The above SELECT statement can also be written
as "select first_name from students_details;"

You can also retrieve data from more than one column. For example, to select first name and last
name of all the students.

SELECT first_name, last_name FROM student_details;

You can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT
statement. We will discuss these commands in coming chapters.

NOTE: In a SQL SELECT statement only SELECT and FROM statements are mandatory.
Other clauses like WHERE, ORDER BY, GROUP BY, HAVING are optional.

How to use expressions in SQL SELECT Statement?


Expressions combine many arithmetic operators, they can be used in SELECT, WHERE and
ORDER BY Clauses of the SQL SELECT Statement.

Here we will explain how to use expressions in the SQL SELECT Statement. About using
expressions in WHERE and ORDER BY clause, they will be explained in their respective
sections.

The operators are evaluated in a specific order of precedence, when more than one arithmetic
operator is used in an expression. The order of evaluation is: parentheses, division,
multiplication, addition, and subtraction. The evaluation is performed from the left to the right of
the expression.

For example: If we want to display the first and last name of an employee combined together, the
SQL Select Statement would be like

SELECT first_name || ' ' || last_name FROM employee;

Output:

first_name || ' ' || last_name


---------------------------------
Rahul Sharma
Anjali Bhagwat
Stephen Fleming
Shekar Gowda
Priya Chandra

You can also provide aliases as below.


SELECT first_name || ' ' || last_name AS emp_name FROM employee;

Output:

emp_name
-------------
Rahul Sharma
Anjali Bhagwat
Stephen Fleming
Shekar Gowda
Priya Chandra

Potrebbero piacerti anche