DAY-1 - Introduction To Oracle DB & Basic SQL
DAY-1 - Introduction To Oracle DB & Basic SQL
Important columns in any relational database's tables will be a column whose entry (customer
ID, serial number) can uniquely identify any particular item or record (the primary key), and any
column(s) that link to other tables (the foreign key(s)). The size and complexity of relational
databases typically requires stored procedures to support the relationships and provide access
(interfaces) to external programs which, for example, "query" the relational database to
retrieve and present selected data.
Relational Database Concept
ORACLE
It is a very large and multi-user database management system. Oracle is a relational database
management system developed by 'Oracle Corporation'.
Oracle works to efficiently manage its resource, a database of information, among the multiple
clients requesting and sending data in the network.
It is an excellent database server choice for client/server computing. Oracle supports all major
operating systems for both clients and servers, including MSDOS, NetWare, UnixWare, OS/2
and most UNIX flavors.
Relational Database Properties
What is table?
The data in RDBMS is stored in database objects called tables. The table is a collection of
related data entries and it consists of columns and rows.
Remember, a table is the most common and simplest form of data storage in a relational
database.
What is field?
Every table is broken up into smaller entities called fields. The fields in the CUSTOMERS table
consist of ID, NAME, AGE, ADDRESS and SALARY.
A field is a column in a table that is designed to maintain specific information about every
record in the table.
Relational Database Properties
What is record or row?
A record, also called a row of data, is each individual entry that exists in a table. For example
there are 7 records in the above CUSTOMERS table.
What is column?
A column is a vertical entity in a table that contains all information associated with a specific
field in a table.
Relating Multiples tables
The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is
a means for combining fields from two tables by using values common to each.
Relating Multiples tables
Types of joins available in SQL:
INNER JOIN: returns rows when there is a match in both tables.
LEFT OUTER JOIN: returns all rows from the left table, even if there are no matches in the right table.
RIGHT OUTER JOIN: returns all rows from the right table, even if there are no matches in the left table.
FULL OUTER JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least
one table in the SQL statement.
CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined
tables.
Communicating With a RDBMS
Using SQL
Relational Database Management
System
What is RDBMS?
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and
for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access.
A relational database is a database that has a collection of tables of data items, all of which is
formally described and organized according to the relational model. Data in a single table
represents a relation, from which the name of the database type comes. In typical solutions,
tables may have additionally defined relationships with each other.
SQL Statements
SQL statements are generally considered to be either Data Manipulation Language (DML)
statements or Data Definition Language (DDL) statements.
Data manipulation language (DML)
SELECT
INSERT
UPDATE
DELETE
MERGE
SQL Statements
Data definition language (DDL)
CREATE
ALTER
DROP
RENAME
TRUNCATE
COMMENT
SQL Statements
Data control language (DCL)
GRANT
REVOKE
Transaction control
COMMIT
ROLLBACK
SAVEPOINT
About PL / SQL
PL/SQL (Procedural Language/Structured Query Language) is Oracle Corporation's procedural
language extension for SQL and the Oracle relational database. PL/SQL is available in Oracle
Database (since version 7).Oracle Corporation usually extends PL/SQL functionality with each
successive release of the Oracle Database.
PL/SQL includes procedural language elements such as conditions and loops. You can declare
constants and variables, procedures and functions, types and variables of those types, and
triggers. You can handle exceptions (runtime errors). Arrays are supported involving the use of
PL/SQL collections. Implementations from version 8 of Oracle Database onwards have included
features associated with object-orientation. You can create PL/SQL units—procedures,
functions, packages, types, and triggers —that are stored in the database for reuse by
applications that use any of the Oracle Database programmatic interfaces.
PL/SQL Environment
SQL*Plus is the most basic Oracle Database utility, with a basic command-line interface,
commonly used by users, administrators, and programmers.
Oracle SQL Developer is a free graphical tool for database development. With SQL Developer,
you can browse database objects, run SQL statements and SQL scripts, and edit and debug
PL/SQL statements. You can also run any number of provided reports, as well as create and
save your own. SQL Developer enhances productivity and simplifies your database
development tasks.SQL Developer can connect to any Oracle Database version 10g and later
and runs on Windows, Linux and Mac OSX.
Benefits Of PL/SQL
Tight Integration with SQL
High Performance
High Productivity
Portability
Scalability
Manageability
Support for Object-Oriented Programming
Support for Developing Web Applications
Support for Developing Server Pages
Basic SELECT Statement
SQL SELECT statement is used to fetch the data from a database table which returns data in the
form of result table. These result tables are called result-sets.
Here, column1, column2...are the fields of a table whose values you want to fetch. If you want
to fetch all the fields available in the field, then you can use the following syntax:
CONNECT TODD@database_alias
Displaying Table Structure
Describe command
Syntax
DESCRIBE { table-Name | view-Name }
Description
Provides a description of the specified table or view. For a list of tables in the current schema,
use the Show Tables command. For a list of views in the current schema, use the Show Views
command. For a list of available schemas, use the Show Schemas command.
If the table or view is in a particular schema, qualify it with the schema name. If the table or
view name is case-sensitive, enclose it in single quotes. You can display all the columns from all
the tables and views in a single schema in a single display by using the wildcard character '*'.
Displaying Table Structure
Examples
To describe the view EMP_DETAILS_VIEW, enter
DESCRIBE EMP_DETAILS_VIEW
SQL *Plus Editing Commands
Recall that the previously executed commands (in current SQL*Plus session) are stored in the local buffer. One
way to change an SQL statement in the buffer is by using the line editor. The following are a list of line edit
commands.
LIST or L--Lists the contents of the buffer
LIST n or L n--Lists the contents of line number n in the buffer and makes the line current
LIST * or L *--Lists the current line
LIST m n--Lists the range from m to n line
Append text or A text--Adds to the end of the current line (e.g., "A ," adds a comma to the end of line
INPUT or I--Adds one or more lines after the current line so you can begin adding the text.
CHANGE /text--Deletes text from the current line
CHANGE /oldtext/newtext--Replaces oldtext with newtext in the current line
DEL -- Deletes the current line
SQL *Plus File Commands
SQL*Plus file command allow you to execute commands (or programs) stored in an external
file, input or output data from/to a file, and save SQL commands typed during current session.
Some SQL*Plus file commands are:
SAVE filename. This allows you to save buffer contents into a file.
START filename. This allows you to execute a batch of SQL statements stored in a file.
SPOOL filename. This allows you save SQL statements together with their outputs to a file.
GET filename. This retrieve a file and places it into the buffer.
@ filename. This allows you to execute a PL/SQL procedure(s) stored in a file.
Practice session
1. Connect schema HR through SQL Plus
2. Connect Schema HR Using SQL Developer
3. Describe the Table EMPLOYESS
4. Retrieve all rows from the Table DEPARTMENTS
5. Retrieve First_Name,Salary from the Table EMPLOYEES
Thank You