0% found this document useful (0 votes)
135 views41 pages

Newgen Management Trainee: Oracle Technical Orientation Program

This document provides an overview of SQL (Structured Query Language). It describes the main components and clauses of SQL including DDL, DML, DCL, and DQL. It also covers topics such as constraints, joins, functions, grouping, indexing, and query optimization. The document is intended as training material for a new management trainee at Oracle.

Uploaded by

msatheshchendra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
135 views41 pages

Newgen Management Trainee: Oracle Technical Orientation Program

This document provides an overview of SQL (Structured Query Language). It describes the main components and clauses of SQL including DDL, DML, DCL, and DQL. It also covers topics such as constraints, joins, functions, grouping, indexing, and query optimization. The document is intended as training material for a new management trainee at Oracle.

Uploaded by

msatheshchendra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 41

NewGen Management Trainee

Oracle Technical Orientation Program

SQL

What is SQL?

Structured Query Language (SQL) is the set of statements with which all programs and users access data in an Oracle database.
The Structured Query Language is used in manipulating data stored in Relational Database Management Systems (RDBMS).

Components of SQL
DDL Data Definition Language DML Data Manipulation Language DCL Data Control Language DQL Data Query Language

TCL Transaction Control Language

DDL
Data Definition Language CREATE ALTER DROP RENAME TRUNCATE

DML
Data Manipulation Language

INSERT UPDATE DELETE

DCL
Data Control Language

GRANT REVOKE

DQL
Data Query Language

SELECT

TCL
Transaction Control Language

COMMIT ROLLBACK SAVEPOINT

Create, Alter ,Drop


Create Table

Create table <table name> ( col 1 data type (size), col 2 data type (size), col 3 data type (size), col 4 . );
Alter Table

Alter table <table name> Add col name datatype(size);


Drop Table

Drop table <table name>

Constraints
Constraints are used to prevent invalid data entry. Constraints can be classified into:

1. Domain Integrity:
It deals with Unique and Check Constraints.

2. Entity Integrity:
It deals with not null const.

3. Referential Integrity:
It deals with primary key and foreign key constraints.

Primary Key
Primary key is the combination of Unique and Not Null Constraints. Unique Constraint doesn't allow duplicate values. Not Null constraint doesn't allow null values. Primary key accepts only Unique and Not Null value. A table can have only primary key. Same column cant be designated as primary key and unique key.

Operators
Operators are used to perform some function. Operators are of 3 types:

1. Arithmetic Operators 2. Relational Operators 3. Logical Operators

Functions
SQL has provided certain functions on various types of data to enhance its capability. Functions can be classified into:

1. 2. 3. 4. 5

Numeric Functions String Functions Date Functions Conversion Functions Group Functions

Joins
A Join is a query that combines rows from two or more tables,view, or materialized views(snapshots). Oracle performs a join whenever multiple tables appear in the querys FROM clause. EquiJoin: An equijoin is a join with a join condition containing

an equality operator. It always use = operator in where condition for retrieving data from 2 or more tables. There should be at least one common column in both the tables. An EquiJoin is also Known as Natural Join

e.g. Select ename,job, deptno, dname from emp, dept where emp.deptno = dept.deptno; Join Condition: emp.deptno = dept.deptno e.g. Select ename,job,deptno,dname from emp natural join dept; (introduced in 9i)

Cartesian Product
A Join Query on two or more tables without a Join Condition is known as Cartesian Product. Each row of one table is combined with each row of the other to form a Cartesian Product.

Non-equijoin
A join query having a non-equi operator in a join condition is known as Non-equijoin. e.g. Select ename,job,sal,deptno,dname from emp,dept where emp.deptno = dept.deptno and sal>2000;

Self Join
A self join is a join of a table to itself. A join within a same table is known as self join

Outer Join
Outer Join: An outer join extends the result of equijoin. An outer join returns all rows that satisfy the join condition in any of the tables. Operator + is used along with outer join.

