oracle 10g imp notes of dinesh

36

Upload: dinch

Post on 16-Nov-2014

814 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: Oracle 10g IMP Notes of Dinesh
Page 2: Oracle 10g IMP Notes of Dinesh
Page 3: Oracle 10g IMP Notes of Dinesh

What is SQL & What is Useful For Oracle ?

Using SQL you can communicate with the oracle server. SQL has the following advantages: * Efficient. * Easy to learn and use. * Functionally complete with SQL you can define , retrieve and manipulate data inthe tables. * SQL is a command language for commuication with the oracle sever from any tool or application.

The Oracle Server Provides an open comprehensive and integrated approach to information management an oracle server consists of an oracle database and an oracle instant every time a database is started in the System Global Area - SGA is allocated and oracle background processes are also started the System Global Area is an area of memory used for database information shared by the database users the combination of the background processes and memory buffers is called and oracle instance.

SQL Statements

DQL - Data Querying Language Select

DML - Data Manipulation Language Insert , Update , Delete , Merge

DDL - Data Definition Language Create , Alter , Drop , Truncate

DCL - Data Control Language Grant , Revoke

TCL - Transaction Control Language Commit , Rollback , Savepoint

For Set Line Write this Command SQL> Set Lines 130 (Number n)

Projection - Choose the columns in a table that you want returned by your query. Selection - Choose the rows in a table that you want returned by your query. Joining - To bring together data that is stored in different tables by creating a link between them.

Select - Identifies what columns From - Identifies which tables * - Star Retrieves all data ; - Semicolon execution eg:- Select * from Tab ;

Arithmetic Expressions Low Priority High Priority Add + Subtract - Multiply * Divide /

Create expressions with Number and Date by using Arithmetic Operators. Same priority is evaluated from left to right.

Using Column Aliases Alias - AS " " eg:- Select last_name as "Name" , commission_pct comm from employees ;

Using Concatenation Operater ( || ) Two Vertical Bars eg:- Select last_name||job_id as "employees" from employees ;

Page 1

Page 4: Oracle 10g IMP Notes of Dinesh

Using Literal Character Strings ( ' ' ) Single Quotation Marks eg:- Select last_name || ' is a ' || job_id as "Employees Details" from employees ; eg:- Select last_name || ' :- Salary = ' || salary as " Monthly Salary " from employees ;

Using Eliminating Duplicate Rows DISTINCT Keyword eg:- Select Distinct Department_id,Job_id from employees ;

Using to Display Table Structure Describe Desc eg:- Describe employees eg:- Desc employees

Using the Where Clause Where is Retrireing Selected Rows Character Strings and Date Values are enclosed in single quotation marks The default date Format is DD-MON-RR Where condition(s) ; eg:- Select employee_id, last_name, job_id, department_id From employees Where department_id = 90 ; eg:- Select last_name, job_id, department_id From employees Where last_name = 'Whalen' Where hire_date = '01 -jan-95' Where salary >= 6000 Where expr operator value's ;

Using Comparison Conditions > - Greater Than >= - Greater Than or Equal To = - Equal To <= - Less Than or Equal To < - Less Than != , ^= , <> - Not Equal To eg:- Select * from employees where column_name all Comparison Conditions ;

Using Other Comparison Conditions Between....And - Between two values inclusive eg:- Select last_name, salary from employees Where salary Between 2500 And 3500 ;

In ( Set ) - Match any of a list of values eg:- Select employee_id, last_name, salary, manager_id from employees Where manager_id in (100, 200, 201) Where last_name in ('Hartstein','vargas') ;

Like % _ Escape - Match a character pattern is call wildcard characters eg:- Select first_name from employees where first_name Like 'S%' ; Select last_name from employees where last_name Like '_O%' ; Select employee_id, last_name, job_id from employees Where job_id Like '%Sa\_%' escape '\' ;

Is Null - is a Null Value Is Not Null - is a Null Value eg:- Select last_name, manager_id from employees where manager_id is null ; // is Null Select last_name, job_id, commission_pct from employees where commission_pct is Not Null ; // is Not Null

Page 2

Page 5: Oracle 10g IMP Notes of Dinesh

Using Logical Conditions

And - Returns True If both compoent conditions are True And Requires both conditions to be True. eg:- Select employee_id, last_name, job_id, salary from employees where salary >= 10000 and job_id Like '%man' ;

Or - Returns True If either compoent condition is True or False Or Requires one condition True. eg:- Select employee_id, last_name, job_id, salary from employees where salary >= 10000 or job_id like '%Man%' ;

Not - Returns True If the following condition is False. eg:- Select last_name, job_id from employees Where job_id Not In ('IT_prog','St_Clerk','Sa_Rep') Where job_id Not In ('Ac_Account','Ad_Vp') Where salary Not Between 10000 And 15000 Where last_name Not Like '%A%' Where commission_pct Is Not Null

Rules of Percedence

0 - Parenthese around the expressions you want to calculate first 1 - Arithmetic operators 2 - Concatenation operator 3 - Comparison conditions 4 - Is [Not] , Null , Like , [Not] In 5 - [Not] Between 6 - And Logical condition 7 - Or Logical condition 8 - Not Logical condition Using Order by Asc/Desc

Sort rows with the order by clause order by :- Asc : Ascending order default Desc : Desceding order There order by clause comes last in the select statment always at last. eg:- Select last_name, job_id, department_id, hire_date from employees order by hire_date ; eg:- Select employee_id, last_name, salary*12 annsal from employees order by annsal ; eg:- Select employee_id, last_name, salary*12 annsal from employees order by 2 ; eg:- Select last_name,department_id,salary from employees order by department_id,salary desc ;

Page 3

Page 6: Oracle 10g IMP Notes of Dinesh

SQL FunctionsTwo Type of SQL Functions

Single - Row Functions ÈMultiple - Rows Functions

These Function operate on single row These Functions can manipulate groups only and retruns one result per row. of rows to give one result per group of rows.

Characters

NumberDate

Conversion General

Function_Name (arg1,arg2)

Functions

Character Functions General Functions Date Functions Number Functions

NVL Months_Between Round NVL2 Add_Months Trunc NULLIF Next_Day Mod Coalesce Last_Day Case To_Char Decode To_Date To_Number

Case-Manipulation Character Manipulation Functions Functions Lower Concat Upper Substr Initcap Length Instr Lpad | Rpad Trim Replace

Single-Row Functions * Manipulate data items * Accept arguments and return one value * Act on each row returned * Return one result per row * May modify the data type * Can be nested * Accept arguments which can be a column or an expression Function_Name[(arg1,arg2,....)] * Can be used in select, where, and order by clauses; can be nested.

In the Syntax : Function_Name is the name of the function. arg1,arg2 is argument to be used by the function this can be represented by a column name or expression.

Page 4

Page 7: Oracle 10g IMP Notes of Dinesh

Character Functions

Case Manipulation Functions

Lower - Converts alpha character values to lower case. eg:- Select Lower(first_name) from employees ;

Upper - Converts alpha character values to upper case. eg:- Select Upper(last_name) from employees ;

Initcap - Coverts alpfa character values to upper case for the first letter of each world, all other letters in lower case. eg:- Select Initcap(job_id) from employees ;

Character Manipulation Functions

Concat - Joins values together (You are Limited to using two parameters with concat) eg:- Select Concat(last_name,first_name) from employees ;

Substr - Extracts a string of determined length. eg:- Select Substr(last_name,1,3) from employees ;

Length - Shows the length of a string as a numeric value. eg:- Select Length(first_name) from employees ;

Instr - Finds numeric position of a named character. eg:- Select Instr(last_name,'A') from employees ;

Lpad - Pads the character value right justified. eg:- Select Lpad(salary,10,'*') from employees ;

Rpad - Pads the character value left justified. eg:- Select Rpad(salary,10,'*') from employees ;

Trim - Trim heading or trailing character (or both) from a character string. If trim_character or trim_source is a character literal, you must enclose it in sigle quotes. eg:- Select Trim('H' from last_name) from employees ;

Replace - Replace the character from stringf which you want. eg:- Select Replace(last_name,'A','O') from employees ;

Number Functions Dual is a Dummy Table.

