Newgen Management Trainee: Oracle Technical Orientation Program
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
DDL
Data Definition Language CREATE ALTER DROP RENAME TRUNCATE
DML
Data Manipulation Language
DCL
Data Control Language
GRANT REVOKE
DQL
Data Query Language
SELECT
TCL
Transaction Control Language
Create table <table name> ( col 1 data type (size), col 2 data type (size), col 3 data type (size), col 4 . );
Alter Table
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:
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;
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.
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.
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.
Tuning Techniques
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 :
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
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;