E.g. Select empno, ename,job, dept.deptno, dname from emp, dept where emp.deptno(+) = dept.deptno;
E.g. Select empno,ename,job,dept.deptno, dname from emp,dept where emp.deptno = dept.deptno(+);

Cross Join
Cross Join(9i) is used to generate Cartesian products. e.g. Select ename,job,dname from emp cross join dept;

e.g. Select ename,job,emp.deptno,dname from emp cross join dept;

SQL Functions
SQL has several built-in functions :

To_char function converts a number or date to a string. The syntax for the to_char function is: to_char( value, [ format_mask ], [ nls_language ] ) value can either be a number or date that will be converted to a string. format_mask is optional. This is the format that will be used to convert value to a string. nls_language is optional. This is the nls language used to convert value to a string.

To_Number()
To_number function converts a string to a number.

The syntax for the to_number function is:


To_number( string1, [ format_mask ], [ nls_language ] ) string1 is the string that will be converted to a number. format_mask is optional. This is the format that will be used to convert string1 to a number. nls_language is optional. This is the nls language used to convert string1 to a number.

To_Date
The to_date function converts a string to a date.

The syntax for the to_date function is: to_date( string1, [ format_mask ], [ nls_language ] ) string1 is the string that will be converted to a date. format_mask is optional. This is the format that will be used to convert string1 to a date. nls_language is optional. This is the nls language used to convert string1 to a date.

NVL()
The NVL function lets you substitute a value when a null value is encountered.

The syntax for the NVL function is: NVL( string1, replace_with ) string1 is the string to test for a null value. replace_with is the value returned if string1 is null.

Decode()
The decode function has the functionality of an IF-THEN-ELSE statement.
The syntax for the decode function is: decode( expression , search , result [, search , result]... [, default] ) expression is the value to compare. search is the value that is compared against expression. result is the value returned, if expression is equal to search. default is optional. If no matches are found, the decode will return default. If default is omitted, then the decode statement will return null (if no matches are found).

SELECT DECODE (value, <if this value>, <return this value>) FROM dual; Simple Decode
Ex
SELECT program_id, DECODE(customer_id, 'AAL', 'American Airlines') AIRLINE, delivered_date FROM airplanes WHERE ROWNUM < 11;

Decode()