Round - Rounds values to specified decimal Round(45.926,2) --> 45.93 eg:- Select Round (45.923,2), Round (45.923,0), Round (45.923,-1) From Dual ; Trunc - Truncates values to sepcified decimal Trunc(45.926,2) --> 45.92 eg:- Select Trunc (45.923,2), Trunc (45.923,0), Trunc (45.923,-1) From Dual ;

Mod - Returns remainder of division Mod (1600,300) --> 100 eg:- Select last_name, salary, Mod(salary,500) From employees Where job_id = 'SA_Rep' ;

Page 5

Page 8: Oracle 10g IMP Notes of Dinesh

Working With Dates * Oracle Database Stores dates in an internal numeric format :- Centurys - Years - Months - Days - Hours - Minutes - Seconds * The default data display format is DD-MON-RR * The default display input format for and date is DD-MON-RR. Vaild oracle dates are between January 1,4712 B.C and December 31,9999 A.D * This data is stored intenally as follows :- Centurys - Years - Months - Days - Hours - Minutes - Seconds 19 94 06 07 5 10 43 :- June 7th, 1994 5:10:43 PM

Sysdate function will use for seeing Date and Time You can use Arithmetic with Dates + - * / eg:- Select Sysdate from Dual ;

Using Date & Time Functions

Months_Between - Number of months between two dates. Months_Between ('01-sep-95','11-jan-99') => 19.774194

Add_Months - Add calendar months to date. Add_Months ('11-jan-94',6) => '11-jul-94'

Next_Day - Next day of the date specified. Next_Day ('01-sep-95','Friday') => '08-sep-95'

Last_Day - Last day of the month. Last_Day ('01-feb-95') => '28-feb-95'

Fmt - If the format model Fmt is omitted, date is rounded to the Nearest Day. All eg:- Select employee_id,hire_date, Months_Between (sysdate,hire_date) , Add_Months (hire_date,2) , Last_Day (hire_date) From employees Where hire_date like '%94%' ;

Assume Sysdate = ' 25-jul-95 ' ;

Round (sysdate,'month') => 01-aug-95 Round (sysdate,'year') => 01-jan-96

Trunc (sysdate,'month') => 01-jul-95 Trunc (sysdate,'year') => 01-jan-95

eg:- Select employees_id,hire_date,Round(hire_date,'month'),Trunc(hire_date,'month') From employees where hire_date like '%97' ; eg:- Select Trunc (sysdate - hire_date) as days, Trunc (months_between(sysdate,hire_date)/12) as years, Trunc (mod(months_between(sysdate,hire_date),12)) as months From employees ;

Page 6

Page 9: Oracle 10g IMP Notes of Dinesh

Conversion Function Data Types Conversion

Implicit Data Type Conversion Explicit Data Type Conversion Varchar2 or Char To Number Varchar2 or Char To Date Number To Varchar2 Date To Varchar2

Explict Data Type Conversion Functions FM : Has an element to remove padded blanks or suppress leading zeros. FX Modifer

To_Char ( Data , ' Format ' ) eg:- Select employee_id, To_Char(hire_date,'MM/YY') as Month_Hired from employees Where last_name = 'Higgins' ; eg:- Select last_name,To_Char(hire_date,'FmDD Month YYYY') as Hiredate from employees ; eg:- Select last_name,To_Char(Hire_date,'Fmdd month yyyy fmHH:MI:SS Am') from employees ;

