Oracle naveen Sql


Published on

this is sufficientt to study SQL
their are all syntax and i may cover all selected area in dbms

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Oracle naveen Sql

  1. 1. TABLE <ul><li>A Table is a database object. </li></ul><ul><li>A Table is a logical representation of data present in the database. </li></ul><ul><li>A Table is a unit of storage that holds data in the form of Rows & columns. </li></ul>
  2. 2. Data Definition Language (DDL) <ul><li>Data Definition Language and the commands are used to create an object (eg. Table), alter the structure of the object and also to drop the object created. </li></ul><ul><li>The DDL commands are </li></ul><ul><li>Create, Alter, Truncate, Drop, Rename </li></ul>
  3. 3. CREATE Command <ul><li>Command to create a table. </li></ul><ul><li>Syntax: </li></ul><ul><li>create table <table name> (column name1 datatype, column name 2 datatype, column name 3 datatype,….); </li></ul><ul><li>Message: table created </li></ul>
  4. 4. CREATE Command Norms <ul><li>The first letter of the table name should be an alphabet. </li></ul><ul><li>Oracle reserved words cannot be used for naming objects. </li></ul><ul><li>Length of a table name should not exceed 30 characters. </li></ul><ul><li>Two different tables cannot have the same name. </li></ul><ul><li>Blank spaces or single quotes cannot be used with the table names. Numerals & underscore can be used. </li></ul>
  5. 5. ALTER Command <ul><li>Command to Alter a table. </li></ul><ul><li>Syntax: </li></ul><ul><li>alter table <table name> modify column name datatype; </li></ul><ul><li>alter table <table name> add column name datatype; </li></ul><ul><li>Message: table altered </li></ul>
  6. 6. ALTER Command <ul><li>Alter command can be used to </li></ul><ul><li>Add a new column to the table. </li></ul><ul><li>To change the datatype or the width of the datatype of column(s). </li></ul><ul><li>To include or drop constraints. </li></ul>
  7. 7. ALTER Command Norms <ul><li>The length of an existing column datatype can be reduced only if the table is empty or without records. </li></ul><ul><li>The length of an existing column datatype can be increased even if the table has records. </li></ul>
  8. 8. TRUNCATE Command <ul><li>Command to Truncate a table. </li></ul><ul><li>Syntax: </li></ul><ul><li>truncate table <table name>; </li></ul><ul><li>Message: table truncated </li></ul>
  9. 9. TRUNCATE Command <ul><li>Truncate command delete’s all the rows/records from the specified table. </li></ul><ul><li>Adding ‘reuse storage’ to the truncate command delete’s all the records but reclaims the space used for the storage of the records. </li></ul><ul><li>Ex: truncate table <table name> reuse storage; </li></ul>
  10. 10. DROP Command <ul><li>Command to drop the table. </li></ul><ul><li>Syntax: </li></ul><ul><li>Drop table <table name>; </li></ul><ul><li>Message: table dropped </li></ul>
  11. 11. DESC Command <ul><li>Command to view the structure of the table. </li></ul><ul><li>Syntax: desc <table name> </li></ul><ul><li>Table structure will be displayed </li></ul>
  12. 12. RENAME Command <ul><li>Command to Rename the name of the table. </li></ul><ul><li>Syntax: rename <table name> to <New table name>; </li></ul><ul><li>Table renamed. </li></ul>
  13. 13. Data Manipulation Language (DML) <ul><li>Data Manipulation Language and the commands are used to Query and manipulate existing objects (Ex: Tables). </li></ul><ul><li>The DML commands are </li></ul><ul><li>Insert, Select, Update, Delete </li></ul>
  14. 14. INSERT Command <ul><li>Values while using the insert command must be seperated by commas. </li></ul><ul><li>Values of data type char,varchar2, raw, long & date must be enclosed in single quotes. </li></ul><ul><li>The values must be in the same order as they are defined in the table. </li></ul>
  15. 15. INSERT Command <ul><li>To populate the table with rows/records. </li></ul><ul><li>Syntax: </li></ul><ul><li>Insert into <table name> values (a list of data values); </li></ul><ul><li>Message: 1 row created </li></ul>
  16. 16. INSERT Command <ul><li>To insert multiple records using a single insert command. </li></ul><ul><li>Syntax: </li></ul><ul><li>Insert into <table name> values (& colu name 1, &colu name 2, &col name 3); </li></ul><ul><li>Oracle prompts for values </li></ul><ul><li>Enter the value for <colu name 1>: </li></ul><ul><li>Enter the value for <colu name 2>: </li></ul><ul><li>Enter the value for <colu name 3>: </li></ul>
  17. 17. INSERT Command <ul><li>To skip fields in a record while inserting a record. </li></ul><ul><li>The necessary fields should be explicitly mentioned. </li></ul><ul><li>Syntax: </li></ul><ul><li>Insert into <table name> (<Col name 1> ,<Col name 2>,…) values (list of values); </li></ul><ul><li>Message: 1 row created. </li></ul>
  18. 18. INSERT Command <ul><li>Another way of skipping one or more fields is to enter NULL value against the column name. </li></ul><ul><li>Syntax: </li></ul><ul><li>Insert into <table name> values (<value1>, <value2>, <value3>, Null, <value5>, Null, <value7>); </li></ul><ul><li>Message: 1 row created. </li></ul>
  19. 19. SELECT Command <ul><li>Used for Querying the Databade </li></ul><ul><li>Syntax: </li></ul><ul><li>Select <colu name1>, <colu name2> ……. From <table name1>, <table name2>…; </li></ul><ul><li>Output will be a set of rows & columns of the specified table(s) </li></ul>
  20. 20. SELECT Command <ul><li>We can display either all the columns in a table or only specific columns from the table. </li></ul><ul><li>The order of the column names in the select command specifies the order in which they should be displayed. </li></ul>
  21. 21. SELECT Command with DISTINCT clause <ul><li>Distinct clause in the select command is included to avoid selection of duplicate rows. </li></ul><ul><li>Syntax: Select distinct(<colu name>) from <table name>; </li></ul><ul><li>Output will be the specified column with its values. </li></ul>
  22. 22. SELECT Command with WHERE & ORDER BY clause <ul><li>Where clause is included in the select command to select specific rows from a table. Rows which satisfy the where condition will only be retrieved. </li></ul><ul><li>Order by clause is used to arrange the displayed rows as per a pre defined order or in the ascending or descending order. Order by clause can also be used to arrange multiple columns. </li></ul>
  23. 23. SELECT Command with WHERE & ORDER BY clause <ul><li>Syntax: </li></ul><ul><li>Select <colu name>, <colu name>,… from <table name>,<table name>… [Where <condition(s)>] </li></ul><ul><li>[Order by <colu name1>, <colu name2>,….. asc/desc] ; </li></ul>
  24. 24. UPDATE Command <ul><li>Update command is used to reflect changes to the existing rows/records in a table. </li></ul><ul><li>A single or more than one column can be updated. </li></ul><ul><li>Specific rows can be updated based on conditions. </li></ul>
  25. 25. UPDATE Command <ul><li>Syntax: </li></ul><ul><li>Update <table name> set <column name> = <value>,…. Where <condition>; </li></ul><ul><li>Output: </li></ul><ul><li>1 row updated </li></ul>
  26. 26. DELETE Command <ul><li>Syntax: </li></ul><ul><li>Delete from <table name> </li></ul><ul><li>where <condition>; </li></ul><ul><li>Output: </li></ul><ul><li>1 row deleted </li></ul>
  27. 27. DELETE Command <ul><li>One or more rows can be deleted using the delete command. </li></ul><ul><li>To delete specific rows the where clause is used with the delete command. </li></ul><ul><li>Where clause can also include a query. </li></ul><ul><li>The delete command can be used to delete all rows of a table while the structure remains unaltered. </li></ul>
  28. 28. Transaction Control Language (TCL) <ul><li>All changes made to a database can be referred to as transactions. </li></ul><ul><li>Transaction changes can be made permanent to a database by explicitly using the Transaction control commands. </li></ul><ul><li>When a DDL statement is used the transactions are implicitly made permanent to a database. </li></ul><ul><li>The transaction control commands are Commit, Savepoint & Rollback. </li></ul>
  29. 29. SAVEPOINT COMMAND <ul><li>Syntax: </li></ul><ul><li>Savepoint <savepoint name>; </li></ul><ul><li>Savepoints are used to divide a very lengthy transsaction to smaller ones. </li></ul><ul><li>They are used to identify to a point in the transaction to which we can later rollback. Savepoint is used in conjuction with rollback. </li></ul>
  30. 30. ROLLBACK COMMAND <ul><li>Syntax: Rollback; or </li></ul><ul><li>Rollback to Savepoint <SP name>; </li></ul><ul><li>Rollback command is used to undo the work in the current transaction. </li></ul><ul><li>Rollback to a particular savepoint to undo the transactions after the savepoint or roll back the entire transactions to undo the changes done. </li></ul>
  31. 31. COMMIT COMMAND <ul><li>Syntax: Commit; </li></ul><ul><li>Commit command is used to end a transaction. </li></ul><ul><li>Only with the commit command the transactions can be made permanent to the database. </li></ul><ul><li>This command erases all savepoint’s in the transactions. </li></ul>
  32. 32. Data Control Language (DCL) <ul><li>Provides users with privilege command. </li></ul><ul><li>The owner of the tables has sole command on them and can allow other users access to the tables or other objects. </li></ul><ul><li>Privileges once granted can also be withdrawn by the owner. </li></ul><ul><li>The data control commands are Grant & Revoke. </li></ul>
  33. 33. GRANT COMMAND <ul><li>Syntax: grant <privileges> on <object name> to <username>; </li></ul><ul><li>To grant privileges on particular columns: </li></ul><ul><li>Syntax: Grant <privileges>(col name1,col name2,..) on <table name> to <username>; </li></ul><ul><li>Owner A – grant - B & B - C. Then A has to follow the grant syntax allowing B to grant to C. </li></ul><ul><li>Syntax:Grant <privileges> on <object name> to <username> with grant option; </li></ul><ul><li>ALL to grant all the privileges . </li></ul>
  34. 34. REVOKE COMMAND <ul><li>Syntax: revoke <privileges> on <object name> from <username>; </li></ul><ul><li>To withdraw privileges from the user. </li></ul>
  35. 35. Different Select Statements <ul><li>Creating tables using select statements: </li></ul><ul><li>Create table emp1 as select * from emp; Or </li></ul><ul><li>create table emp2(id, name) as select empno,ename from emp; </li></ul><ul><li>Select Command to insert records: </li></ul><ul><li>Insert into emp3 (select ename,job from emp); </li></ul><ul><li>Defining column alias name using select statement: </li></ul><ul><li>Select empno id from emp; Or </li></ul><ul><li>Select empno, ename, sal*1.10 as revised_sal from emp where sal>2000; </li></ul>
  36. 36. OPERATORS IN SQL <ul><li>Operators supported by SQL </li></ul><ul><li>ARITHMETIC OPERATORS </li></ul><ul><li>COMPARISON OPERATORS </li></ul><ul><li>LOGICAL OPERATORS </li></ul>
  37. 37. ARITHMETIC OPERATORS <ul><li>They are included in SQL commands to perform calculations on number based values. </li></ul><ul><li>ARITHMETIC Operators are </li></ul><ul><li>+ (Addition) - (Subtraction) * (Multiplication) / (Division) </li></ul><ul><li>Precedence of Arithmetic operators </li></ul><ul><li>* / + - </li></ul>
  38. 38. COMPARISON OPERATORS <ul><li>They are used in conditions to compare one expression with another. </li></ul><ul><li>Comparison operators are </li></ul><ul><li>= != < > <= >= </li></ul><ul><li>Between (to check between any two values) </li></ul><ul><li>In (to match with any of the values in list) </li></ul><ul><li>Like (to match a character pattern) </li></ul><ul><li>Is null (to check if it is null) </li></ul><ul><li>Not between , not in , not like , is not null </li></ul>
  39. 39. COMPARISON OPERATORS <ul><li>Using IN operator if we search for character values then the column name must match exactly with the values in the list. </li></ul><ul><li>Knowing the character value is not required for using the LIKE operator. </li></ul><ul><li>LIKE can use characters of </li></ul><ul><li>% (to match zero or more characters) </li></ul><ul><li>_ (matches exactly one character) </li></ul>
  40. 40. LOGICAL OPERATORS <ul><li>Used to combine the results of two conditions to produce a single result. </li></ul><ul><li>The logical operators are: </li></ul><ul><li>AND </li></ul><ul><li>NOT </li></ul><ul><li>OR </li></ul>
  41. 41. OPERATOR PRECEDENCE <ul><li>1 - Arithmetic operators </li></ul><ul><li>2 - Comparison operators </li></ul><ul><li>3 - NOT Logical operator </li></ul><ul><li>4 - AND Logical Operator </li></ul><ul><li>5 - OR Logical Operator </li></ul><ul><li>The order of precedence can be altered using parenthesis </li></ul>
  42. 42. SQL * PLUS FUNCTIONS <ul><li>Functions to perform operations using the DML commands. </li></ul><ul><li>A function takes one or more arguments and returns a value. </li></ul><ul><li>Functions can be classified into SINGLE ROW functions & GROUP functions. </li></ul>
  43. 43. SINGLE ROW FUNCTIONS <ul><li>DATE FUNCTIONS </li></ul><ul><li>NUMERIC FUNCTIONS </li></ul><ul><li>CHARACTER FUNCTIONS </li></ul><ul><li>CONVERSION FUNCTIONS </li></ul><ul><li>MISCELLANEOUS FUNCTIONS </li></ul><ul><li>DUAL: System table created by ORACLE with 1 column of varchar2 datatype & 1 row with value ‘x’. </li></ul>
  44. 44. DATE FUNCTIONS <ul><li>1. ADD_MONTHS </li></ul><ul><li>Returns a date after adding a specified date with the specified number of months. </li></ul><ul><li>Format: add_months (D,N) </li></ul><ul><li>D - date </li></ul><ul><li>N – number of months </li></ul><ul><li>Example: </li></ul><ul><li>select hiredate, add_months(hiredate,4) from emp; </li></ul>
  45. 45. DATE FUNCTIONS <ul><li>2. LAST_DAY </li></ul><ul><li>Returns the date corresponding to the last day of the month. </li></ul><ul><li>Format: last_day (D) </li></ul><ul><li>D - date </li></ul><ul><li>Example: </li></ul><ul><li>select sysdate, last_day(sysdate) from dual; </li></ul>
  46. 46. DATE FUNCTIONS <ul><li>3. MONTHS_BETWEEN </li></ul><ul><li>To find out the number of months between two given dates. </li></ul><ul><li>Format: months_between (D1,D2) </li></ul><ul><li>D1 – date1 D2 – date2 </li></ul><ul><li>Output: number </li></ul><ul><li>If D1 later than D2 – result – positive </li></ul><ul><li>If D1 earlier than D2 – result - negative </li></ul><ul><li>If D1 & D2 - same date – result – integer (result based on the 31 day month & time components) </li></ul>
  47. 47. DATE FUNCTIONS <ul><li>4. ROUND </li></ul><ul><li>The function returns the date which is rounded to the unit specified by the format model. </li></ul><ul><li>Format: round (date,‘format’) </li></ul><ul><li>Format options: year, month, day </li></ul><ul><li>Specifying the format is optional. If the format is not specified the date will be rounded to the nearest day. </li></ul><ul><li>Select sysdate, round(sysdate, ‘year’) from dual; </li></ul>
  48. 48. DATE FUNCTIONS <ul><li>5. NEXT_DAY </li></ul><ul><li>Returns the next date corresponding to the day specified. </li></ul><ul><li>Format: next_day (D, ‘day’) </li></ul><ul><li>D – date day – any week day </li></ul><ul><li>Example: </li></ul><ul><li>Select next_day(sysdate, ‘Monday’) from dual; </li></ul>
  49. 49. DATE FUNCTIONS <ul><li>6. TRUNCATE </li></ul><ul><li>Truncate function returns the date with the time portion of the day truncated to the unit specified by format model. </li></ul><ul><li>Format: trunc (date,‘format’) </li></ul><ul><li>Format options: year, month, day </li></ul><ul><li>Specifying the format is optional. If the format is not specified the date will be rounded to the nearest day. </li></ul><ul><li>Ex: Select sysdate, trunc(sysdate, ‘year’) from dual; </li></ul>
  50. 50. DATE FUNCTIONS <ul><li>7. GREATEST </li></ul><ul><li>This function returns the latest date present in the argument. </li></ul><ul><li>Format: greatest (date1, date2, . . . ) </li></ul><ul><li>Example: </li></ul><ul><li>Select hiredate, sysdate, greatest (hiredate, sysdate) from emp; </li></ul>
  51. 51. DATE FUNCTIONS <ul><li>8. NEW_TIME </li></ul><ul><li>This function displays the time & the date of a date column or literal date in other time zones. </li></ul><ul><li>Format: new_time (date,‘this’, ‘other’) </li></ul><ul><li>This – 3 letter abbreviation of the current time zone, Other – 3 letter abbreviation of the zone in which the date is wanted. </li></ul><ul><li>Ex: Select new_time(sysdate, ‘est’, ‘yst’) from dual; </li></ul>
  52. 52. CHARACTER FUNCTIONS <ul><li>Character Functions accept input & return either character or number values. </li></ul><ul><li>1. Initcap </li></ul><ul><li>Format : Initcap (‘char’) </li></ul><ul><li>Example : select initcap(‘orbit’) from dual; </li></ul><ul><li>Output : Orbit </li></ul><ul><li>2. Lower </li></ul><ul><li>Format : Lower (‘char’) </li></ul><ul><li>Example : select lower (‘STUDENTS’) from dual; </li></ul><ul><li>Output : students </li></ul>
  53. 53. CHARACTER FUNCTIONS <ul><li>3. Upper </li></ul><ul><li>Format : Upper (‘char’) </li></ul><ul><li>Example : select upper (‘students’) from dual; </li></ul><ul><li>Output : STUDENTS </li></ul><ul><li>4. Ltrim </li></ul><ul><li>Format : Ltrim (‘char’, ‘set’) </li></ul><ul><li>Example : select ltrim (‘abcorbit’, ‘abc’) from dual; </li></ul><ul><li>Output : orbit </li></ul>
  54. 54. CHARACTER FUNCTIONS <ul><li>5. Rtrim </li></ul><ul><li>Format : Rtrim (‘char’, ‘set’) </li></ul><ul><li>Example : select rtrim (‘abcorbit’, ‘orbit’) from dual; </li></ul><ul><li>Output : abc </li></ul><ul><li>6. Translate </li></ul><ul><li>Format : Translate (‘char’, ‘from’, ‘to’) </li></ul><ul><li>Example : select translate (‘mister’, ‘m’ , ‘s’) from dual; </li></ul><ul><li>Output : sister </li></ul>
  55. 55. CHARACTER FUNCTIONS <ul><li>7. Replace </li></ul><ul><li>Format : Replace (‘char’, ‘search string’, ‘replace string’) </li></ul><ul><li>Example : select replace (‘boke is bola’, ‘b’ , ‘c’) from dual; </li></ul><ul><li>Output : coke is cola </li></ul><ul><li>8. Substr </li></ul><ul><li>Format : Substr (‘char’, m, n) </li></ul><ul><li>Example : select substr (‘fraction’, 3 , 6) from dual; </li></ul><ul><li>Output : action </li></ul>
  56. 56. CHARACTER FUNCTIONS <ul><li>CHR Function : returns the character equivalent of the number that is specified. </li></ul><ul><li>Format : chr(number) </li></ul><ul><li>Example : select chr (77) from dual; </li></ul><ul><li>Output : M </li></ul>
  57. 57. CHARACTER FUNCTIONS <ul><li>LPAD Function : Pads the value to the left of the given string </li></ul><ul><li>Format: lpad (‘String’, N, ‘S’) </li></ul><ul><li>String : Character string which has to be displayed with the left padding </li></ul><ul><li>N = number which indicates the total length of the return value </li></ul><ul><li>S = string with which the left padding has to be done </li></ul><ul><li>Example : select lpad (‘Oracle’, 10, ‘*’) from dual; </li></ul><ul><li>Output : ****Oracle </li></ul>
  58. 58. CHARACTER FUNCTIONS <ul><li>RPAD Function : Pads the value to the right of the given string </li></ul><ul><li>Format: rpad (‘String’, N, ‘S’) </li></ul><ul><li>String : Character string which has to be displayed with the left padding </li></ul><ul><li>N = number which indicates the total length of the return value </li></ul><ul><li>S = string with which the left padding has to be done </li></ul><ul><li>Example : select rpad (‘Oracle’, 10, ‘*’) from dual; </li></ul><ul><li>Output : Oracle **** </li></ul>
  59. 59. CHARACTER FUNCTIONS <ul><li>LENGTH Function : Returns the length of a string </li></ul><ul><li>Format: length (‘String’) </li></ul><ul><li>Example : select length(‘microsoft’) from dual; </li></ul><ul><li>Output : 9 </li></ul>
  60. 60. CHARACTER FUNCTIONS <ul><li>DECODE Function : Value by value replacement. </li></ul><ul><li>Format: decode (value, if1, then1, if2, then2,….) from table_name; </li></ul><ul><li>Example : select empno, ename, decode (ename, ‘SMITH’, ‘MYTH’) from emp; </li></ul><ul><li>SOUNDEX Function: Compares words that are spelled different but sound alike. </li></ul><ul><li>Example : select ename from emp where soundex (ename) = soundex (‘MILER’); </li></ul>
  61. 61. CONCATENATION OPERATOR <ul><li>Used to merge two or more strings, or a string and a data value together. </li></ul><ul><li>Example : select (‘The date on which’||ename|| ‘joined this org is’ ||hiredate) join_date from emp; </li></ul>
  62. 62. NUMERIC FUNCTIONS <ul><li>Numeric functions accepts numeric input and returns numeric values a output </li></ul><ul><li>1. ABS Function : </li></ul><ul><li>Ex: select abs(-10) from dual; </li></ul><ul><li>Output : 10 </li></ul><ul><li>2. CEIL Function: </li></ul><ul><li>Format : ceil(n) </li></ul><ul><li>Ex: select ceil(11.678) from dual; </li></ul><ul><li>Output : 12 </li></ul>
  63. 63. NUMERIC FUNCTIONS <ul><li>3. COS Function: </li></ul><ul><li>Format : cos(n) </li></ul><ul><li>Ex: select cos(116) from dual; </li></ul><ul><li>Output : integer </li></ul><ul><li>4. COSH Function: </li></ul><ul><li>Format : cosh(n) </li></ul><ul><li>Ex: select cosh(0) from dual; </li></ul><ul><li>Output : 1 </li></ul>
  64. 64. NUMERIC FUNCTIONS <ul><li>5. EXP Function: </li></ul><ul><li>Format : exp(n) </li></ul><ul><li>Ex: select exp(2) from dual; </li></ul><ul><li>Output : integer </li></ul><ul><li>6. FLOOR Function: </li></ul><ul><li>Format : floor(n) </li></ul><ul><li>Ex: select floor(10.2) from dual; </li></ul><ul><li>Output : 10 </li></ul>
  65. 65. NUMERIC FUNCTIONS <ul><li>7. POWER Function: </li></ul><ul><li>Format : power(m,n) </li></ul><ul><li>Ex: select power(5,2) from dual; </li></ul><ul><li>Output : 25 </li></ul><ul><li>8. MOD Function: </li></ul><ul><li>Format : mod(m,n) </li></ul><ul><li>Ex: select mod(10,3) from dual; </li></ul><ul><li>Output : 1 </li></ul>
  66. 66. NUMERIC FUNCTIONS <ul><li>9. ROUND Function: </li></ul><ul><li>Format : Round(m,n) </li></ul><ul><li>Ex: select round(100.256,2) from dual; </li></ul><ul><li>Output : 100.26 </li></ul><ul><li>10. TRUNC Function: </li></ul><ul><li>Format : trunc(m,n) </li></ul><ul><li>Ex: select trunc(100.256,2) from dual; </li></ul><ul><li>Output : 100.25 </li></ul>
  67. 67. NUMERIC FUNCTIONS <ul><li>11. SQRT Function: </li></ul><ul><li>Format : sqrt(n) </li></ul><ul><li>Ex: select sqrt(25) from dual; </li></ul><ul><li>Output : 5 </li></ul><ul><li>12. LN Function: returns logarithmic value </li></ul><ul><li>Format : ln(n) </li></ul><ul><li>Ex: select ln(2) from dual; </li></ul><ul><li>Output : logarithmic value of 2 </li></ul>
  68. 68. NUMERIC FUNCTIONS <ul><li>13. MOD Function: gives the remainder of value divided by another </li></ul><ul><li>Format : mod(m,n) </li></ul><ul><li>Ex: select mod(25,6) from dual; </li></ul><ul><li>Output : 1 </li></ul><ul><li>14. SIGN Function: returns sign of a value without its magnitude </li></ul><ul><li>Format : sign(n) </li></ul><ul><li>Ex: select sign(-32) from dual; Output : -1 </li></ul>
  69. 69. CONVERSION FUNCTIONS <ul><li>Convert a value from one data type to another. Conversion functions are </li></ul><ul><li>To_char( ) </li></ul><ul><li>To_date ( ) </li></ul><ul><li>To_number ( ) </li></ul><ul><li>To_char ( ) function : Converts date to a value of varchar2 datatype in the form specified by date format (fmt). If fmt is neglected then it converts date to varchar2 in the default format. </li></ul>
  70. 70. CONVERSION FUNCTIONS <ul><li>Format : To_char(date, ‘format’) </li></ul><ul><li>Ex: select to_char(sysdate, ‘ddth “of” fmmonth yyyy’) from dual; </li></ul><ul><li>Fill mode (fm) – format mask is used to avoid blank padding of characters and zero padding to numeric. </li></ul>
  71. 71. CONVERSION FUNCTIONS <ul><li>To_date ( ) function : Converts char or varchar2 datatype to date datatype in the form specified by character format (fmt). </li></ul><ul><li>Format : To_date(char, ‘format’) </li></ul><ul><li>Ex: select to_date(‘august 22 2004’ , ‘month-dd-yyyy’) from dual; </li></ul><ul><li>Select round(to_date( ‘27-jan-04’, ‘year’) from dual; </li></ul>
  72. 72. CONVERSION FUNCTIONS <ul><li>To_number ( ) function : Converts a string containing number into a number datatype on which arithmetic operators can be performed. </li></ul><ul><li>Format : To_number(char, ‘format’) </li></ul><ul><li>Ex: select to_number(‘100’) from dual; </li></ul>
  73. 73. MISCELLANEOUS FUNCTIONS <ul><li>UID USER NVL VSIZE </li></ul><ul><li>UID Function : this function returns the integer corresponding to the user currently logged in. </li></ul><ul><li>Ex: Select uid from dual; </li></ul><ul><li>USER Function : this function returns the users login name, which is in varchar2 datatype. </li></ul><ul><li>Ex: Select user from dual; </li></ul>
  74. 74. MISCELLANEOUS FUNCTIONS <ul><li>NVL Function : is used in cases where the null value has to be considered as zero. </li></ul><ul><li>Format : nvl (expression1,expression2) </li></ul><ul><li>If exp1 is null, nvl will return exp2. </li></ul><ul><li>If exp1 is not null, nvl will return exp1. </li></ul><ul><li>If exp1 & exp2 are of different datatypes then oracle converts the datatype of exp2 to exp1 and then compares. </li></ul><ul><li>Ex: select sal,comm,nvl(comm,1000) from emp; </li></ul>
  75. 75. MISCELLANEOUS FUNCTIONS <ul><li>VSIZE Function : it returns the number of bytes in the expression. If the expression is null, it returns null. </li></ul><ul><li>Ex: select vsize(‘students’) from dual; </li></ul>
  76. 76. GROUP FUNCTIONS <ul><li>Group Function returns a result based on a group of rows. </li></ul><ul><li>1. AVG Function : will return the average value of column specified in the argument of the column. </li></ul><ul><li>Ex: Select avg(sal) from emp where deptno = 10; </li></ul><ul><li>2. SUM Function : used to obtain the sum of a range of values. </li></ul><ul><li>Ex: Select sum(sal) from emp; </li></ul>
  77. 77. GROUP FUNCTIONS <ul><li>3. MIN Function : will return the least of all values of the column present in the argument. </li></ul><ul><li>Ex: Select min(sal) from emp; </li></ul><ul><li>4. MAX Function : will return the maximum of all values of the column present in the argument. </li></ul><ul><li>Ex: Select max(sal) from emp; </li></ul>
  78. 78. GROUP FUNCTIONS <ul><li>5. COUNT Functions : used in order to count the number of rows. </li></ul><ul><li>A. Count(*) – counts all rows inclusive of duplicates & nulls. </li></ul><ul><li>Ex: Select count(*) from emp; </li></ul><ul><li>B. Count (col_name) – it counts the number of values present in the column without including the nulls. </li></ul><ul><li>Ex: Select count(comm) from emp; </li></ul>
  79. 79. GROUP FUNCTIONS <ul><li>C. Count (distinct col_name) – it counts the number of values present in the column without including the nulls but eliminates duplicate values without counting </li></ul><ul><li>Ex: Select count(distinct deptno) from emp; </li></ul>
  80. 80. GROUP BY CLAUSE <ul><li>If an SQL command consists of a ‘where’ clause and a ‘group by’ clause then group by must follow the where clause </li></ul><ul><li>Ex: Select max(sal) from emp group by deptno; </li></ul>
  81. 81. HAVING CLAUSE <ul><li>Having clause is used to specify certain conditions on rows, retrieved by using the group by clause. Having clause always follows the group by clause. </li></ul><ul><li>Ex: Select max(sal) from emp group by deptno having deptno != 10 ; </li></ul><ul><li>Arithmetic operators can also be performed in a group function. </li></ul>
  82. 82. ADDITIONAL FUNCTIONS <ul><li>STDDEV Function : gives the standard deviation of a norm of values. </li></ul><ul><li>Ex: select stddev(sal) from emp; </li></ul><ul><li>VARIANCE Function : gives the variance of a norm of values. </li></ul><ul><li>Ex: select variance(sal) from emp; </li></ul>
  83. 83. SET OPERATORS <ul><li>Set operators combine the result of two queries into a single one. Joining queries to retrieve rows. </li></ul><ul><li>UNION </li></ul><ul><li>UNION ALL </li></ul><ul><li>INTERSECT </li></ul><ul><li>MINUS </li></ul>
  84. 84. SET OPERATORS <ul><li>The columns in the select statements joined using the set operators should adhere to the rules: </li></ul><ul><li>The queries which are related by a set operator should have the same number of columns and the corresponding columns must be of the same datatype. </li></ul><ul><li>Such a query should not contain any column of long datatype. </li></ul><ul><li>The label under which the rows are displayed are those from the first select statement. </li></ul><ul><li>It is not required that the column names specified in the select statements be the same but the datatype must necessarily match. </li></ul>
  85. 85. SET OPERATORS <ul><li>UNION </li></ul><ul><li>Union operator returns distinct rows selected by both the queries. </li></ul><ul><li>Neglects duplicate rows. </li></ul><ul><li>Ex: Select deptno from emp union select deptno from dept; </li></ul><ul><li>Select dname from dept union select ename from emp; </li></ul>
  86. 86. SET OPERATORS <ul><li>UNION ALL </li></ul><ul><li>Union operator select all rows selected by either of the queries including the duplicates. </li></ul><ul><li>Does not neglect duplicate rows. </li></ul><ul><li>Ex: Select deptno from emp union all select deptno from dept; </li></ul><ul><li>Select dname from dept union all select ename from emp; </li></ul><ul><li>If an order by clause needs to be used it should follow the last select statement and the order by can be a column or an integer(when column name are different in the select statements) </li></ul>
  87. 87. SET OPERATORS <ul><li>INTERSECT </li></ul><ul><li>Intersect operator returns only rows that are common to both the queries. </li></ul><ul><li>Ex: Select deptno from emp intersect select deptno from dept; </li></ul><ul><li>Select dname from dept intersect select ename from emp; </li></ul>
  88. 88. SET OPERATORS <ul><li>MINUS </li></ul><ul><li>Minus operator returns all distinct rows selected by the first query and not by the second query. </li></ul><ul><li>Ex: Select deptno from emp minus select deptno from dept; </li></ul><ul><li>Select dname from dept minus select ename from emp; </li></ul>
  89. 89. JOINS <ul><li>Joins are used to combine the data spread across tables. </li></ul><ul><li>Joins are performed by where clause. </li></ul><ul><li>The table names to which the respective column belong should be specified in the join condition in the where clause. </li></ul><ul><li>If a join involves more than 2 tables then the first 2 tables are joined based on the condition and then compares the result with the next table and so on. </li></ul>
  90. 90. JOINS <ul><li>SIMPLE JOIN </li></ul><ul><li>Retrieves rows from two tables having a common column. </li></ul><ul><li>Simple Joins are classified in EQUI Join & NON – EQUI Join. </li></ul><ul><li>EQUI JOIN: </li></ul><ul><li>Combines the rows that have equivalent values for the specified columns. Uses ( = ) operator in the condition. </li></ul><ul><li>Ex: Select * from emp,dept where emp.deptno = dept.deptno; </li></ul>
  91. 91. JOINS <ul><li>NON – EQUI JOIN </li></ul><ul><li>A non equi join specifies the relationship between columns belonging to different tables by making use of the relational operators, other than the ( = ) </li></ul><ul><li>Ex: Select e.ename, e.sal, g.grade from emp e, grade g where e.sal between g.losal and g.hisal; </li></ul>
  92. 92. JOINS <ul><li>SELF JOIN </li></ul><ul><li>Joining a table to itself is called a self join. It joins one row in a table to another. It can compare each row of the table to itself and also with other rows of the same table. </li></ul><ul><li>Ex: Select a.ename, b.ename from emp a, </li></ul><ul><li>emp b where b.mgr = a.empno </li></ul>
  93. 93. JOINS <ul><li>OUTER JOIN </li></ul><ul><li>The outer join returns all the rows returned by simple join as well as those rows from one table that do not match any rows from the other table. </li></ul><ul><li>The symbol (+) represents outer join. </li></ul><ul><li>LEFT OUTER JOIN: </li></ul><ul><li>Ex : select e.ename , e.sal, d.dname, d.loc from emp e, dept d where d.deptno = e.deptno(+); </li></ul><ul><li>Left side table gets priority. </li></ul>
  94. 94. JOINS <ul><li>RIGHT OUTER JOIN: </li></ul><ul><li>Ex : select e.ename , e.sal, d.dname, d.loc from emp e, dept d where e.deptno(+) = d.deptno; </li></ul><ul><li>Right side table gets priority. </li></ul>
  95. 95. SUBQUERIES <ul><li>Nesting of queries, one within the other is termed as a subquery. Subqueries are used to retrieve data that depend on the values in the table itself. </li></ul><ul><li>Ex: Select * from emp where deptno = (select deptno from dept where dname = ‘SALES’); </li></ul>
  96. 96. SUBQUERIES <ul><li>Subqueries which return multiple values. </li></ul><ul><li>A subquery can return more than one value. In such cases we should include operators like any , all , in or not in between the comparison operator and the subquery. </li></ul><ul><li>Ex: Select * from emp where deptno = any </li></ul><ul><li>(Select deptno from dept where deptno = 30 and dname =‘SALES’); </li></ul><ul><li>Select * from emp where empno in (select distinct mgr from emp); </li></ul><ul><li>(=any) is equivalent to IN and (!=all) is equivalent to NOT IN. </li></ul>
  97. 97. SUBQUERIES <ul><li>MULTIPLE Subquery </li></ul><ul><li>Nesting of a query within a subquery is also allowed. The subqueries will retrieve information from more than one table and such information is passed on the main query. </li></ul><ul><li>Select deptno from dept where deptno = any (select deptno from emp where job = (select job from emp where sal = 1600)); </li></ul>
  98. 98. SUBQUERIES <ul><li>CORRELATED Subquery </li></ul><ul><li>Correlated subquery is evaluated once for every row processed by the parent statement. </li></ul><ul><li>Ex: select distinct(job) from emp e </li></ul><ul><li>Where 3<=(select count(job) from emp </li></ul><ul><li>where e.job = job); </li></ul><ul><li>If the subquery is selected from the same table as the main query, then the main query must define an alias for the table name, and the subquery must use the alias to refer to the column’s value in the main query. </li></ul>
  99. 99. CONSTRAINTS <ul><li>Integrity Constraints : An integrity constraint is a mechanism used by Oracle to prevent invalid data entry into the table. Constraints are used for enforcing rules that the column in a table have to conform with. </li></ul><ul><li>Types of Constraints : </li></ul><ul><li>Domain integrity constraints </li></ul><ul><li>Entity integrity constraints </li></ul><ul><li>Referential integrity constraints </li></ul>
  100. 100. Domain Integrity Constraints <ul><li>These constraints set a range, and any violations that take place will prevent the user from performing the manipulation that caused the breach. </li></ul><ul><li>Types of Domain Integrity Constraints: </li></ul><ul><li>Not Null Constraint </li></ul><ul><li>Check Constraint </li></ul>
  101. 101. NOT NULL CONSTRAINT <ul><li>By default all columns in a table allow null values. </li></ul><ul><li>When a ‘Not Null’ constraint is enforced though either on a column or a set of columns in a table , it will not allow null values. The user has to provide a value for the column. </li></ul><ul><li>Not Null constraint can be defined using alter table command even when the table contains rows. </li></ul><ul><li>One null is not equivalent to another null. </li></ul>
  102. 102. NOT NULL CONSTRAINT <ul><li>Ex: Create table student_master (sid number(5), sname varchar2(25) constraint nn1 not null, add1 varchar2(25) constraint nn2 not null, add2 varchar2(20) not null, course varchar2(25) not null, phone number(8)); </li></ul><ul><li>Providing a constraint name will be useful for any future reference. If a constraint name is not specified then oracle generates a unique code which is a set of alphanumeric characters. </li></ul><ul><li>The data dictionary is a set of tables that a user can query to obtain information on users and structures in the database. DD table user_constraints has to be queried to obtain the constraint name. </li></ul>
  103. 103. NOT NULL CONSTRAINT <ul><li>Altering a table to include Not Null constraint </li></ul><ul><li>If the records present in the table have null values in the column on which the constraint is to be defined the Oracle will not allow any modification on that column. </li></ul><ul><li>Ex: Alter table student_master modify phone not null; </li></ul><ul><li>Alter table student _master add add3 varchar2(25) constraint nn3 not null; </li></ul>
  104. 104. CHECK CONSTRAINTS <ul><li>Check constraints specify conditions that each row must satisfy. </li></ul><ul><li>These are rules governed by Logical or Boolean expressions. </li></ul><ul><li>Check conditions cannot contain subqueries. </li></ul><ul><li>Ex: create table pro_master(p_code varchar2(25), p_desc varchar2(20), p_cat varchar2(20), qty_hand number(5), re_level number(5), max_level number(5) constraint max check(max_level<500), p_rate number (9,2)); </li></ul>
  105. 105. CHECK CONSTRAINTS <ul><li>Tables can be altered to add a check constraints only if the table is empty. </li></ul><ul><li>Ex: Alter table pro_master add constraint qty check (qty_hand <=1000); </li></ul>
  106. 106. TABLE LEVEL & COLUMN LEVEL CONSTRAINTS <ul><li>The table level constraint is a part of the table definition. An intergrity constraint defined at the table level can impose rules on any columns in the table whereas column level constraint being a part of the column definition can be imposed only on the column on which it is defined. </li></ul><ul><li>The NOT NULL constraint can be given only at column and not at table level. A column level definition can be done by any integrity constraint in the create table command but the alter table command allows only removal or addition of a not null constraint at the column level. </li></ul>
  107. 107. Entity Integrity Constraints <ul><li>Each entity represents a table and each row of a table represents an instance of the entity. Each row of a table can be uniquely identified using the entity constraint. </li></ul><ul><li>Entity integrity constraints are of two types. </li></ul><ul><li>Unique constraints </li></ul><ul><li>Primary key constraints </li></ul>
  108. 108. Unique Constraints <ul><li>Usage of the unique key constraint is to prevent the duplication of values within the rows of a specified column or a set of columns in a table. </li></ul><ul><li>Columns defined with this constraint can also allow null values. </li></ul><ul><li>If unique constraint is defined in combination of columns it is said to be a composite unique key. Those combination of columns cannot have duplicate values. </li></ul><ul><li>Maximum combination of columns that a composite unique key can contain is 16. </li></ul>
  109. 109. Unique Constraints <ul><li>A composite unique key is applicable to all the columns included in the constraint clause. This kind of constraint can be applied only at the table level. </li></ul><ul><li>The entity integrity & referential integrity constraints can have composite keys. </li></ul><ul><li>Ex: create table pro_master(p_code varchar2(25), p_desc varchar2(20), p_cat varchar2(20), qty_hand number(5), re_level number(5), max_level number(5) constraint max check(max_level<500), p_rate number (9,2), constraint pro_uni unique (p_desc)); </li></ul>
  110. 110. Unique Constraints <ul><li>Ex: alter table pro_master add constraint pro_uni unique (p_desc); </li></ul><ul><li>Composite: </li></ul><ul><li>Ex: alter table pro_master add constraint comb_uni unique(p_code,p_desc); </li></ul>
  111. 111. Primary Key Constraints <ul><li>The primary key constraint avoids duplication of rows and does not allow Null values, when enforced in a column or a set of columns. As a result it is used to identify a row. A table can have only one primary key. If a primary key constraint is assigned to a combination of columns it is said to be a composite primary key which can contain a maximum of 16 columns. </li></ul><ul><li>Primary key constraint cannot be defined in an alter table command when the table contains rows having null values. </li></ul>
  112. 112. Primary Key Constraints <ul><li>Defining at column level </li></ul><ul><li>Ex: create table student_detail(sid number(6) constraint stu_prim primary key, sname varchar2(25), course varchar2(25)); </li></ul><ul><li>Defining at Table level </li></ul><ul><li>Ex: create table student_detail(sid number(6) , sname varchar2(25), course varchar2(25), constraint stu_prim primary key(sid)); </li></ul>
  113. 113. Primary Key Constraints <ul><li>Altering a table to add constraint </li></ul><ul><li>Ex: alter table student_detail add constraint stu_prim primary key(sid); </li></ul><ul><li>Composite Primary Key </li></ul><ul><li>Ex: create table first (sno varchar2(4),city varchar2(20), status number(3), pno varchar2(4), qty number(6), constraint comb_prim primary key(sno,pno)); </li></ul>
  114. 114. Referential Integrity Constraints <ul><li>To establish parent child relationship between two tables having a common column, we make use of referential integrity constraints. To implement this we should define the column in the parent table as primary key and the same column in the child table as a foreign key referring to the corresponding parent entity. </li></ul>
  115. 115. Referential Integrity Constraints <ul><li>Foreign Key: a column or a combination of columns included in the definition of referential integrity which would refer to a referenced key. </li></ul><ul><li>Referenced key: it is a unique or a primary key which is defined on a column belonging to the parent table. </li></ul><ul><li>Child table: this table depends upon the values present in the referenced key of the parent table which is referred by a foreign key. </li></ul><ul><li>Parent table: this table determines whether insertion or updation of data can be done in child table. This table would be referred by child tables foreign key. </li></ul>
  116. 116. Referential Integrity Constraints <ul><li>Ex: Parent table: </li></ul><ul><li>Create table supplier_city(sno varchar2(5), city varchar2(25),constraint supp_prim primary key(sno)); </li></ul><ul><li>Ex: Child table: </li></ul><ul><li>Create table parts(sno varchar2(5) constraint part_fk references supplier_city(sno), pno varchar2(5), qty number(6), constraint part_prim primary key(sno,pno)); </li></ul>
  117. 117. Referential Integrity Constraints <ul><li>The foreign key ensures that all values in the column sno of the table Parts have corresponding values in the parent table Supplier_city. </li></ul><ul><li>The referential integrity constraint does not use foreign key keyword to identify the columns that make up the foreign key. This is because the constraint is defined at column level. The foreign key is automatically enforced on the columns. </li></ul>
  118. 118. Referential Integrity Constraints <ul><li>It is possible to define the referential integrity constraint at the table level. </li></ul><ul><li>Ex: create table city_status(city varchar2(25) constraint city_prim primary key, status number(3)); </li></ul><ul><li>Ex: alter table supplier_city add constraint city_fk foreign key(city) references city_status(city); </li></ul><ul><li>(Foreign key word is optional. It is to simplify the syntax) </li></ul>
  119. 119. On Delete Cascade <ul><li>The On Delete Cascade clause specifies that Oracle maintains referential integrity by automatically removing dependent foreign key values if a referenced primary key value is removed. </li></ul><ul><li>Syntax: create table table_name (colu1 definition, colu2 datatype constraint cons_name references parent_table_name (colu_name) on delete cascade); </li></ul>
  120. 120. On Delete Cascade <ul><li>When a relation is created between two tables, it is permissible to delete records in the child table. But a vice-versa operation, deleting records from a parent table when it references another child table is not allowed. The on delete clause will override this and allows automatic deletion of child records when a parent record is deleted. </li></ul>
  121. 121. Deferrable Constraints <ul><li>Deferred constraint checking is possible in oracle. When a constraint is made deferrable, oracle leaves the checking until the transaction is committed. </li></ul><ul><li>Each constraint has two additional attributes to support deferred checking of constraints. </li></ul><ul><li>It may be deferred or not deferred </li></ul><ul><li>The initial state may be set to initially deferred or initially immediate </li></ul>
  122. 122. Deferrable Constraints <ul><li>The three conditions that can be set are </li></ul><ul><li>Deferrable initially immediate – this checks for constraint violation at the time of insert. </li></ul><ul><li>Deferrable initially deferred – this checks for constraint violation at the time of commit. </li></ul><ul><li>Non deferrable initially immediate – this is the default condition which need not be specified. </li></ul>
  123. 123. Deferrable Constraints <ul><li>Syntax for setting the condition. </li></ul><ul><li>Alter table table_name add constraint const_name foreign key(colu_name) references parent_table_name(colu_name) deferrable initially deferred. </li></ul><ul><li>The above syntax when used will add a constraint which will by default only be checked at commit time. The constraint and the fact that it is checked at commit time, is both encapsulated within the definition of the table. </li></ul>
  124. 124. Deferrable Constraints <ul><li>To enable all constraints </li></ul><ul><li>Set constraints all immediate; </li></ul><ul><li>To disable all constraints </li></ul><ul><li>Set constraints all deferred; </li></ul><ul><li>SET CONSTRAINTS lasts for the duration of the transaction. </li></ul><ul><li>Enforcement is applied to any future inserts or updates, but does not care about data already in the table. </li></ul><ul><li>Syntax: Alter table table_name enforce constraint const_name; </li></ul>
  125. 125. Deferrable Constraints <ul><li>To drop a constraint </li></ul><ul><li>Alter table table_name drop constraint const_name; </li></ul><ul><li>Dropping a constraint will allow future insertions or other manipulations without affecting existing data in the table. </li></ul>
  126. 126. Concept Of Locking <ul><li>Oracle automatically uses different types of locks to control concurrent access of data and to prevent destructive interaction between users. Oracle automatically locks a resource on behalf of a transaction. This is done to prevent other transactions from doing something also requiring exclusive access to the same resource. Throughout its operation, oracle automatically acquires different types of locks at different levels of restrictive ness depending on the resource being locked and the operation being preformed. The lock is released automatically when an event occurs so that the transaction no longer requires the resource. </li></ul>
  127. 127. Concept Of Locking <ul><li>The concept of locking can be better understood when two users are accessing the same rows on the same table at the same time it may end up in a situation where one user might delete the row where the other is trying to update the row. To overcome this situation Oracle incorporates a locking facility which permits or denies access to other users on a table or to certain rows in a a table, when a user is still is in the process of operating on them. </li></ul>
  128. 128. Types of Locks <ul><li>Locks provide high degree of data concurrency. Locks can be acquired at two different levels. </li></ul><ul><li>Row Level Lock (For a specific row) </li></ul><ul><li>The select command when used with “for update of clause” places an exclusive lock on one or more rows of a table. This command can be used to lock the rows that would be updated later. A row level lock is applied to prevent other users from manipulating the row till the update is completed. </li></ul>
  129. 129. Types of Locks <ul><li>Ex: select * from emp where sal = 1600 for update of job; </li></ul><ul><li>The next update command will be similar to: </li></ul><ul><li>Update emp set job = ‘MANAGER’ where sal = 1600; </li></ul><ul><li>Since the rows are locked by the ‘For update’ clause no other user can update the particular row till the lock is released. Users can manipulate the other rows in the table. </li></ul>
  130. 130. Types of Locks <ul><li>Table Level Lock (For entire table) </li></ul><ul><li>A table level lock will protect table data thereby guaranteeing data integrity when data is being accessed concurrently by multiple users. A table lock can be held in several modes. </li></ul><ul><li>Share Lock </li></ul><ul><li>Share Update Lock </li></ul><ul><li>Exclusive Lock </li></ul>
  131. 131. Types of Locks <ul><li>Share Lock </li></ul><ul><li>A share lock locks the table allowing users to only query but not insert, update or delete rows in a table. Multiple users can place share locks on the same table at the same time. </li></ul><ul><li>Ex: lock table <table_name> in share mode; </li></ul>
  132. 132. Types of Locks <ul><li>Share Update Lock </li></ul><ul><li>It locks rows that are to be updated in a table. It permits other users to concurrently query, insert, update or even lock other rows in the same table. It prevents the other users from updating the row that has been locked. We can enforce a share update lock by using the ‘for update’ clause in the select statement. </li></ul><ul><li>Share update lock falls under the table level but is effective only on rows. </li></ul>
  133. 133. Types of Locks <ul><li>Share Update Lock </li></ul><ul><li>Ex: lock table <table_name> in share update mode; </li></ul><ul><li>If a row is being updated in a table, when the share update lock is applied, the table will be available for all DML manipulation except the row that is being updated. </li></ul><ul><li>It allows numerous users to concurrently lock different rows of a table. </li></ul>
  134. 134. Types of Locks <ul><li>Exclusive Lock </li></ul><ul><li>Exclusive lock is the most restrictive of all table locks. When issued by one user, it allows the other users to only query but not insert, delete or update rows in a table. Only one user can place an exclusive lock on a table at a time, whereas many users can place a share lock on the same table at the same time. Locks can be released by issuing either a rollback or commit. </li></ul><ul><li>Ex: lock table <table name> in exclusive mode; </li></ul>
  135. 135. Types of Locks <ul><li>Nowait Clause </li></ul><ul><li>When a user has locked a table without a “nowait” clause in the lock table format, if another user tries to violate the above restriction by trying to lock the table, then, he will be made to wait indefinitely until the user who has initially locked the table issues a commit or rollback statement. The delay could be avoided by appending a “nowait” clause in the lock table command. </li></ul><ul><li>Ex: lock table <table_name> in exclusive mode nowait; </li></ul>
  136. 136. Types of Locks <ul><li>Deadlock </li></ul><ul><li>A deadlock occurs when two users have a lock, each on a separate object, and, they want to acquire a lock on each other’s object. When this happens, the first user has to wait for the second user to release the lock, but the second user will not release it until the lock on the first user’s object is released. At this point both the users are at impasse and cannot continue. In such case, Oracle detects the deadlock automatically and solves the problem by aborting one of the two transactions. </li></ul>
  137. 137.   User Performing The task Locks mode permitted by another user     Establishing a share lock Share Share update Exclusive Yes No No Establishing a share update lock No Yes No Establishing an exclusive lock No No No Querying a table Yes Yes Yes
  138. 138. TABLE PARTITIONS <ul><li>Tables can be Partitioned and stored in different locations as per requirements. </li></ul><ul><li>A single logical table can be split into a number of physicallly separate pieces based on ranges of key values. Each of the parts of the table is called a PARTITION. </li></ul><ul><li>Although the partitions are held and managed independently , they can be queried and updated by reference to the name of the logical table. </li></ul><ul><li>Partitioning provides for physical security of the data. Breaking the table into smaller units minimizes the risk of data loss as only a part of the table could be lost rather than the entire table. </li></ul>
  139. 139. TABLE PARTITIONS <ul><li>A partitioned table consists of a number of pieces each having the same logical attributes. </li></ul><ul><li>There is a difference between a table which has single partition and a table that has no partitions. A non – partitioned table cannot be partitioned later. </li></ul><ul><li>Each partition is stored in a different segment and has different physical attributes. </li></ul><ul><li>Table partitions can be stored in different physical locations. We can access and manipulate data in one partition even if some or all of the other partitions are unavailable. Administrators and users can perform maintenanace operations on one partition while the data in other partitions will be available for use. </li></ul>
  140. 140. Advantages of Table Partitions <ul><li>Saves disk space & processing time. </li></ul><ul><li>Reduction in the size of the unit of failure. </li></ul><ul><li>Partition mechanism built into the server can enable set based parallel update & delete operations. </li></ul><ul><li>Allows for concurrent use of various partitions for various purposes. </li></ul><ul><li>Failure of one partition will not affect data access from other partitions. </li></ul><ul><li>Allows maintenance of selected partitions while other partitions can be available for users. </li></ul><ul><li>VLDB’s support. </li></ul>
  141. 141. Advantages of Table Partitions <ul><li>Advantages of storing partitions in different table spaces. </li></ul><ul><li>Reduces the possibility of data corruption in multiple partitions. </li></ul><ul><li>Back up & recovery of each partition can be done independently. </li></ul><ul><li>Controlling the mapping of partitions to disk drives is possible. </li></ul>
  142. 142. Creating a table partition <ul><li>Create table table_name (col1 datatype, col2 datatype) partition by range(col name) (partition part_name values less than (value), partition part_name values less than (value)); </li></ul><ul><li>Ex: create table stu_master (sid number(3) constraint stu_prim primary key, sname varchar2(15), courseid number(3)) partition by range (sid) (partition part1 values less than (100), partition part2 values less than (200)); </li></ul><ul><li>Partitioned tables CANNOT contain any column with Long, Long Raw, LOB, Object types, User defined datatypes. </li></ul>
  143. 143. Inserting records into a partitioned table <ul><li>The records are stored in the table based on the partition key specified. </li></ul><ul><li>The partition key specified in the insert statement is compared with partition bound defined when creating the partitioned table. </li></ul><ul><li>Ex: insert into stu_master values (&sid, ‘&sname’, &courseid); </li></ul><ul><li>Sid = 90, Sid = 100, Sid = 150, Sid = 200, Sid = 300 </li></ul>
  144. 144. Querying the Partitions <ul><li>Ex: </li></ul><ul><li>select * from stu_master partition (part1); </li></ul><ul><li>Select * from stu_master partition (part2); </li></ul><ul><li>First partition – Sid range 1 - 99 </li></ul><ul><li>Second partition – Sid range 100 - 199 </li></ul>
  145. 145. Additional Points <ul><li>A table can be partitioned on more than one column. The partition key specified in the insert statement is compared with partition bound defined when creating the partitioned table. </li></ul><ul><li>Ex: create table stu_master2 </li></ul><ul><li>(sid number(3) constraint stu_prim primary key , sname varchar2(15), cid number(3)) partition by range (sid,cid) (partition part1 values less than (100,10), partition part2 values less than (200,20), partition part3 values less than (maxvalue,maxvalue)); </li></ul>
  146. 146. Additional Points <ul><li>Ex: insert into stu_master2 values (&sid, ‘&sname’, &cid); </li></ul><ul><li>Sid = 90 Cid = 9, Sid = 150 Cid = 2 </li></ul><ul><li>In the second insert, inspite of Cid = 2, the record is inserted in the second partition because the LEFT PREFIX OF THE PARTITION BOUND TAKES PRECEDENCE. </li></ul><ul><li>The keyword MAXVALUE if specified in the partition bound value_list will accept all values greater than the bound specified in the previous partition. </li></ul>
  147. 147. Additional Points <ul><li>The table partitions can also be placed in tablespaces defined by the user. </li></ul><ul><li>Placing table partitions in different tablespaces is optional. </li></ul><ul><li>When the Tablespace clause is not mentioned Oracle creates the table in the default “System” tablespace. </li></ul><ul><li>Syntax: Create table tab_name(col1 definition, col2 definition) partition by range (col_name) </li></ul><ul><li>(partition part_name values less than (value) tablespace tabspc_name, partition part_name values less than (value) table space tabspc_name); </li></ul>
  148. 148. Partition Maintenance Operations <ul><li>Moving Partitions : to move a partition from one tablespace to another. </li></ul><ul><li>Syntax: alter table table_name move partition part_name tablespace tabspc_name; </li></ul><ul><li>Adding Partitions : to add a new partition after the existing last partition (high end) </li></ul><ul><li>Syntax: alter table table_name add partition part_name values less than (value); </li></ul><ul><li>The new partition is appended to the end of the table. </li></ul><ul><li>The add partition option mentioned above is only for the tables where the last existing partition has been defined with a specific key value. </li></ul>
  149. 149. Partition Maintenance Operations <ul><li>Splitting Partitions: </li></ul><ul><li>To split a partition in two. </li></ul><ul><li>To add a partition at the beginning or in the middle of a table or if the partition bound on the highest partition is MAXVALUE. </li></ul><ul><li>Syntax: alter table table_name split partition part_name at (value) into (partition part_name, partition part_name); </li></ul>
  150. 150. Partition Maintenance Operations <ul><li>Ex: alter table stu_master split partition part2 at (150) into (partition part3, partition part4); </li></ul><ul><li>Part3 – Sid range 100 – 150 </li></ul><ul><li>Part4 – Sid range 151 – 200 </li></ul><ul><li>Splitting a partition is not possible along the partition key specified as it the highest bound. </li></ul><ul><li>Ex: Splitting Part2 of table stu_master at 200. </li></ul>
  151. 151. Partition Maintenance Operations <ul><li>Dropping Partitions: </li></ul><ul><li>Syntax: alter table table_name drop partition part_name; </li></ul><ul><li>Exchanging Table partitions: </li></ul><ul><li>Exchanging table partitions is used to convert a partition into a non-partioned table, and a table into a partition of a partioned table by exchanging their data segments. </li></ul><ul><li>Useful when an application using non-partitioned table needs to be converted into partitions of a partitioned table. </li></ul>
  152. 152. Partition Maintenance Operations <ul><li>Syntax: Alter table table_name exchange partition part_name with table table_name_exch; </li></ul><ul><li>Ex: create table part_exchange (sid number(3) constraint stu_prim primary key, sname varchar2(15), courseid number(3)); </li></ul><ul><li>Insert into part_exchange values (139,’xyz’,100); </li></ul><ul><li>Alter table stu_master exchange partition part2 with table part_exchange; </li></ul><ul><li>The data exchange will take place only if the records in the table conform to the partition bound. </li></ul>
  153. 153. Synonym <ul><li>A synonym is a database object which is used as an alias for a Table,View or Sequence. </li></ul><ul><li>They are used to simplify SQL statements. </li></ul><ul><li>Hide the name and owner of an object </li></ul><ul><li>Provide location transparency for remote objects of a distributed database </li></ul><ul><li>Provide public access to an object </li></ul><ul><li>Synonyms can be used when a user creates a table or other database object and wants to grant all privileges to other users but does not want them to alter the structure or drop the table all together. </li></ul>
  154. 154. Synonym <ul><li>Synonyms can either be private or public. </li></ul><ul><li>Private Synonyms are created by normal user which is available only to that user </li></ul><ul><li>Public synonyms are created by the DBA and are available to any database user </li></ul><ul><li>Syntax: for creating a synonym </li></ul><ul><li>Create [public/private] synonym synonym_name for table_name; </li></ul>
  155. 155. Synonym <ul><li>A synonym is an alias of a table and all manipulations on it actually affects the table. </li></ul><ul><li>Users other than the creator can perform all DML operations on the Synonym in the same way that of the table but cannot perform any DDL operations on the synonym excepting for dropping the Synonym itself. </li></ul><ul><li>Querying user_synonyms will provide details of synonyms that the user has created. </li></ul>
  156. 156. Synonym <ul><li>Public Synonyms are created by a DBA to hide the identity of a base table and to reduce the complexity of SQL statements. </li></ul><ul><li>Public synonyms are owned by user group PUBLIC. When there is a public and local object with the same name, the local object takes precedence. But the public synonym can be used as usual after dropping the local object. </li></ul><ul><li>An example of a public synonym is TAB which is used to view the tables owned by the user. </li></ul>
  157. 157. Sequences <ul><li>A Sequence is a database object, which can generate unique, sequential integer values. It can be used to automatically generate primary key values. A sequence can be either in an ascending or a descending order. </li></ul><ul><li>Syntax: create sequence seq_name [increment by n] [start with n] [minvalue n] </li></ul><ul><li>[maxvalue n] [cycle/nocycle] [cache/nocache]; </li></ul>
  158. 158. Sequences <ul><li>Increment by n: ‘n’ is an integer which specifies the interval between the sequence numbers. The default is 1. If n is positive, then the sequence ascends and if it is negative the sequence descends. </li></ul><ul><li>Start with n: specifies the first sequence numbers to be generated. </li></ul><ul><li>Minvalue n: specifies the minimum value of the sequence. By default it is 1 for an ascending sequence and 10e26-1 for a descending sequence. </li></ul><ul><li>Maxvalue n: it specifies the maximum value that the sequence can generate. By default, it is –1 for descending & 10e27-1 for ascending sequence. </li></ul>
  159. 159. Sequences <ul><li>Increment by n: ‘n’ is an integer which specifies the interval between the sequence numbers. The default is 1. If n is positive, then the sequence ascends and if it is negative the sequence descends. </li></ul><ul><li>Start with n: specifies the first sequence numbers to be generated. </li></ul><ul><li>Minvalue n: specifies the minimum value of the sequence. By default it is 1 for an ascending sequence and 10e26-1 for a descending sequence. </li></ul><ul><li>Maxvalue n: it specifies the maximum value that the sequence can generate. By default, it is –1 for descending & 10e27-1 for ascending sequence. </li></ul>
  160. 160. Sequences <ul><li>Cycle: Specifies that the sequence continues to generate values from the beginning after reaching either its max or min value. </li></ul><ul><li>No cycle: specifies that the sequence cannot generate more values after reaching either its maxvalue or minvalue. The default value is ‘no cycle’. </li></ul><ul><li>Cache: the cache option pre allocates a set of sequence numbers and retains them in the memory so that sequence numbers can be accessed faster. When the last of the sequence numbers in the cache has been used, oracle reads another set of numbers into the cache. </li></ul>
  161. 161. Sequences <ul><li>Nocache: the default value ‘nocache’, does not preallocate sequence numbers for faster access. </li></ul><ul><li>Ex: to generate student id numbers automatically without any duplicates, we can create a sequence and use it in the insert statement. </li></ul><ul><li>Create sequence sidseq Increment by 1 start with 1 minvalue 1 maxvalue 100 cycle cache 40 </li></ul><ul><li>After creating a sequence its values can be accessed with the help of the pseudo columns like currval and nextval. A pseudo columns behave like table columns, but it is not actually stored in the table. We can select values from pseudo columns but cannot perform manipulations on their values. </li></ul>
  162. 162. Sequences <ul><li>Nextval: nextval returns initial value of the sequence, when referred to, for the first time. Later references to nextval will increment the sequence using the increment by clause and return the new value. </li></ul><ul><li>Currval: returns the current value of the sequence which is the value returned by the last reference to nextval. </li></ul><ul><li>Ex: insert into stu_master (sid,sname) values (‘s’||sidseq.nextval, ‘rajat’); </li></ul><ul><li>To obtain the current sequence number: </li></ul><ul><li>Syntax: Select seq_name.currval from dual; </li></ul><ul><li>Ex: select sidseq.currval from dual; </li></ul>
  163. 163. Sequences <ul><li>Sequences are altered to </li></ul><ul><li>Set or eliminate minvalue or maxvalue </li></ul><ul><li>Change the increment value </li></ul><ul><li>Change the number of cached sequence numbers </li></ul><ul><li>Ex: alter sequence sidseq maxvalue 150; </li></ul><ul><li>Rules for sequences: </li></ul><ul><li>Ascending Sequence: Minvalue should be less than Maxvalue </li></ul><ul><li>Descending Sequence: Maxvalue should be less than Minvalue. </li></ul><ul><li>USER_SEQUENCES will provide details about sequences created by users. </li></ul>
  164. 164. View <ul><li>A view is a tailored presentation of the data contained in one or more tables or other views. </li></ul><ul><li>A view takes the output of a query and treats it as a table. Therefore, a view can be thought of as a “stored query” or a “virtual table”. </li></ul><ul><li>The tables on which views are built are called base tables. </li></ul><ul><li>Views can be used in most of the places where tables can be used. </li></ul>
  165. 165. View <ul><li>Advantages of views: </li></ul><ul><li>They provide an additional level of table security by restricting access to a predetermined set or rows and columns of a table. </li></ul><ul><li>They hide data complexity. </li></ul><ul><li>Ex: A single view might be defined with a join, which is a collection of related columns or rows in multiple tables. However, the view hides the fact that this information actually originates from several tables. </li></ul><ul><li>They simplify commands for the user because they allow the to select information from multiple tables without actually knowing how to perform a join. </li></ul><ul><li>View’s isolate applications from changes in the base table. Views can provide a different view of the table by renaming the columns without affecting the table. </li></ul>
  166. 166. View <ul><li>Syntax for creating a view: </li></ul><ul><li>Create [or replace] [{no}{force}] view <view_name> [column alias_name] as <query> [with {check option} {read only} {constraint}]; </li></ul><ul><li>Ex: </li></ul><ul><li>Create view emp_v1 as select * from emp1; </li></ul><ul><li>Create view emp_v2 as select empno,ename,sal from emp1; </li></ul><ul><li>Create view emp_v3 as select * from emp1 where deptno = 10; </li></ul>
  167. 167. View <ul><li>With check option: </li></ul><ul><li>Create or replace view emp_v3 as select * from emp1 where deptno = 10 with check option constraint empv; </li></ul><ul><li>Update emp_v3 set deptno = 20 where deptno = 10; </li></ul><ul><li>Error: check option violation </li></ul><ul><li>View with specific columns: </li></ul><ul><li>Create view emp_v4 as select empno,sal,comm from emp1; </li></ul><ul><li>Update emp_v4 set deptno = 40; </li></ul><ul><li>Error: invalid column name </li></ul>
  168. 168. View <ul><li>With Read only option: </li></ul><ul><li>Create or replace view emp_v3 as select * from emp1 with read only; </li></ul><ul><li>Update emp_v3 set deptno = 20; </li></ul><ul><li>Error: virtual column not allowed here. </li></ul><ul><li>Creating Views with errors: </li></ul><ul><li>If there are no syntax errors in a create view statement, oracle will create view evn if the view defining query refers to a non – existent table or an invalid column of an existing table, or when the view owner does not have the required privileges. The view will anyway be created and entered into the data dictionary. The view is considered created with errors. </li></ul>
  169. 169. View <ul><li>With Force option: to create a view with errors, include the Force option in the Create View command: </li></ul><ul><li>Ex: Create force view force_v as select * from dept1; </li></ul><ul><li>When we create a table with the name Dept1, the view is automatically recompiled and becomes valid. </li></ul><ul><li>Manual recompilation: </li></ul><ul><li>Alter view force_v compile; </li></ul>
  170. 170. View <ul><li>Join Views: joining of tables is possible in view. </li></ul><ul><li>Ex: create empdept_v as select empno,ename,sal,e.deptno,dname from emp e,dept d where e.deptno = d.deptno; </li></ul><ul><li>View’s cannot select pseudo columns like currval & nextval. </li></ul><ul><li>If a view’s query contains joins, set operators, group functions and distinct clause the deletion, updation and insertion cannot be performed. </li></ul><ul><li>The changes performed in the view will affect the base table and vice versa. </li></ul><ul><li>Group functions and group by clause can also be included in the views. </li></ul><ul><li>While using functions the column must be given an alias name. </li></ul>
  171. 171. View <ul><li>Partition View: With partition views the data resides in separate tables. These tables are brought together at runtime using the operator union all. </li></ul><ul><li>Syntax: create view <view name> as </li></ul><ul><li>Select * from table_name1 union all </li></ul><ul><li>Select * from table_name2 union all </li></ul><ul><li>Select * from table_name3; </li></ul><ul><li>Dropping Views: </li></ul><ul><li>Syntax: drop view view_name; </li></ul><ul><li>DD table USER_VIEWS will provide details on views that user has created </li></ul>
  172. 172. INDEX <ul><li>Indexes are optional structures associated with tables. Indexes are created explicitly to speed up SQL statement execution on a table. Oracle index provides a faster access to table data, it affects only the speed of execution. </li></ul><ul><li>The index points directly to the location of the rows containing the value. The absence or presence of an index does not require a change in the working of any SQL statement. </li></ul>
  173. 173. INDEX <ul><li>An index is created on a column or a combination of columns using Create Index command. </li></ul><ul><li>Syntax: </li></ul><ul><li>Create index index_name on table_name(column_name); </li></ul><ul><li>When an index is created, Oracle fetches and sorts the columns to be indexed, and stores the ROWID along with the index value for each row. </li></ul>
  174. 174. INDEX <ul><li>Indexes are physically & logically independent of the data in the associated table. </li></ul><ul><li>Indexes can be created or dropped without affecting the base table. </li></ul><ul><li>Indexes are independent structures & require storage space. </li></ul><ul><li>Oracle automatically maintains & uses indexes once they are created. </li></ul><ul><li>Oracle automatically reflects changes to data, such as addition or new rows, deletion,updating rows, in all relevant indexes with no additional action by users. </li></ul>
  175. 175. UNIQUE INDEXES <ul><li>Unique indexes guarantee that no two rows of a table have duplicate values in the columns that define the index. </li></ul><ul><li>Syntax: create unique index index_name on table_name(column_name); </li></ul><ul><li>A unique index is automatically created when we create unique or primary key constraint. Alternatively a constraint is imposed on the column we create unique index. </li></ul><ul><li>We cannot create index for a column which is already indexed. </li></ul>
  176. 176. COMPOSITE INDEXES <ul><li>A composite index is an index created on multiple columns of a table. Columns in a composite index can appear in any order and need not be adjacent columns of a table. </li></ul><ul><li>Syntax: create index index_name on table_name(col_name1, col_name2); </li></ul><ul><li>Composite index can enhance the retrieval speed of data for select statements in which the ‘where’ clause references all or the leading portion of the columns in the composite index. Generally, the most commonly accessed or most selective columns go first in the order of the column list. </li></ul>
  177. 177. REVERSE KEY INDEXES <ul><li>Creating a reverse key index, when compared to a standard index, reverses each byte of the column being indexed while keeping the column order. </li></ul><ul><li>Syntax: create index index_name on table_name(column_name) reverse; </li></ul><ul><li>REBUILD a reverse key index into normal index using the keyword NOREVERSE. </li></ul><ul><li>Syntax: Alter index index_name rebuild noreverse; </li></ul><ul><li>We cannot rebuild a normal index as a reverse key index. </li></ul>
  178. 178. BITMAP INDEXES <ul><li>The advantages for Bitmap Indexes are greatest for low cardinality columns. Columns in which the number of distinct values is small compared to the number of rows in a table. </li></ul><ul><li>Syntax: create bitmap index index_name on table_name(column_name); </li></ul><ul><li>Bitmap indexes can dramatically improve query performance. AND and OR conditions in the where clause of a query can be quickly resolved by performing the corresponding Boolean operations directly on the bitmaps before converting the resulting bitmap into rowids. </li></ul>
  179. 179. INDEX ORGANIZED TABLES <ul><li>An index organized table differs from a regular table in that the data for the table is held in its associated index. Changes to the table data, updation,insertion, deletion result only in updating the index. </li></ul><ul><li>EX: create table table_index (sid number(6) primary key, sname varchar2(20)) organization index; </li></ul><ul><li>Index organized table should have a primary key column, and that column will be the indexed column. </li></ul>
  180. 180. PARTITIONING IN INDEX <ul><li>Syntax for creating a partitioned index: </li></ul><ul><li>Create index index_name on table_name (col1,col2) </li></ul><ul><li>[Global/Local] partition by range (col2)) </li></ul><ul><li>partition values less than (value) tablespace tabspc_name1, Partition values less than (value) tablespace tabspc_name2; </li></ul><ul><li>In the case of global indexes the keyword global need not be specified as it is present by default. </li></ul>
  181. 181. LOCAL INDEXES <ul><li>Syntax: Create index index_name on table_name (column_name) local; </li></ul><ul><li>In a local index, the partition key in the partition index should refer to the same rows as that of the underlying table partition. </li></ul><ul><li>A local index can be created on a partition by specifying the LOCAL attribute in the Create Index statement. </li></ul><ul><li>DD table User_segments gives details of partition name,segement type & tablespace name. </li></ul>
  182. 182. GLOBAL INDEXES <ul><li>The keys of a global index may refer to the rows stored in more than one underlying partitions. An index is global prefixed if it is partitioned on the left prefix of the index columns. </li></ul><ul><li>Ex: </li></ul><ul><li>A table partition based on more than one column. If the index is created on the first column then it is said to be prefixed. On the other hand, if the indexing is done on the second column then an error message is displayed that the global index should be prefixed. </li></ul>
  183. 183. GLOBAL INDEXES <ul><li>Create table stu_master(sid number(6), sname varchar2(20)) partition by range (sid)) </li></ul><ul><li>(partition part1 values less than (100), </li></ul><ul><li>Partition part2 values less than (200), </li></ul><ul><li>Partition part3 values less than (maxvalue)); </li></ul><ul><li>Create index stu_index on stu_master(sid) global partition by range (sid)) </li></ul><ul><li>(partition a values less than (150), </li></ul><ul><li>Partition b values less than (maxvalue)); </li></ul>
  184. 184. GLOBAL INDEXES <ul><li>Create table stu_master2(sid number(6), sname varchar2(20)) partition by range (sid,sname)) </li></ul><ul><li>(partition part1 values less than (100, ‘xyz’), </li></ul><ul><li>Partition part2 values less than (200, ‘abc’), </li></ul><ul><li>Partition part3 values less than (maxvalue,maxvalue)); </li></ul><ul><li>Create index stu_index2 on stu_master(sid,sname) global partition by range (sname)) </li></ul><ul><li>(partition a values less than (‘xyz’), </li></ul><ul><li>Partition b values less than (maxvalue)); </li></ul><ul><li>Error: global partitioned index must be prefixed. </li></ul>
  185. 185. INDEX PARTITIONS VS TABLE PARTITIONS <ul><li>Unlike table partitions movement of the index partitions requires individual reconstruction of the index or each partition (only in the case of global index). </li></ul><ul><li>Alter index index_name rebuild partition part_name; </li></ul><ul><li>Index partitions cannot be dropped manually. They are dropped when the data they refer to is deleted from the partitioned table. </li></ul><ul><li>In the partition index the rows are also truncated when their main rows from the relevant partitions are also truncated. </li></ul><ul><li>DD tables User_indexes, user_ind_partitions, user_ind_columns provide details about indexes. </li></ul>
  186. 186. OBJECT ORIENTED PROGRAMMING <ul><li>Object Oriented Programming is a way to organize data and the code within program. </li></ul><ul><li>A fruit is a CLASS. </li></ul><ul><li>Apple is an OBJECT of the CLASS fruit. </li></ul><ul><li>COLOUR is an ATTRIBUTE of the OBJECT Apple. </li></ul><ul><li>The action Cutting the Apple is a METHOD/FUNCTION of the OBJECT Apple of the CLASS Fruit. </li></ul><ul><li>Object : An object is a reusable component that developers need to be aware of, rather than how it works. Objects are basic entities in a system. They could represent any item handled by a program. An ATTRIBUTE could be any property of the object. </li></ul>
  187. 187. OBJECT ORIENTED PROGRAMMING <ul><li>Class : Class refers to the definition of an Object. Class helps in making the entire set of data and code into a single user defined datatype. Once the class has been defined, a number of Objects that belong to the same class can be treated. </li></ul><ul><li>Attributes : Attributes help in identifying an object. These are implemented in the form of variable declarations made within the object clss definition. </li></ul><ul><li>Methods : Methods are functions and procedures that are used to perform actions related to the object. Anything that is to be done with the object is implemented as a method. </li></ul>
  188. 188. Features of Object Oriented Programming <ul><li>a. Abstraction b. Encapsulation </li></ul><ul><li>c. Inheritance d. Polymorphism </li></ul><ul><li>Example using C++ </li></ul><ul><li>Base Class Derived Class </li></ul><ul><li>Class A Class B:Public A </li></ul><ul><li>{ { </li></ul><ul><li>Public; int b ; </li></ul><ul><li>Int a = 10; } </li></ul><ul><li>Void display ( ) main ( ) </li></ul><ul><li>{ { B x; </li></ul><ul><li>Cout << “a= ”<<a; x.display ( ); </li></ul><ul><li>} } </li></ul><ul><li>} </li></ul>
  189. 189. Features of Object Oriented Programming <ul><li>Declaration </li></ul><ul><li>Main ( ) int sum(int x, int y) </li></ul><ul><li>{ { </li></ul><ul><li>Int x; return x+y; </li></ul><ul><li>X = sum(10,20); } </li></ul><ul><li>Cout << “x= ”<<x; float sum (float x,float y) </li></ul><ul><li>Y = sum(10.5,11.2); { </li></ul><ul><li>Cout <<“y= ”<<y; return x+y </li></ul><ul><li>} } </li></ul>
  190. 190. Features of Object Oriented Programming <ul><li>Abstraction : Hiding the DATA and the METHODS from another CLASS. DATA values and associated METHODS defined in a CLASS(BASE CLASS) can be called in another CLASS (DERIVED CLASS) but the BASE CLASS details is not revealed. </li></ul><ul><li>Encapsulation : the wrapping of DATA and FUNCTION/METHOD into a single unit is known as Encapsulation. In other words Encapsulation means binding the DATA with associated METHODS. The virtual container which is Encapsulated is a CLASS. </li></ul><ul><li>The real time instance of a CLASS or physical existence of a CLASS is an OBJECT. Encapsulation of DATA from direct access by the program is called ‘DATA HIDING’ </li></ul>
  191. 191. Features of Object Oriented Programming <ul><li>Inheritance : Deriving the features from one CLASS to another. In other words, reusing the capabilities of already defined CLASS. Inheritance allows desigining of new objects that inherit the functionality of objects that are already created. Inheritance provide the idea of reusability. </li></ul><ul><li>Polymorphism : Providing many functionalities with single name and different prototype. The ability of an OBJECT to take more than one form is known as Polymorphism. It enables different OBJECTs to have methods of the same name that accomplish similar tasks, but in different ways. </li></ul>
  192. 192. Advantages of Object Oriented in Oracle <ul><li>Oracle 8.0 and further versions are Object Relational Database Management System, since they are an extension of the traditional relational database where the object oriented structures & concepts such as Abstract Type, Varrying Arrays, Nested Tables.. Are included. </li></ul><ul><li>Object reuse: If object oriented database objects are created the database objects can be reused to create other database objects. </li></ul><ul><li>Standard Adherence: if multiple applications use the same set of database objects, then a set of standards for the database objects can be created. </li></ul><ul><li>Defined access path: for each object procedures and functions can be defined which act on them. The data and the method that can be accesing this object can be united. </li></ul>
  193. 193. Object Oriented Database Objects <ul><li>Abstract Data Types : abstract data types are datatypes that consist of one or more subtypes. Thay can more accurately describe the data. </li></ul><ul><li>Ex: create or replace type address_ty as object (street_no number(3),street_name varchar2(20), city varchar2(20), state varchar2(20)); </li></ul><ul><li>The above example creates a Type, which is named as address_ty. </li></ul><ul><li>The abstract datatype that has been created can be used to represent a column in a table. </li></ul><ul><li>The data type can be used to create object tables. In an object table the columns of the table map to the columns of the abstract data type. </li></ul>
  194. 194. Object Oriented Database Objects <ul><li>Abstract Data Types : </li></ul><ul><li>The create type command is the most important in Object Relational Database. This will create an abstract data type named as address_ty. The as object clause explicitly identifies address_ty as an object oriented implementation. The replace clause recreates the type if it already exists. This clause can be used to change the definition of an existing type without dropping it. Once an abstract datatype has been created it can be used within other data objects. </li></ul>
  195. 195. Object Oriented Database Objects <ul><li>Abstract Data Types : </li></ul><ul><li>When an abstract type is created, then a standard representation of abstract data elements is created. If the same abstract data type is used in multiple places then there is a guarantee that the same logical data is represented in the same manner. The reuse of the abstract data type leads to the enforcement of standard representation for the data. </li></ul>
  196. 196. Object Oriented Database Objects <ul><li>Insertion of values into Abstract Data Types : </li></ul><ul><li>Values cannot be inserted directly into the abstract data type. In order to store data we have to create a table that uses the data type. The data type then be stored in the table in the format specified. Hence the values cannot be directly inserted into the abstract data type. The abstract data type has to be used in a table and only then can values be stored in it. </li></ul>
  197. 197. Object Oriented Database Objects <ul><li>Implementing an object type as a column object : </li></ul><ul><li>Ex: Create table stu_master (sid number(5), sname varchar2(25), stu_address address_ty, tel_no number(8)); </li></ul><ul><li>Ex: desc stu_master </li></ul><ul><li>The same can be viewed through the DD table called user_tab_columns. </li></ul><ul><li>Ex: select column_name,data_type from user_tab_columns where table_name = ‘STU_MASTER’; </li></ul>
  198. 198. Object Oriented Database Objects <ul><li>When a user wants to view the actual values of the stu_address data type then a query using the DD table user_type_attrs can be used. </li></ul><ul><li>Ex: select attr_name, length, attr_type_name from user_type_attrs where type_name = ‘ADDRESS_TY’; </li></ul><ul><li>Inserting records into abstract data types : </li></ul><ul><li>Ex: insert into stu_master values (101, ‘ritvik’, address_ty(100, ‘ameerpet’, ‘hyderabad’, ‘AP’), 2334456); </li></ul>
  199. 199. Object Oriented Database Objects <ul><li>Selecting records from abstract data types : </li></ul><ul><li>Ex: select * from stu_master; </li></ul><ul><li>If the user wants to query only the city name from the stu_master then a normal query would raise an error. </li></ul><ul><li>Ex: select city from stu_master; </li></ul><ul><li>Error: invalid column name </li></ul><ul><li>The abstract data type cannot be queried directly because it is a data type and not a table. The dot notation has to be used for the purpose of querying i.e., column.attribute. </li></ul><ul><li>Ex: select from stu_master a; </li></ul>
  200. 200. Object Oriented Database Objects <ul><li>Updating records in abstract data types : </li></ul><ul><li>Updating is done similar in a similar manner to that of the select statement. The dot notation and an alias name of the table is used. </li></ul><ul><li>Ex: update stu_master a </li></ul><ul><li>Set a.stu_address.street_no = 10 </li></ul><ul><li>where sname = ‘AJAY’; </li></ul><ul><li>Deleting record from abstract data types: </li></ul><ul><li>To delete records that contain abstract data types use the dot notation and the table alias name. </li></ul><ul><li>Ex: delete from stu_master a </li></ul><ul><li>Where a.stu_address.state = ‘AP’; </li></ul>
  201. 201. Object Oriented Database Objects <ul><li>Dropping object types : dropping of object types can be done in the same manner as other data base objects. However there should be no dependencies on the object type. </li></ul><ul><li>Ex: drop type address_ty </li></ul><ul><li>Error: cannot drop or replace a type with type or table dependencies. </li></ul><ul><li>The compiler informs that the object type cannot be changed or dropped without dropping the other objects dependent on it. However the FORCE clause can be used to forcefully drop an object type. </li></ul>
  202. 202. Object Oriented Database Objects <ul><li>Dropping object types with FORCE clause : </li></ul><ul><li>Ex: drop type address_ty force; </li></ul><ul><li>Using the force option to drop type with dependencies is not recommended. The operation is not recoverable and the data in the table could become inaccesible. </li></ul><ul><li>DD views which list the information about direct dependencies and dependency management are </li></ul><ul><li>User_dependencies </li></ul><ul><li>All_dependencies </li></ul><ul><li>Dba_dependencies </li></ul>
  203. 203. Object Oriented Database Objects <ul><li>Security for abstract data types : </li></ul><ul><li>When two users create two different abstract data types, each of these users can grant the other privilege of using the abstract data type. however., when selecting, inserting, deleting the dot notation should be used. Here the user name has to be specified in addition to the column attribute notation. </li></ul>
  204. 204. Object Oriented Database Objects <ul><li>Indexing abstract data types : </li></ul><ul><li>Ex: create index streetnum on stu_master (stu_address.street_no); </li></ul><ul><li>Once an index is created, it can be checked using the data dictionary table - all_indexes </li></ul><ul><li>Ex: select owner,index_name,index_type,table_owner,table_name,table_type from all_indexes where owner = ‘SCOTT’; </li></ul>
  205. 205. Object Oriented Database Objects <ul><li>Varying Arrays : these help in storing repeating attributes of a record in a single row. Varying arrays have a fixed lower value(0) and a flexible upper value(it could be any valid number). Varying arrays cannot be extended beyond the limit that was defined when the Varying array was created. Collectors are a set of elements that are treated as a part of a single row. Collectors such as Varying array allow repetition of only those column values that change potentially, saving storage space. </li></ul>
  206. 206. Object Oriented Database Objects <ul><li>Creating a Varying Array: </li></ul><ul><li>Ex: create type cid as varray(5) of number(5); </li></ul><ul><li>Ex: create type cname as varray(5) of varchar2(15); </li></ul><ul><li>The above command creates a Varying Array that could contain a maximum of 5 values and each value could be of data type varchar2. </li></ul><ul><li>Creating a table using the varying array : </li></ul><ul><li>Ex: create table stu_detail(sid number(8), sname varchar2(25), course_id cid, course_name cname); </li></ul>
  207. 207. Object Oriented Database Objects <ul><li>Ex: Desc stu_details </li></ul><ul><li>To ascetain the kind of data type that is stored in each column the user_types data dictionary view can be queried. </li></ul><ul><li>Ex: Select typecode,attributes from user_types where type_name = ‘CID’; </li></ul><ul><li>The DD view user_coll_types gives the characteristics of the varying array including the upper bound, collection type, data type including the abstract data type on which it is based. </li></ul>
  208. 208. Object Oriented Database Objects <ul><li>Inserting records into Varying Arrays: </li></ul><ul><li>Ex: insert into stu_detail values (220212, ‘satish’, cid(10,20,30),cname(‘ora’, ‘dwh’, ‘d2k’)); </li></ul><ul><li>When values are inserted into the varying array care should be taken such that the maximum limit is not exceeded. </li></ul><ul><li>Selecting data from varying array: </li></ul><ul><li>The varying array cannot be queried directly using the select command. A cursor FOR loop should be used. </li></ul><ul><li>Ex: select * from stu_detail; </li></ul><ul><li>Ex: select course_name from stu_detail; </li></ul>
  209. 209. Object Oriented Database Objects <ul><li>Nested Tables: Nested Tables have no limit on the number of entries per row. A nested table is a table within a table. A table is represented as a column within another table. Multiple rows can be present in the nested table for each row in the main table. </li></ul><ul><li>Ex: create type stu_ty as object (cid number(5), cname varchar2(15), course_fees number(7,2)); </li></ul><ul><li>To use this data as the basis for a nested table a new abstract data type has to be created </li></ul><ul><li>Ex: create type stu_nt as table of stu_ty; </li></ul>
  210. 210. Object Oriented Database Objects <ul><li>The as table of clause assumes that this type will be used as the basis of a nested table. The type stu_nt can be used to create the table stu_detail. </li></ul><ul><li>Ex: create table stu_detail (sid number(8), sname varchar2(25), course_detail stu_nt) nested table course_detail store as stu_nested_table; </li></ul><ul><li>The nested table is stored in a table that is named stu_nested_table. The data in the nested table is not stored with the rest of the table’s data. It is stored apart from the main table. Oracle maintains pointers between the main table and the nested table. </li></ul>
  211. 211. Object Oriented Database Objects <ul><li>To see the structure of the table: </li></ul><ul><li>Ex: desc stu_detail or </li></ul><ul><li>Ex: select column_name, data_type from user_tab_columns where table_name = ‘STU_DETAIL’; </li></ul><ul><li>To verify if stu_nt is a type: </li></ul><ul><li>Ex: select typecode, attributes from user_types where type_name = ‘STU_NT’; </li></ul>
  212. 212. Object Oriented Database Objects <ul><li>To check the data types used as the basis for nested tables: </li></ul><ul><li>Ex: select coll_type, elem_type_owner, elem_type_name, upper_bound, length from user_coll_types where type_name = ‘STU_NT’; </li></ul><ul><li>To see the attributes of stu_ty: </li></ul><ul><li>Ex: select attr_name, length, attr_type_name from user_type_attrs where type_name = ‘STU_TY’; </li></ul>
  213. 213. Object Oriented Database Objects <ul><li>Querying Nested Tables: </li></ul><ul><li>A nested table is a column inside a table. To support queries of columns and rows of a nested table, Oracle provides a keyword (THE). A relational table is in existence in the database and hence can be queried whereas a nested table is a datatype and does not exist till it is used in a relational table and hence cannot be queried. In order to query the nested table the nested table is flattened using the THE function. </li></ul><ul><li>Ex: select NT.cid, NT.cname, NT.course_fees, from the (select course_detail from stu_detail) NT; </li></ul>
  214. 214. Object Oriented Database Objects <ul><li>‘ THE’ function can be used to perform Inserts or Updates directly against the Nested Table without necessarily requiring inserts into the main table. </li></ul><ul><li>Ex: insert into the (select course_detail from stu_detail where sid = 220234) </li></ul><ul><li>values (stu_ty (10, ‘oracle’, 3000)); </li></ul><ul><li>Ex: update the (select course_detail from stu_detail where sid = 220234) set course_fees = 3500 where cid = 10; </li></ul>
  215. 215. Object Oriented Database Objects <ul><li>When records already exist in a nested table and records are to be inserted into the main table, the key words CAST & MULTISET have to be used together. </li></ul><ul><li>The CAST keyword allows casting of the result of a query in the form of a nested query. </li></ul><ul><li>The MULTISET keyword allows the cast query to contain multiple records. </li></ul><ul><li>Ex: insert into stu_detail values (220235, ‘vinod’, cast(multiset(select * from the (select order_detail from stu_detail where sid = 220234)) as stu_nt)); </li></ul>
  216. 216. Object Oriented Database Objects <ul><li>Nested tables Vs Varying Arrays: </li></ul>