Complex Decode
SELECT program_id, DECODE(customer_id, 'AAL', 'American Airlines', 'ILC', 'Intl. Leasing Corp.', 'NWO', 'Northwest Orient', 'SAL', 'Southwest Airlines', 'SWA', 'Sweptwing Airlines',

Group By clause
The GROUP BY clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.

The syntax for the GROUP BY clause is:


SELECT column1, column2, ... column_n, aggregate_function (expression) FROM tables WHERE predicates GROUP BY column1, column2, ... column_n; aggregate_function can be a function such as SUM, COUNT, MIN, or MAX.

Having Clause
The HAVING clause is used in combination with the GROUP BY clause. It can be used in a SELECT statement to filter the records that a GROUP BY returns.

The syntax for the HAVING clause is:


SELECT column1, column2, ... column_n, aggregate_function (expression) FROM tables WHERE predicates GROUP BY column1, column2, ... column_n HAVING condition1 ... condition_n; aggregate_function can be a function such as SUM, COUNT, MIN, or MAX.

Tuning Techniques

What is Explain Plan?


Whenever you read or write data in Oracle, you do so by issuing an SQL statement. One of Oracle's task when it receives such a statement is to build a query execution plan. An execution plan defines how Oracle finds or writes the data. For example, an important decision that Oracle has to take is if it uses indexes or not. And if there are more indexes, which of these is used. All this is contained in an execution plan. If one wants to explore such an execution plan, Oracle provides the SQL statement EXPLAIN PLAN to determine this. Syntax for Explain Plan: explain plan for sql-statement; explain plan set statement_id='some identifier' for sql-statement; explain plan set statement_id='some identifier' into table_name for sqlstatement; explain plan into table_name for sql-statement;

The Plan Table


The plan table is the table that Oracle fills when you have it explain an execution plan for an SQL statement. You must make sure such a plan table exists. Oracle ships with the script UTLXPLAN.SQL which creates this table, named PLAN_TABLE (which is the default name used by EXPLAIN PLAN). If you like, however, you can choose any other name for the plan table, as long as you have been granted insert on it and it has all the fields as here. The fields in Plan Table: The most important fields within the plan table are operation, option, object_name, id, and parent_id.

Optimizers

Types of Optimizers
Cost-Based Optimizer (CBO) In evaluating potential query plans, this optimizer makes use of available statistics to better estimate the cost of the query. Besides simply knowing the size of tables and indexes involved, it tries to estimate the selectivity of conditions involved in devising its plan. Rule-Based Optimizer (RBO) The Rule-Based Optimizer does not look at the explicit statistics, but tries to make decisions on general rules, such as push selections before joins, or prefer a sort-merge join to a nested loops join. In Oracle9i, both of these optimizers were supported, but they strongly suggested use of the CBO. In the release of Oracle10i, the RBO was no longer included. We will focus entirely on the CBO.

What is Index?
An index is a performance-tuning method of allowing faster retrieval of records. An index creates an entry for each value that appears in the indexed columns. By default, Oracle creates B-tree indexes. Create an Index The syntax for creating a index is:
CREATE [UNIQUE] INDEX index_name ON table_name (column1, column2, . column_n) [ COMPUTE STATISTICS ]; UNIQUE indicates that the combination of values in the indexed columns must be unique. COMPUTE STATISTICS tells Oracle to collect statistics during the creation of the index. The statistics are then used by the optimizer to choose a "plan of execution" when SQL statements are executed.

Types of Indexes
There are two types of indexes :

B-Tree (balanced/ binary tree) indexes Bitmap indexes


B-Tree indexes are the most commonly used because bit map indexes have a big overhead in normal operations.

Types of Indexes
B-Tree Indexes : B-Tree indexes are most appropriate when retrieving a small amount of data from a very large table. The bigger the table and the lower the number of rows that you want to retrieve then the more appropriate it use to use indexes. Bitmap Indexes : If your application is designed to query on non-selective columns, then you should consider the use of bitmap indexes, but only when the data is infrequently updated as bitmap indexes add a considerable overhead to update operations: each new value of the indexed column will require a whole new bitmap to be created and deletes and updates of the indexed column will also require the bitmaps to be adjusted, as there is one bitmap for each distinct value in the column

References
Use the below link to refer to the details on Tuning Techniques and usage of Hints. ..\Siri\Oracle's Query Optimizer.htm

Table Space

What is a Table Space?


A tablespace is a logical storage unit within an Oracle database. It is logical because a tablespace is not visible in the file system of the machine on which the database resides. A tablespace, in turn, consists of at least one datafile which, in turn, are physically located in the file system of the server. A datafile belongs to exactly one tablespace. Each table, index and so on that is stored in an Oracle database belongs to a tablespace. The tablespace builds the bridge between the Oracle database and the filesystem in which the table's or index' data is stored. There are three types of tablespaces in Oracle: Permanent tablespace Undo tablespace temporary tablespaces

Creating Tablespace
The create tablespace statement is used to create a tablespace. Permanent tablespace

create tablespace ts_something logging datafile '/dbf1/ts_sth.dbf' size 32m autoextend on next 32m maxsize 2048m extent management local; create tablespace data datafile '/home/oracle/databases/ora10/data.dbf' size 10M autoextend on maxsize 200M extent management local uniform size 64K;

Tablespace Contd
Temporary Tablespace

create temporary tablespace temp_mtr tempfile '/dbf1/mtr_temp01.dbf' size 32m autoextend on next 32m maxsize 2048m extent management local;
Undo Tablespace create undo tablespace ts_undo datafile '/dbf/undo.dbf' size 100M;

You might also like