To_Date ( Data , ' Format ' ) eg:- Select employee_id, To_Date(hire_date,'FmDD Mon YYYY) as Hiredate From employees Where last_name = 'Higgins' ;

To_Number ( Data , ' Format ' ) eg:- Select employee_id, salary, To_Number(salary,'999.99') as Salary from employees ;

RR Date Format Oracle have one main function saving date and time by oracle it self in century when you insert any record it take default date and time but in other way like DD-Mon-RR is the format of oracle. Day-Month-Century Year Hrs Min Sec .This DD-Mon-RR is Nice to by time zone also and it show the year in which your are working like in 21 Century of 2009. DD-Mon-RR eg:- Select last_name, To _Char(hire_date,'DD-Mon-YYYY) From employees Where hire_date < To_Date ('01-Jan-90','DD-Mon-RR) ; eg:- Select last_name, To_Char(hire_date,'DD-Mon-YYYY) From employees Where To_Date(hire_date,'DD-Mon-YY') < '01-Jan-1990' ;

Page 7

Page 10: Oracle 10g IMP Notes of Dinesh

Format elements of vaild Date, Time and Number Formats :-

Date Format :

SCC or CC - Century; Server perfixes B.c Date with- YYYY or SYYYY - Year; Server prefixes B.C Date with- YY or YY or Y - Last Three, Two or One Digits of year Y,YYY - Year with comma in this position IYYY,IYY,IY,I - Four, Three, Two or One Digits year based on the ISO Standard SYEAR or YEAR - Year spelled out; Server prefixes B.C Date with- BC or AD - B.C./.D Indicator B.C or A.D - B.C./A.D. Indicator with periods Q - Quarter of year MM - Month: Two Digit value Month - Name of month padded with blanks to length of nice characters Mon - Name of month, Three letter abbreviation RM - Roman numeral month WW or W - Week of Year or month DDD or DD or D - Day of year, month or week DAY - Name of day padded with blanks to a length of nine characters DY - Name of day; Three letter abbreviation J - Julian day; The number of days sinec 31 December / . , m- Punctuation is reproduced in the result " of The " - Quoted string is reproduced in the result TH - Ordinal number (for eg:- DDth for 4th) SP - Spelled our number (for eg:- DDsp for Four)

Time Format :

AM or PM - Meridian indicator A.M or P.M - Meridian indicator with period HH or HH12 or HH24 - Hours of day or Hours (1-12) or Hours (0-23) MI - Minute (0-59) SS - Second (0-59) SSSS - Second past minight (0-86399)

Number Fromat : 9 - Numeric position (Number of 9s determine display width) eg:- 9999 - 1234 0 - Display leading zeros eg:- 09999 - 001234 $ - Floating Dollar sign eg:- $99999 - $1234 L - Floating Local currency symbol eg:- L99999 - FF1234 . - Decimal point in position specified eg:- 9999.99 - 1234.00 , - Comma is position specified eg:- 99,999 - 1,234 MI - Minus sign to right (negative values) eg:- 99999MI - 1234- PR - Parenthesize negative numbers eg:- 99999PR - <1234> EEEE - Scientificnotation (format must specified four Es) eg:- 99.999EEEE - 1.234E+03 V - Multiply by 10n times (n=number of 9s after V eg:- 999v99 - 123400) B - Display zero values as blanks, Not 0 eg:- B9999.99 - 1234.00

Page 8

Page 11: Oracle 10g IMP Notes of Dinesh

Nesting Functions * Single-row function can be nested to any level. * Nested functions are evaluated from deepest level to the least deep level.

F3 ( F2 ( F1 ( Col , Arg1 ) , Arg2 ) , Arg3 ) Step1=Result1 Step2=Result2 Step2=Result3

eg:- Select last_name, NVL(To_Char(Manager_id),'No Manager') from employees Where manager_id Is Null ; eg:- Select To_Char(Next_Day(Add_Months(hire_date,6),'Friday')'fmday, month DDth, YYYY') as "Next 6 Month Review" from employees order by hire_date ;

General Function

These function work with any Data type and pertain to using Null.

NVL - Converts a Null value to ab actual value. eg:- Select last_name, salary, NVL(commission_pct,0), (salary*12)+(salary*12*NVL(commission_pct,0)) as Ann_sal from employees ; eg:- Select last_name, salary, commission_pct, (salary*12)+(salary*12*commission_pct) as Ann_sal from employees ;

NVL2 - If expr1 is not null, NVL2 returns expr2 If expr1 is Null,NVL2 returns expr3. The argument expr1 can have any data type. eg:- Select last_name, salary, commission_pct, NVL2(commission_pct,'sal+com','sal') as Income From employees Where department_id in (50,80) ;

NULLIF - Compares two expressions and returns Null if they are equal, or the first expression if theyare not equal. Note:- The NULLIF Function is logically equivalent to the following case expression. The case expression is discussea in a subsequent pages Case when expr1=expr2 Then Null else expr1 end eg:- Select first_name, length(first_name) as "expr1", last_name, length(last_name) as "expr2", NULLIF(length(first_name),length(last_name)) as Result from employees ;

COALESCE - Returns the first Non-Null expression in the expression list eg:- Select last_name, COALESCE(commission_pct,salary,10) as comm from employees order by commission_pct ; Note:- The advantage of the COALESCE functions over the NVL function is that the COALESCE function can take multiple alternate values.

Coditional Expressions

* Provide the use of IF-Then-Else within a SQL Statment * Use Two Methods Case Expression

Case expr When comparison_expr1 Then return expr1 When comparison_expr2 Then return expr2 When comparison_exprn Then return expn Else else_expr End eg:- Select last_name,job_id,salary, Case job_id When 'IT_Prog' Then 1.10*salary When 'ST_Clerk' Then 1.15*salary When 'SA_Rep' Then 1.20*salary Else salary End "Revised Salary" From employees ;

Page 9

Page 12: Oracle 10g IMP Notes of Dinesh

Decode Function

Decode (column|expression,search1,result1[,search2,result2,....,][,default]) eg:- Select last_name, job_id, salary, Decode (job_id, 'IT_Prog' , 1.10*salary , 'ST_Clerk' , 1.15*salary , 'SA_Rep' , 1.20*salary , salary ') "Revised Salary" From employees ; eg:- Select last_name, salary, Decode (Trunc(salary/2000,0), 0,0.00, 1,0.09, 2,0.20, 3,0.30, 4,0.40, 5,0.42, 6,0.44, 0.45) "Tax Rate" From employees Where department_id = 80 ;

Displaying Data From Multiple Tables Sometimes you need to use data from more than one tables. To avoid a cartesian product, always include a vaild join condition in a where clause. Cartesian product = 20 X 8 = 160 Rows eg:- Select last_name, department_name from employees,departments ; There will be created a cartesian product result.

Types of JoinsOracle Proprictary Joins (8i & Prior): SQL:1999 Compliant Joins:

Equijoin = Cross JoinsNon Equijoin Between...And Natural Joins

Outer Join (+) Using Clasuse Self Join Full or Two Side Outer Joins

Aribitray Joins Conditions For Outer Joins

Oracle Proprictary Joins (8i & Prior):

Equijoin Note. Equijoin are also called simple joins or inner joins eg:- Select table1.column, table2.column From table1 alias, table2 alias Where table1.column1 = table2.column2 ; eg:- Select employee_id.emp,last_name.emp,department_id.dept,location_id.dept From employees emp,departments dept Where emp.department_id = dept.department_id ; eg:- Select c.last_name,d.department_name,l.city From employees e,departments d,locations l Where e.department_id = d.department_id and d.location_id = l.location_id ;

Non Equijoins Note. Retrieving records with non-equijoins eg:- Select e.last_name,e.salary,j.grade_level From employees e,job_grades j Where e.salary Between j.lowest_sal And j.highest_sal ;

Outer Joins Note. The outer join operater si the plus sign(+) eg:- Select e.last_name,e.department_id,d.department_name From employees e,departments d Where e.department_id(+)=d.department_id ;

Self Joins Note. Joining a table to it self eg:- Select worker.last_name||' Works for '||manager.last_name From employees worker,employees manager Where worker.manager_id = manager.employee_id ;

Page 10

Page 13: Oracle 10g IMP Notes of Dinesh

SQL:1999 Compliant Joins: Use a Join to query Data from more than one table. Select table1.column,table2.column From table1 [Cross Join table2] | [Natural Join table2] | [Join table2 Using (column_name)] | [Join table2 On (table1.column_name = table2.column_name)] | [Left | Right | Full Outer Join table2 On (table1.column_name = table2.column_name)] ;

Cross Joins Note. The cross join clause produces the cross-product of two tables this is the same as a cartesian product between the two tables. eg:- Select last_name,department_name From employees Cross Join department ;

Natural Joins Note. The Natural Joins clause is based on all columns in the tables that have the same name. The join can happen only on columns having the same names and data types in both the tables. But if the columns have the same name,but different data types, then the Natural Join syntax causes an error. eg:- Select department_id,department_name,location_id,city From departments Natural Join locations ;

Joins with the Using clause Note. If sereral columns have the same names but the data types do not match, the Natural Join clause can be modified with the using clause to specify the columns that should be used for an equijoin. eg:- Select e.employee_id,e.last_name,d.location_id From employees e Join departments d using (department_id) ;

Creating Three-way Joins with the On Clause. eg:- Select employee_id,city,department_name From employees e Join departments d On d.department_id = e.department_id Join locations l On d.location_id = l.location_id ;

Left Outer Join eg:- Select e.last_name,e.department_id,d.department_name From employees e Left Outer Join department d On (e.department_id = d.department_id) ; // like a plus sign (+)

Right Outer Join eg:- Select e.last_name,e.department_id,d.department_name From employees e Right Outer Join department d On (e.department_id = d.department_id) ; // like a plus sign (+)

Full Outer Join eg:- Select e.last_name,e.department_id,d.department_name From employees e Full Outer Join department d On (e.department_id = d.department_id) ;

Page 11

Page 14: Oracle 10g IMP Notes of Dinesh

Group Functions What are Group Functions ? Group Function operate on sets of rows to give one result per group.These sets may be the whole table or the table split into group

Group Functions Group by Having AVG Count Max Min Stddev Sum Variance

eg:- Select [column,] group function (column),... From table [Where condition] [Group by column] [Order by column] ;

Using Group Function

AVG - Average value of n,ignoring null values eg:- Select AVG(salary) from employees ;

Count - Count number of all rows in table there are eg:- Select Count(*) from employees ;

Max - Maximum value in any datatype eg:- Select Max(salary) From employees ; Min � Miniman value in any datatype eg:- Select Min(salary) From employees ;

Stddev � Standard deviation in only numerice eg:- Select Stddev(salary) Form employees ;

Sum � Sum in only numerice eg:- Select Sum(salary) From employees ;

Variance � Variance in only numerice eg:- Select Variance(salary) From employees ;

Note. You can use Distinct keyword in this function. All group function ignore Null values in the column you can use other like NVL , NVL2 etc...

Creating Group Function Group by Function is Divide rows in a table into smaller groups by using the group by clause

Using the Group by clause eg:- Select department_id, AVG(salary) From employees Group by department_id ; eg:- Select department_id,job_id,Sum(salary) From employees Group by department_id,job_id ;

Using the Having clause Use the Having clause to restrict Groups eg:- Select department_di,Max(salary) From employees Group by department_id Having Max(salary) > 10000 ;

Note. Column aliases are used for the queries.

Page 12

Page 15: Oracle 10g IMP Notes of Dinesh

Subquery can be wirten single-row and multiple-row sub queries. eg:- Select select_list From table Where expr operater (Select select_list From table) ; eg:- Select last_name From employees Where salary > (Select salary From employees Where last_name = 'Abel' ) ;

Type of Subqueries Note. The outer and inner queries can get data from defferent tables.

Single-Row Subquery � Return only one row. Use Single-row comparison operators (< , <= , = , >= , > , <>) eg:- Select last_name, job_id From employees Where job_id = (Select job_id From employees Where employee_id = 141) ;

Multiple-Row Subquery � Return more than one row. Use Mutiple-row comparison operators (IN , ANY, ALL) eg:- Select last_name, salary, department_id From employees Where salary IN (Select Min(salary) From employees Group by department_id) ; eg:- Select employee_id, last_name, job_id, salary From employees Where salary < Any (Select salary From employees Where job_id = 'IT_Prog') and job_id <> 'IT_Prog' ;

Substitution Variables Temporarily Store Values - Single Ampersand (&) - Double Ampersand (&&) - Define Command Pass Variable values between SQL Statements Dynamically alter headers and footers

Using the (&) Substitution Variable Use a variable prefixed with an Ampersand (&) to prompt the user for a value. eg:- Select employee_id, last_name, salary, department_id From employees Where employee_id = &employee_num ; Note. You can use also in caharacter and date values like '&first_name' or '&date'

Defining Substitution Variables Define Variable = Values Define and Undefine Commands eg:- Define job_title = � IT_Prog� Undefine job_title sp2 � 0135: sysmbol job_title is undefined eg:- Select employee_id, last_name, salary, department_id From employees Where employee_id = &employee_num ; Define employee_num = 200Note. If i will not put any value inside that variable it will take define value.

Using the && Substitution Variables Using the Double Ampersand (&&) If you want to rouse the variable value without prompting the user each time. eg:- Select employee_id, last_name, job_id, &&column_name From employees Order by &column_name ; The same thing will pass to order by when will you enter the value.

Using the Verify Command Use the verify command to toggle the display of the substitution variable, before and after iSQL*Plus replaces substitution variables with values. eg:- Set Verify No Select employee_id,last_name,salary,department_id From employees Where employee_id = &employee_num ; � employee_num � = 200 Old 3: Where employee_id = &employee_num New 3: Where employee_id = 200 Set Verify Off

Page 13

Page 16: Oracle 10g IMP Notes of Dinesh

Use Set Commands to control current session eg:- Set system_variable values To see all set variable values, use the show all Command eg:- Show All eg:- Ttitle No/Off Ttitle 'string' Btitle 'string'

Data Manipulating Language Data Manipulating Language (DML) is a core part of SQL when you want to add, update or delete data in the database, you execute a DML Statement, A collection of DML Statement that from a logical unit of work is called a transaction (TCL).

DML Data Manipulating Language - Insert Delete - - Merge Update -

Insert Insert into table [(column[,column...])] Values (values[,value...]) ; eg:- Insert into employees ( employee_id, first_name , last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id ) Values ( 113, 'Louis', 'Popp', 'Lpopp', ' 515.124.4569', sysdate, 'Ac_Account', 6900, Null, 205, 100 ) ; Insert statement with a subquery eg:- Insert into sales_reps (id,name,salary,commission_pct) Select employee_id,last_name,salary,commission_pct From employees Where job_id like '%Rep%' ; Note. Do Not use the Values Clause. eg:- Insert into copy_emp Select * From employees ;

The Update Statement Modify existing rows with the update statement Update Update table_name Set column = value [,column = value,... ] [Where condition(s)] ; Specific row or rows are modified if you specify the where clause. eg:- Update employees Set department_id = 70 Where employee_id = 113 ; // 1 rows updated All rows in the table are modified if you omit the where clause. eg:- Update copy_emp Set department_id = 110 ; // 22 rows updated Updating two columns with a subquery update employee 114's job and salary to match that of employee 205 eg:- Update employees Set job_id = (Select job_id From employees Where employee_id = 205), salary = (Select salary From employees Where employee_id = 205) Where employee_id = 114 ; // 1 row updated error :- Update employees Set department_id = 55 Where department_id = 110 ; update * employees error at line 1: ORA � 02291: integrity constraint (hr.empl_dept_pk) violated � parent key not found

Page 14

Page 17: Oracle 10g IMP Notes of Dinesh

The Delete Statement You can remove existing rows from a table by using the delete statement. Delete [From] table_name [Where Condition(s)] ; Deleting rows from a table specific rows are deleted if you specify the where clause. eg:- Delete From departments Where department_ name = 'Finance' ; 1 row deleted All rows in the table are deleted if you omit the where clause. eg:- Delete From copy_emp ; 22 rows deleted Deleting rows based on another table use subqueries in delete statements to remove rows from a table based on values from another table. eg:- Delete From employees Where department_id = (Select department_id From departments Where department_name like '%Public%') ; 1 row deleted. Deleting rows integrity constraint error error:- Delete From departments Where department_id 60 ; Delete From * departments error at line 1: ORA � 02292: Integrity constraint (Hr.emp_dept_fk) Voilated � child record found

Using a Subquery in an Insert Statement Insert into (Select employee_id, last_name, email, hire_date, job_id, salary, department_id From employees Where department_id = 50 with check option) Values (99998,'smith','james',to_date('07-jun-99','DD-Mon-RR'),'ST_clerk',500) ; error:- Insert into * error at line 1: ORA � 01402: View with check option where � clause violation.

Using Explicit Default Values Default with Insert Insert into departments (department_id,department_name,manager_id) Values (300,'Engineering',Default) ; Note. If no default values for the correspading column has been specified, oracle set the column to Null value. eg:- Update departments Set manager_id = Default Where department_id = 10 ;

The Merge Statment * Provides the ability to conditionally update or Insert data into a database table. * Performs an update the row exites, and an Insert if it is a new row - Avoids separate updates - In creases performance and case of use - Is useful in data warehousing appliactions The merge Statement you can conditionally insert or updata rows in a table by using the merge statement. syntax: Merge Into table_name table_alias Using (table|view|sub_query) alias On (join conditions) When Matched Then Update Set col1 = col_val1, col2 = col2_val When Not Matched Then Insert (column_list) values (column_values) ;

Page 15

50

Page 18: Oracle 10g IMP Notes of Dinesh

Merging Rows Insert or Update rows in the copy_emp table to match th employees. eg:- Create Table copy_emp as Select * From employees ; Delete From copy_emp ; Merge Into copy_emp c Using employees e On (c.employee_id = e.employee_id) When Matched Then Update Set c.first_name = e.first_name, c.last_name = e.last_name, c.email = e.email, c.phone_number = e.phone_number, c.hire_date = e.hire_date, c.job_id = e.job_id, c.salary = e.salary, c.commission_pct = e.commission_pct, c.manager_id = e.manager_id, c.department_id = e.department_id, When Not Matched Then Insert Values ( e.cemployee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id, e.salary, e.commission_pct, e.manager_id, e.department_id ) ; Select * From copy_emp ;

Transaction Control Language You can control the logic of transaction by using the Commit, Savepoint and Rollback Statement. TCL Transaction Control Language Commit � Savepoint � Rollback Commit � end the current transaction by making all pending data changes permanet Savepoint Name � marks a savepoint with in the current transaction Rollback � rollbsck ends the current transaction by discarding any changes and or savepoints created after the savepoints to which you are rolling back if you omit the to savepoints clause the rollback statement rolls back the entire transations. As savepoints are logical, there is no way to list the savepoints you have created. Note. Savepoint is not ANSI Standard SQL .

Controlling Transactions Savepoint, Commit and Rollback with only DML Statment Insert, Update, Delete, Merge.

Time Commit Transaction Delete Statement Savepoint A Insert Statement Update Statement Savepoint B Insert Statement Commit

Commit Rollback Rollback Rollback it will save to savepoint B to saveepoint A it will full Rollback save all changes it will Rollback it will Rollback savepoint B up to savepoint A

Page 16

Page 19: Oracle 10g IMP Notes of Dinesh

eg:- Select * From employees ; Insert into departments Values(290,'corporate Tax',Null,1700) ; 1 row inserted Delete From employees Where employee_id = 9999 ; 1 row deleted Commit ; commit complete or Rollback ; rollback complete

Creating and Managing Tables Database Object Objects Description Table => Basic unit of storage; composed of rows and columns � Data stores View => Logically represents subsets of data from one or more tables � subset of data from one or mare table. Sequence => Numeric value generator Index => Improves the performance of some queries Synonym => Gives alternative names to objects

DDL Data Defination Language Create � Alter Drop � Truncate Table � Basic unit of storage composed of rows and columns � Data stores

The Create Table Statement You Moust Have Varchar2 - Create Table Privilage Varchar - A Storage Area Char Number Create Table [schema.] table (column datatypes [Defailt expr] [,...]) ; You specify Table name column name, column datatype, and column size What is schema referencing aother user's table, A sehema is a collection of object � schema object are the logical Structure that directly refer to data in a database, schema object include tables, views, synonyms, sequences, stored, procedures, indexes, clusters and database links. If a table does not belog to the user, the ower's name must be prefired to the table. For example if there is a schema named user_b, and user_b has an employees table then specify the following to redrive data from that table. eg:- Select * From user_b.employees ; The Default Option * Specify a default value for a column during an insert eg:- , , , , hire_date Date Default Sysdate, . . . . * Literal values expressions, or SQL Function are legal values * Anthor columns name or a pseudo column are illegal values * The default datatype must match the columns datatypes. Createing Table eg:- Create Table dept ( deptno number(2), dname varchar2(14), loc varchar2(13) ) ; Table Created. Confirm Table Creation Describe dept

Tables in the Oracle Database * User Tables : - Are a collection of tables created and maintained by the user - Contain user informations * Data Dictionary : - Is a collection of table created and maintained by the oracle srever - Contain database information

Page 17

Page 20: Oracle 10g IMP Notes of Dinesh

Data dictionary tables are owaned by the sys user. The base tables are rarely accessed by the user beause the information in them is not easy to understand. Therfore, users typically access data dictionary views information stored in the data dictionary includes names of the oracle server users, privileges granted to users, database object names, the constraints, and auditing information. There are four categories of data dictionary views; each categories has a distinct prefix that refliects its interded use. Prefix Description User_ - These views contain information about object owned by the user.

All_ - These views contain information about all of the tables (object tables and relational tables) accessible to the user.

DBA_ - These views are restircted views which can be accessed only by user who have been assigned the DBA role.

V$ - These views are dynamic performance views database server performance, memory and locking

eg:- Select table_name From user_tables ; // See the names of tables owened by th users. eg:- Select Distinct object_type From user_objects ; // View distinct object types owned by the users. eg:- Select * From user_catalog ; // View tables views,synonyms and sequences owned by the users.

Data Types Prefix Description Varchar(size) Variable length character data Char(size) Fixed length character data Number(p,s) Variable length numeric data Date Data & Time Jan 1,4712,BC and Dec 31,9999,AD Long Variable legth character data up to 2 Gigabytes Clob Character data up to 4 Gigabytes Raw and Long Raw Raw binary data Blob Binary data up to 4 Gigabytes Bfile Binary data stored in an external file up to 4 Gigabytes RowID A-64 base number system representing the unique Note. A long column is not copied when a table is created using a sub query.

Date & Time Data types New Date & Time enhancements with oracle 9i * New Date & Time data types have been introduced * New data type storage is available * Enhancements have been made to time zones and local time zone Prefix Description Timestamp Data with fractional seconds Interval Year to Month Stored as an interval of years and months Interval Day to Second Stored as an interval of days to hours minutes and seconds

* The Timestamp datatype is an extension of the date data type. * It stores the year, month and day of the date data type, plus hours, minute and second values as well as fractional second value. * The timestamp data types is specified as following: Timestamp [(Fractional_seconds_precision)] :- 17-Jan-87 12.00.00.00000 AM eg:- Create Table Time ( No_id Number(2), Name Varchar2(15), DateTime Timestamp(7) ) ;

Page 18

Page 21: Oracle 10g IMP Notes of Dinesh

Timestamp with time zone data type * Timestamp with Time Zone is a variant of timestamp that includes a time zones displacement in its value. * The Time Zone displacement is the difference, in hours and minutes, between local time and UTC. Timestamp [(Fractionl_seconds_precision)] with Time Zone eg:- Timestamp '1999-04-15 8:00:00 -8:00' Timestamp '1999-04-15 11:00:00 -5:00' Timestamp '1999-04-15 8:00:00 US/Pacific' ; Timestamp with local time data type * Timestamp with local time zone is another variant of timestamp that includes a time zone displacement in its value. * Timestamp with local time zone datatype is specified as followns: Timestamp [(Fractional_seconds_precision)] with local Time Zone

Interval year to month data type * Interval year to month stores a period of time using the year and month date & time fields Interval year to month data type Interval year [(year_precision)] to month eg:- Interval '123-2' year(3) to month Interval '123' year(3) Interval '300' month(3) Interval day to second data type * Interval day to second stores a period of time in terms of day, hours, minutes and seconds Interval day [(day_precision)] To second [(Fractionl_seconds_precision)] eg:- Interval '4 5:12:10:222' day to second(3)

Creating a table by using a subquery * Create a table and insert rows by combining the create table statement and the As Subquery option. Create Table table_name [(column,column...)] As Subquery ; eg:- Create Table dept80 As Select employee_id, last_name, salary*12 annsal, hire_date From employees Where department_id = 80 ; Desc dept80

The Alter Table Statement Add � Modify � Drop

Use the Alter table statement to add, modify or drop columns.

* Alter Table table_name Add (column datatype [Default expr] [,column datatype]...) ;

* Alter Table table_name Modify (column datatype [Default expr] [,column datatype]...) ;

* Alter Table table_name Drop (column ) ;

Note. If a table already contains rows when a column is added, then the new column is initially null for all the rows. Use the Add clause to Add columns eg:- Alter Table dept80 Add (job_id varchar2(9)) ; Table Altered. You can change a columns datatype , size and default value eg:- Alter Table dept80 Modify (last_name varchar(30)) ; Table Altered. Use the Drop column clause to drop columns you no longer nedd from the table eg:- Alter Table dept80 Drop column job_id ; Table Altered.

Page 19

Page 22: Oracle 10g IMP Notes of Dinesh

The Set Unused option * You use the set unused option to mark one or more columns as unused. * You use the Drop unused columns option to remove the columns that are marked as unused. Alter Table table_name Set Unused (column) ; Alter Table table_name Set Unused column, column ; Alter Table table_name Drop unused columns ; eg:- Alter Table dept80 Set Unused (job_id) ; eg:- Alter Table dept80 Drop Unused job_id ; Dropping a Table * All data and structure in the table is deleted. * Any pending transaction are committed. * All indexes are dropped. * You connot roll back the drop table statement. Drop Table table_name ; eg:- Drop Table dept80 ; Table Dropped. Changing the Name of an object * To change the name of a table,view,sequence or synonym you execute the rename statement Rename dept To detail_dept ; Table Renamed.

Truncating a Table * The Truncate Table statement - Removes all rows from a table - Releases the storage space used by that table Truncate Table detail_dept ; Table Truncated. * You connot roll back row removal when using Truncate. * Alternatively, you can remave rows by using the delete statement.

Adding Comments to a Table * You can add comments to a table or column by using the comment statement. eg:- Comment On Table employees Is 'Employee Information' ; Comment Created. * Comment can be viewed through the data dictionary views - All_Col_Comments - User_Col_Comments - All_Tab_Comments - User_Tab_Comments Comment On Table table_name | column table.column Is 'Text' ;

Including Constraints What are Constraints ? * Constraints enforce rules at the table level. * Constraints prevent the deletion of a table if there are dependencies. * The Following constraints types are vaild Code Constraints Description NNK Not Null Specifies that the column connot contain a null value. UK Unique Specifies a column or combination of columns whose values must be unique for all rows in the table. PK Primary Key Uniquely identifies each row of the table. FK Foreign Key Establishes and enforeces a foreign key relationship between the column and a column of the refernced table. Check Specifies a condition that must be true.Note. * Name a constraint or the oracle server generates a name by using the sys_cn format. * View a constraint in the data dictionary. * You con view the constrants defined for a specific table by lookig at the User_Constraints data dictionary table.

Page 20

Page 23: Oracle 10g IMP Notes of Dinesh

Defining Constraints Create Table [Schema.] table ( column datatype [ Default [ column_constraint ] , expr ] .... [ table_constraint ] [ ,... ] ) ; eg:- Create table employees ( employee_id Number(6), first_name Varchar(20),.... job_id Varchar2(10) Not Null Constraint emp_emp_id_pk primary key (employee_id) ) ; * Column constraints level column [Constraint constraint_name] constraint_type, * Table constraint level column,.... [constraint constraint_name] constraint_type(column,....), The Not Null Constraint Is defined at the column level Create Table employees(employee_id Number(6), last_name Varchar2(25) Not Null, salary Number(8,2), commission_pct Number(2,2), hire_date Date Constraint emp_hire_date_nn Not Null ; The Unique Constraint * Defined at either the table level or the column level : Create Table employees( employee_id Number(6), last_name Varchar2(25) Not Null, email Varcher2(25), salary Number(8,2), commission_pct Number(2,2), hire_date Data Not Null,..... Constraint emp_email_uk Unique (email) ; The Primary Kry Constraint * Defined either the table level or the column level : Create Table departments( department_id Number(4), department_name Varchar2(30) Constraint dept_name_nn Not Null, manger_id Number(6), location_id Number(4), Constraint dept_id_pk Primary Key (department_id) ) ; Note. A Unique Index is automatically created for a primary key column. The Foreign Key Constraint * Defined at either the table level or the column level : Create Table employees( employee_id Number(6), last_name Varchar2(25) Not Null, email Varchar2(25), salary Number(8,2), commission_pct Number(2,2), hire_date Date Not Null,.... department_id Number(4), Constraint emp_dept_fk Foreign Key (department_id) References departments (department_id), Constraint emp_email_uk Unique(email) ) ; Note. Foreign Key constraint can be defind at the column or table constraint level. A composite Foreign Key must be created by using the table level defination.

Foreign Key Constraint Keyworkds * Foreign Key : Defines the colums in the child table at the table constraint level. * References : Identifies the table and column in the parent table. * On Delete Casdade : Deletes th dependent rows in the child table when a row in the parent tables is deleted. * On Delete Set Null : Converts dependent foreign key values to null. Note. Without the on delete cascade or the on delete set null options the rows in the parent table cannot be deleted if is referenced in the child table.

Page 21

System Will Give Default Name

User Given Name

Page 24: Oracle 10g IMP Notes of Dinesh

The Check Constraint * Defines a condition that each row must satisfy * The following expressions are not allowed : - References th Currval , Nextval , Level and Rownum pseudocolumns. - Calls th Sysdate , UID , User and UserENV Functions. - Queries that refer to other values in other rows. ......, salary Number(2) constraint emp_salary_min Check (salary > ) ,....... Adding a Constraint Use the Alter Table statements * Add or Drop a constraint, but not modify it structures. * Enable or Disable constraints. * Add a Not Null constraint by using the modify clause. Alter Table table_name Add [constraint constraint_name] type (columns) ; Note. You can define a Not Null column only if the table is empty or if the column has a value for every row. Add a Foreign Key Alter Table employees Add Constraint emp_manager_fk Foreign Key (manager_id) References employees (employee_id) ;

Dropping a Constraint * Remove the manager constraint from the employees table eg:- Alter Table employees Drop Constraint emp_manager_fk ; * Remove the Primary Key eg:- Alter Table departments Drop Primary Key Cascade ;

Alter Table table_name Drop primary key | unique [column] | Constraint constraint_name [Cascade] ; Disabling Constraints * Execute the Disable clause of the Alter Table statement to deactivate an integrity constraint. * Apply the cascade option to disable dependent intergrity constraints. eg:- Alter Table employees Disable Constraint emp_emp_id_pk Cascade ;

Enabling Constraints * Activate an integrity constraint currently disabled in the table definition by using the Enable clause eg:- Alter Table employees Enable Constraint emp_emp_id_pk ;

Cascading Constraints * The cascade constraints clause is used along with the drop columns clause. * The cascade constraints clause drops all referntial integrity constraints that refer to the primary and unique key defined on the dropped columns. * The cascade constraints clause also drops all multicolumn constraints defined on the dropped columns. Note. This statements illustrates the usage of the cascade constraints cluase.Assume table Test1 is created as following:

Page 22

Page 25: Oracle 10g IMP Notes of Dinesh

eg:- Create Table Test1 (PK Number Primary Key, FK Number, Col1 Number, Col2 Number, Constraint FK_constraints Foreign Key(FK) References Test1, Constraint CK1 Check (PK > 0 and Col1 > 0), Constraint CK2 Check (Col1 > 0) ) ; An Error is returned for the following statements. Alter Table Test1 Drop (PK) ; - - pk is a parents key Alter Table Test1 Drop (Col) ; - - Col Check Cascading Constraints eg:- Alter Table Test1 Drop (PK) Cascade Constraints ; eg:- Alter Table Test1 Drop (PK,FK,Col1) Cascade Constraints ;

Viewing Constraints Query the User_constraints table to view all constraints definitions and names eg:- Select constraint_name, constraint_type, search_cindition From User_Constraints Where table_name = 'employees' ; Note. Constraints that are not named by the table owner receive the system assigned constraint name, In constraint type, C stands for Check, P for Primary Key, R for Referential integrity Foreign Key, U for Unique Key. Note. That Not Null constraint is really a check constraint. Viewing the columns associated with constraints View the columns associated with the constraint names in the User_Cons_Columns view. eg:- Select constraint_name,column_name From User_Cons_Columns Where table_name = 'employees' ; Creating Views * Views � Logically represents subsets of data from one or more tables * You embed a subquery within the create view statement Create [Or Replace] [Force|NoForce] View view_name [(alias[,alias]....)] As Subquery [With Check option [Constraint constraint_name] ] [With Read Only [Constraint constraint_name] ] ; * The subquery can contain complex select. Create View eg:- Create View empvu80 As Select employee_id , last_name , salary From employees Where department_id = 80 ; View Created. Create View with Aliases in Subquery eg:- Create View salvu50 As Select employee_id Id_Number, last_name Name, Salary*12 Ann_Salary From employees Where department_id = 50 ; View Created.

Page 23

Page 26: Oracle 10g IMP Notes of Dinesh

eg:- Create View Salvu50 (Id_Number,Name,Ann_Salary) As Select employee_id, last_name, salary*12 From employees Where department_id = 50 ; View Created. Select * From salvu50 ; Note. You can See all Views in user account from User_Views Modifying a View * Modify the empvu80 view by using create or replace view clause. Add an alias for each column name. eg:- Create or Replace View empvu80 (id_number,name,sal,department_id) As Select employee_id , first_name || ' ' || last_name, From employees Where department_id = 80 ; View Created. * Column aliases in the create view clause are listed in the same order as the columns in the subquery. Creating a Complex View * Create a complex view that contains group function to display values from two tables. eg:- Create View dept_sum_vu (name, minsal, maxsal, avgsal) As Select d.department_name, Min(e.salary), Max(salary), Avg(salary) From employees e , departments d Where e.department_id = d.department_id Group by d.department_name ; View Created. Select * From dept_sum_vu ;

Using the with check option clause * You can ensure that DML operations performed on the view stay within the domain of the view by using the with check option clause. eg:- Create or Replace View empvu20 As Select * From employees Where department_id = 20 With Check Option Constraint empvu20_ck ; View Created. Using the with Read Only eg:- Create or Replace View empvu10 (employee_number, employee_name, job_title) As Select employee_id, last_name, job_id From employees Where employee_id = 10 ; Removing a View * You can remove a view without losing data because a view is based on underlying tables in the database. Drop View view_name ; eg:- Drop View empvu80 ; Inline Views An Inline views is a subquery with an alias (or correlation name ) that you can use with in a SQL statement. eg:- Select a.last_name, a.salary, a.department_id, b.maxsal From employees a , (Select department_id max(salary) maxsal From employees Group by department_id) b Where a.department_id = b.department_id and a.salary < b.maxsal ;

Page 24

Page 27: Oracle 10g IMP Notes of Dinesh

Top � N Analysis * Top � N Queries ask for the N largest or smallest values of a column. For example: - What are the ten best selling products ? - What are the ten worst selling products ? * Both largest values and smallest values set are considered Top � N Queries. Performains of Top � N Analysis The high-level structure of a Top � N Analysis Query is :- Select [column_list], Rownum From (Select [column_list] From table_name Order by Top � N_column) Where Rownum <= N ; eg:- Select Rownu, as Rank, last_name, salary From (Select last_name, salary From employees order by salary Desc) Where Rownum <= 4 ; eg:- Select Rownum as Senior, e.last_name, e.hire_date From (Select last_name, hire_date From employees order by hire_date) e Where Rownum <= 4 ; Other Database Obejects Sequence � Generates primary key values. Index � Improves the performance of same queries. Synonym � Alternative name for an object

What is a Sequence ? A Sequence * Automatically genarates unique numbers * Is a sharable objects * Is typically used to create a primany key values * Replaces application code * Speeds up the efficiency of accessing sequence values when cached in memeory

The Create Sequence Statement Define a sequence to generate sequential numbers automatically: Create Sequence [Incerment by n] [Start with n] [{Maxvalue n | Nomaxvalue}] [{Minvalue n | Nominvalue}] [{Cycle | Nocycle}] [{Cache n | Nocahe}] ; Create Sequence Create Sequence dept_deptid_seq Increment by 10 Start with 120 Maxvalue 9999 Nocache Nocycle ; Sequence Created. Conforming Sequences View User_Sequence Select sequence_name, min_value, max_value, increment_by, last_number From User_sequence ; Nextval and Currval Pseudocolumns * Nextval � Returns a the next available sequence values. * Currval � Obtains the current sequence value. * Nextval must be issued for that sequence before currval containes values.

Page 25

Page 28: Oracle 10g IMP Notes of Dinesh

Using a sequence * Insert new department named � Support � in location ID 2500 eg:- Insert into departments (department_id, department_name, location_id) Values (dept_deptid_seq.Nextval, ' support ', 2500) ; Select dep_detid_seq.Currval From Dual ; // 120 Modifying a Sequence Change the increment value, maximum value, minmum value, cycle option or cache option. eg:- Alter Sequence dept_depid_seq Increment by 20 Maxvalue 999999 Nocache Nocycle ; Removing a Sequence Remove a sequence from the data dictionary by using the drop sequence statement. eg:- Drop Sequence dept_deptid_seq ; Sequence dropped. What is Index ? An Index * Is a schema object * Is used by the oracle server to speed up the retrival of rows by using a pointer. * Can reduce disk I/O by using a rapid path access method to locate data quickly. * Is independent of the table it indexes. * Is used and maintained automatically by the oracle server. How are Indexes Created ? * Automatically: A unique index is created automatically when you define a primary key or unique constraint in a table columns definition. * Manually: Users can create nonunique indexes on columns to speed up access to the rows. Creating an Index * Create an index on one or more columns. Create Index index_name On table_name (column[,column]...) ; * Improve the speed of query access to the last_name column in the employees table. eg:- Create Index emp_last_name_idx On employees (last_name) ; View User_ind_columns, User_indexes Removing an Index * Removing an Index from the data dictionary to using the drop index command. Drop Index index_name ; eg:- drop Index emp_last_name_idx ; Index dropped. Synonyms Simplify access to object by creating a synonym (another name for an object) with the synonyms, you can * Ease referring to a table owned by another user * Shorten lengthy object names Create [Public] Synonym synonym_name For object_name ; * Creating and Removing Synonym create a shortened name for the dept_sum_vu view. eg:- Create Synonym d_sum For dept_sum_vu ; eg:- Drop Synonym d_sum ;

Page 26

Page 29: Oracle 10g IMP Notes of Dinesh

Controlling User Access Privileges * Database Security: - Systen Secrity - Data Secrity * System privileges: - Gaining access to the database * Object privileges: - Maniplating the content of the database objects * Schemas: - Collections of object such as table, View and Sequences System Privileges * More than too privileges are available * The datebase administrator has high � level system privileges for task such as: - Creating New Users - Removing Users - Removing Tables - Backing up Tables Creating Users * The DBA creates users by using the Create User Statment. Create User user_name Identified By Password ; eg:- Create User Dinesh_user Identified By Dinesh_B User System Privileges * Once a user is created the DBA can Grant specific system privileges to a user Grant Privileges [,privileges...] To User [,user/role, public...] ; * An application developer for example, may have the following system privileges: View Session_privs - Create Session � To Connect the Database - Create Table � Create Table in the User's Schema - Create Sequence � Create a Sequence in User's Schema - Create View � Create a View in the User's Schema - Create Procedure � Create a Stored Procedure, Function or Package in the User's Schema Granting System Privileges The DBA can Grant a User Specifice System Privileges. eg:- Grant Create Session, Create Table, Create Sequence, Create View To Scott ;

What is a Role ?

Page 27

Managed

Allocating Privileges

Without a Role

Allocating Privileges

With a Role

User Privileges

Page 30: Oracle 10g IMP Notes of Dinesh

Creating and Granting Privileges to Role Create a Role eg:- Create Role dinesh_role ; Granting Privileges to Role eg:- Grant Create Table, Create View To dinesh_role ; Granting Role to Users eg:- Grant dinesh_role To dinesh_user ; Changing your Password & Lock/Unlock Alter User scott Account Unlock Identified By tiger ; Syntax: Grant object_priv [(columns)] On Object To {User|Role|Public} [With Grant Option] ; Granting Object Privileges * Grant query privileges on the employees table eg:- Grant Select On employees To sue, rich ; eg:- Grant update (department_name, location_id) On departments To scott,manager ; Using the with Grant Option and Public Keyword * Give a user authority to pass along privileges. eg:- Grant Select,Insert On departments To scott With Grant Option ; * Allow all users on the system to query data from alice's department tables. eg:- Grant Select On alice.departments To Publice ; How to Revoke Object Privileges * You use the Revoke statement to revoke privileges granted to other users. * Privileges granted to others throgh the with grant option clause are also revoked. Syntax: Revoke {Privileges [,privileges....] | All} On Object From { user[,user...] | Role | Public } [Cascade Constraints] ; Revoking Object Privileges * As user Alice, revoke the select and insert privileges given to user scott on the departments table. eg:- Revoke Select,Insert On departments From scott ;

Database Links * A database link connection allows local users to access data on a remote database Local Remote

emp table

Select * From emp@HQ_acme.com ; HQ_acme.com Database

Page 28

Create SessionAlter Session

Unlimited TablespaceCreate Table Create Cluster

Create SynonymCreate View

Create SequenceCreate Database Link

Create ProcedureCreate TriggerCreate Type

Create OperatorCreate Index type

View to See PrivilegesSession_privs

Role_sys_privsRole_tab_privsUser_role_privs

User_tab_privs_madeUser_tab_privs_recdUser_col_privs_madeUser_col_privs_recd

User_sys_privs

User's

Page 31: Oracle 10g IMP Notes of Dinesh

Database Links * Create the database link Create Public Database Link HQ_acme.com Using 'sales' ; View: All_DB_links eg:- Select * From [email protected] ; Create Public Synonym HQ_emp For [email protected] ; Select * From HQ_emp ; Using the Union operator * Display the current and previous job details of all employees. Display each employee only once. eg:- Select employee_id, job_id From employees Union Select employee_id, job_id From job_history ; Using the Union All operator * Display the current and previous departments of all employees. eg:- Select employee_id, job_id, department_id From employees Union All Select employee_id, job_id, department_id From job_history Order by employee_id ;

The Intersect operator * Use the Intersect operator to return all rows common to multiple queries. eg:- Select employee_id, job_id From employees Intersect Select employee_id, job_id From job_history ; The Minus operator * Use the minus operator to return rows returned by the first query that are not present in the second query (The first Select statement miuns the secound statement) eg:- Select employee_id, job_id From employees Minus Select employee_id,job_id From employees ; Controlling the Order of Rows * Produce an English sentence using to Union operators. eg:- Column a_dummy No Print Select 'Sing' as � My Dream� , 3 a_dummy From Dual Onion Select 'I' 'd like to Teach', 1 From Dual Onion Select 'The world to', 2 From Dual Order by 2 ;

Page 29

Page 32: Oracle 10g IMP Notes of Dinesh

Datetime Functions * Display the time zone offset eg:- Select Tz_offset ('Us/Eastern') From Dual ; Tz_offset ( ['time_zone_name']'[+/- hh:mm'] [sessiontimezone] [DBtimezone] ) Current_date * Display the current data and time in the session's time zone. eg:- Alter Session Set NLS_Date_Format = 'DD-Mon-YYYY, HH24:Min:Ss' ; eg:- Alter Session Set Time_zone = '-8:0' , '-5:0' ; eg:- Select Sessiontimezone, Current_date From Dual ; eg:- Select Time_zone = '[+/-] hh:mm' From Dual ; Current_timestamp * Display the current data and fractional time in the session's time zone. eg:- Alter Session Set Time_zone = '-5:0' ; current_timestamp (precision) eg:- Select sessiontimezone, current_timestamp From Dual ; Localtimestamp * Display the current data and time in the session time zone in a value of timestamp datatype. eg:- Alter Session Set Time_zone = '-5:0' ; eg:- Select current_timestamp, localtimestamp From Dual ; Timestamp [(Fractional_seconds_precision)] with Time Zone Local_timestamp [(Fractional_precision)] Dbtimezone and Sessiontimezone * Display the Value of the database time zone eg:- Select Dbtimezone From Dual ; * Display the Value of the database time zone eg:- Select sessiontimezone From Dual ; Extract * Display the year component form the sysdate. eg:- Select Extract (Year From Sysdate) From Dual ; Select Extract ( [Year] [Month] [Day] [Hour] [Minute] [Second] [Time Zone] [Timezone_minute] [Timezone_resion] [Timezone_Abbr] ) From [datatime_value_expression] [ interval_value_expression] ; From_Tz (timestamp timestamp_value, time_zone_value) To_timestamp (char,[Fmt],['nlsparam']) Time Interval conversion with to Minterval * Display a date the is one year two months after the hire_date for the employees working in the department with thedepartment_id = 20 : eg:- Select hire_date, hire_date+To_yminterval('01-02') as From employees Where department_id = 20 ; All Functions * Tz_offset * Current_date * From_Tz * Current_timestamp * To_timestamp * Localtimestamp * To_timestamp_tz * DBtimezone * To_yminterval * Extract Enhancements to the Group by clause * Rollup * Cube * Grouping * Grouping Set Select [column,] group_function (column), Grouping (expr) From table_name [Where] (condition's) [Group by [Rollup][Cube][Grouping][Grouping Set][Group_by_expression] [Having having_expression] [Order by column] ;

Page 30

Tz_offsetCurrent_date

Current_timestampLocaltimestamp

DBtimezoneSession Time Zone

ExtractFrom_Tz

To_TimestampTo_Yminterval

Timestamp with Time Zone (Ts Tz)Timestamp with Local Time Zone

(Ts Ltz)

('Canada/Yakon')('Europe/London

Page 33: Oracle 10g IMP Notes of Dinesh

* Group data for obtaining the following - Subtotal values by using the Rollup operator - Cross-tobulation values by using the Cube operater * Use the Grouping Function to Identify the level of aggregation in the result set produced by a Rollup and Cube operator. * Use Grouping Set to produce a single result set that is equivalent to a union all approach Group by with Rollup and Cube * Use Rollup or Cube with Group by to produce superaggregate rows by cross-referencing columns. * Rollup grouping produces a results set containing the regular grouped rows and the subtotal values. * Cube grouping produces a results set containing the rows from rollup and cross-tabulation rows. Rollup eg:- Select department_id, job_id, Sum(salary), NVL2(job_id,' ','Total') From employees Where department_id < 60 Group by Rollup (department_id, job_id) ; Cube eg:- Select Count(department_id), department_id, job_id, Sum(salary), NVL2(job_id,' ','Total') From employees Where department_id < 60 Group by Cube (department_id, job_id) ; Grouping with Rollup and Cube eg:- Select department_id dept_id, job_id job, Sum(salary), Grouping (department_id) grp_dept, Grouping (job_id) grp_job, From employees Where department_id < 50 Group by Rollup,Cube (department_id,job_id) ; Grouping Sets eg:- Select department_id, job_id, manager_id, AVG(salary) From employees Group by Grouping Sets ((department_id,job_id),(job_id,manager_id)) ; Concatenated Grouping eg:- Select department_id, job_id, manager_id, Sum(salary) From employees Group by department_id, Rollup (job_id), Cube(manager_id) ;

Pairwise Comparison Subquery eg:- Select employee_id, manager_id, department_id From employees Where (manager_id, department_id) IN (Select manager_id,department_id From employees Where employee_id IN (178,174)) And employee_id Not In (178,174) ; Using Correlated Subquery eg:- Select last_name, salary, department_id From employees outer Where salary > (Select AVG(salary) From employees Where department_id = outer.department_id) ; Using the Exists operator eg:- Select employee_id, last_name, job_id, department_id From employees outer Where Exists (Select 'X' No Exists From employees Where manager_id = outer.department_id) ; With Clause eg:- With dept_costs As ( Select d.department_id, Sum(e.salary) As dept_total From employees e,departments d Where d.department_id = department_id Group by d.department_id = d.department_id ) Avg_costs As ( Select Sum(dept_total)/Count(*) As dept_avg From departments ) Select * From dept_costs Where dept_total > ( Select dept_avg From avg_costs ) Order by department_name ;

Page 31

Page 34: Oracle 10g IMP Notes of Dinesh

Hierarchical Queries Select [Level], column, expr..., From table_name [Where condition's] [Start with condition's] [Connect by Prior condition's] ; Walking the Tree From the Bottom to Up eg:- Select employee_id, last_name, job_id, manager_id From employees Start with employee_id = 101 Connect by Prior manager_id = employee_id ; Walking the Tree From the Top to Down eg:- Select last_name||' Reports to '|| Prior last_name � Walk Top to Down � From employees Start with last_name = 'King' Connect by Prior employee_id ; Column Org_chart Format d12 eg:- Select Lpad(last_name,length(last_name)+(Level*2)-2,'_') As Org_chart From employees Start with last_name = 'King' Connect by Prior employee_id = manager_id ; Multitable Insert Statements Insert [All] [conditional_insert_clause] [insert_into_clause values_clause] (Subquery) Conditional_insert_clause [All] [First] [When condition Then] [insert_into_clause values_clause] [Else] [insert_into_clause values_clause] Insert Insert All, First Unconditional Insert All eg:- Insert All into sal_history Values(empID,hiredate,sal) into mgr_hirstory Values(empID,MGR,sal) Select employee_id empID, hire_date hiredate, salary sal, manager_id mgr From employees Where employee_id > 200 ; Conditional Insert All eg:- Insert All When sal> 10000 Then into sal_history Values(empID,hiredate,sal) When mgr > 200 Then into mgr_hirstory Values(empID,MGR,sal) Select employee_id empID, hire_date hiredate, salary sal, manager_id mgr From employees Where employee_id > 200 ; Conditional First Insert eg:- Insert First When sal > 25000 Then into special_sal Values (deptid,sal) When Hiredate Like ('%00%') Then into hiredate_history_00 Values(deptid,hiredate) When hiredate Like ('%99%') Then into hiredate_history_99 Values(deptid,hiredate) else into hiredate_history Values(deptid,hiredate) Select department_id deptid,Sum(salary) sal,Max(hire_date) hiredate From employees Group by department_id ;

Page 32

Page 35: Oracle 10g IMP Notes of Dinesh

Pivoting Insert eg:- Insert All into sales_info Values (employee_id,week_id,sales-mon) into sales_info Values (employee_id,week_id,sales-tue) into sales_info Values (employee_id,week_id,sales-wed) into sales_info Values (employee_id,week_id,sales-thu) into sales_info Values (employee_id,week_id,sales-fir) Select employee_id,week_id,sales-man,sales-tue,sales-wed,sales-thu,sales-fir From sales_source_date ; External Tables * External tables are read-only tables in which the data is stored outside the database inflat files. * The metadata foran external table is created using a Create Table statement. * With the help of external tables, oracle data can be stored or unloaded as a files. * The data can be querid using SQL but you cannot use DML and no Indexes can be created. Example of Creating an External Table Create [or Replace] Directory As 'Path_Name' ; eg:- Create Directory emp_dir As '/Flat_Files' ; eg:- Create Table oldemp(empno Number(2), empname Char(20), brithdate Date) Organization External ( Type Oracle-Loader Default Directory emp_dir Access Parameters ( Records Delimited by Newline Badfile 'bad_emp' Logfile 'log_emp' Eleds Terminated by ',' ( empno Char, brithdate Char Date_Format date Mask � DD-Mon-YYYY� ) ) Location ('emp1.txt') ) Parallel 5 Reject Limit 200 ; Create Index with Create Table statment eg:- Create Table New_emp (employee_id Number(6) Primary Key Using Index ( Create Index emp_id_idx_on_new_emp(employee_id ) ), first_name Varchar2(20), last_name Varchar2(25) ) ; eg:- Select Index_name, Table_name From User_indexes Where Table_name = ' New_emp ' ; SQL * Plus Editing Commands A[ppend] text C[hange] /old/new/text/ Cl[er] Buff[er] Del n m,m n I[nput] text L[ist] n,m R[un] n text o text Save Filename Get Filename Start Filename @ Filename Edit Filename Spool Filename Exit

Page 33

User_tablesUser_objects

User_tab_privs_madeUser_col_privs_made

Page 36: Oracle 10g IMP Notes of Dinesh

Controlling the Environment Set echo OFF Set Feedback OFF <= Set System Variable to appropriate values Set Pagesize 0 Spool Dropem.sql SQL Statement Spool OFF Set Feedback ON Set Pagesize 24 <= Default System Value Set echo ON

Page 34