PT- Oracle session01


Published on

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

PT- Oracle session01

  1. 1. SESSION 01TOPICS1.Introduction Retrieving2.Information from Database Tables3.Simple Functions4.Storing and Processing Dates and Times5.SQL* Plus6.Sub QueriesDATABASE Collection of data It a structure for storing information. They are typically organized into tables, having collections of records You may think of a table as a collection of columns and rows. For example, a table named Employee might contain the EmpID, Empname, Empsalary, Empdesigination and other information about that employee. Each row is called a data record and each column is called a record field.RELATIONAL DATABASEA relational database is a database that conforms to the relational model, andrefers to a databases data and schema (the databases structure of how that datais arranged).
  2. 2. DBMS stands for Database Management System.It is a collection of interrelated data and a set of program to access the data.The main objective is to provide a convenient environment to retrieve and storethe data from the database.RDBMS stands for Relational Database Management System.Itis a software program used to create the database and it allows you toenter, manipulate, and retrieve dataRDBMS also structured in database tables, fields and records.Most RDBMS use SQL as database query language.DIFFERENCE BETWEEN DBMS and RDBMSBoth are used to organize data and provide retrieval of data.DBMS permits only one person to access the database at a given time; on theother hand RDMS allows many users simultaneous access to the database.RDBMS leads DBMS inSecurity wiseUsed in large organizations to store large amount of data.It has quick retrieval mechanisms.TYPES OF RDBMSSYBASEMS SQL SERVERMYSQLMicrosoft ACCESSDB2TERADATA
  3. 3. RULES FOR WRITING SQL STATEMENTSSQL statements are not case sensitive.SQL statements can be on one or more lines.Keywords cannot be abbreviated or split across lines.Clauses are usually placed on separate lines.Tabs and indents are used to enhance readability.All SQL statements must be terminated by ";"TOOLS IN ORACLESQL*PLUS - An Interface between the database and the languages.SQL*LOADER - An Interface to load the datas from Flat files.STEPS TO WORK ON ORACLE:STEP 1 – ENTER INTO SERVER
  5. 5. SQL – STRUCTURED QUERY LANGAUGE •It is used to communicate with a database. •It is a language that provides an interface to relational database systems. •It is based on the work of Dr.E.F.Codd, in his first implementation SQL was developed by IBM in the mid 1970’s. •IBM was conducting a research project known as system R, and SQL was born from that research project •Laterin 1979, a company then known as Relational Software Inc. (now called as Oracle Corporation) released the first commercial version of SQL •SQL is fully standardized by ANSI (American National Standards Institute), and it is the Standard language for relational database management systems •You can use SQL to access Oracle, SQL server, Sybase, MySQL server. DB2,ADVANTAGES OF SQLSQL provides a variety of tasks, including: •Querying data from the database •Inserting, updating, and deleting rows in a table •Creating, replacing, altering, and dropping objects •Controlling access to the database and its objects.TYPES OF SQL STATEMENTS(a)QUERY STATEMENT(b)DML(c)DDL(d)DCL(e)TCL
  6. 6. (a) Query StatementsSelect statement.To display the contents from a table.Syntax: select column_name from table_name; Select * (view all columns) from table_name;CLAUSES IN SELECT STATEMENT(A) WHEREThe WHERE clause allows you to filter the results from an SQL statement.Syntax:Select col1, col2 from tab where col1>n;(B) GROUP BYThe GROUP BY clause can be used in a SELECT statement to collect data acrossmultiple records and group the results by one or more columns.Syntax:Select col1, col2, avg (col1) from tab group by col1, col2;(C) HAVINGThe HAVING clause is used in combination with the GROUP BY clause. It can be usedin a SELECT statement to filter the records that a GROUP BY returns.
  7. 7. Syntax:Select col1, col2, avg (col1) from tab group by col1, col2 having avg(col1)>n;(D) ORDER BYThe ORDER BY clause allows you to sort the records in your result set. The ORDERBY clause can only be used in SELECT statements.Syntax:Select col1, col2 from tab order by [col1/col2];Example includes all clauses.Select col1, col2, avg (col1) from table_name where col1 >n group by col1, col2having avg (col1)>n order by avg (col1)USING SIMPLE FUNCTIONS •SINGLE ROW FUNCTION •AGGREGATE FUNCTIONSINGLE ROW FUNCTION - it takes single input and has an output for eachinput.
  8. 8. TYPES OF SINGLE ROW FUNCTIONSCHARACTER:Takes character input and returns character or number as an outputExample: select Chr (65) from dual;NUMERIC:Performs calculations and returns number as an output.Example: select mod (24, 4) from dual;DATE:To process dates and times.Example: select last_day (sysdate) from dual;CONVERSIONS:To convert one data type to another.Example: select to_char (sysdate,month yyyy dd ) from dual;REGULAR EXPRESSION:Allow to provide regular expression when searching data.Example: select regexp_instr (this is a good notes for you, g[[ :alpha: ]] {3} )from dual;AGGREGATE FUNCTIONS It can take many inputs and return single output.Example: select avg (col), sum (col), max (col) from table_name group by col;STORING AND PROCESSING DATES AND TIME* CONVERTS DATETIMES USING TO_CHAR () AND TO_DATE ().
  9. 9. * DATETIME FUNCTIONS.* TIME ZONES.* TIMESTAMPS.* TIME INTERVALS.DEFAULT DATE FORMATDD-MON-YY.Syntax: select sysdate from dual;CONVERSION FUNCTIONTO_DATE ():Converts the given string to a DATE format.Syntax: select to_date (01/jan/2009, dd/mon/yyyy) from dual;TO_CHAR ():Converts the datetime or number to a String format.Syntax: select to_char (to_date (01/jan/2009, dd/mon/yyyy), month dd, yyyy) from dual;
  10. 10. DTIME FUNCTION(i) ADD_MONTHS(x, y):Returns the result of adding y months to x. If x is negative, y months are subtractedfrom x.Syntax: select add_months (01-jan-2009, 3) from dual;(ii) LAST_DAY(x):Returns the last day for the month (x).Syntax: select last_day (01-jan-2009) from dual;(iii) NEXT_DAY(x, day):Returns the datetime of the next day following (x).Syntax: select next_day (01-jan-2009, Sunday) from dual;(iv) ROUND(X, [UNIT]):By default, x is rounded to the beginning of the nearest day. If you use an optionUNIT string, x is rounded to that UNIT.Syntax: select round (to_date (01-dec- 2009), yyyy) from dual;
  11. 11. (v) SYSDATE ():Returns the Current Date from the database.Syntax: select sysdate from dual;(vi) TRUNC(x, [UNIT]):By default, x is truncated to the beginning of the day. If you use an option UNITstring, x is truncated to that UNIT.Syntax: select trunc (to_date (06-jun-2009), yyyy) from dual;Changing the Session Time Zone:Syntax: alter session set time_zone = PST;Syntax: select sessiontimezone from dual;Timestamps:* A timestamp can store a fractional second.* A timestamp can store a time zone.TIME INTERVAL:It is a data type that allows storing time intervals.
  12. 12. TIME INTERVAL TYPES:(i) Interval Year to Month Type.Syntax: create table table_name (column_name INTERVAL YEAR (3) TO MONTH);(ii) Interval Day to Second Type.Syntax: create table table_name (column_name INTERVAL DAY TO SECOND (4));SQL*PLUS COMMANDSPOOL COMMANDIt is used to save the copy of the output from SQL*PLUS to the file specified byfilename.Syntax: spool filename; spool off;VERIFY COMMAND.It is used to control the output of the old and new lines.Syntax: set verify on; set verify off;Set verify on - Displays the old and new lines.Set verify off – Stops the displaying old and new lines.ECHO COMMAND.It is used to display the SQL statements while running a script file on the SQL*PLUS.
  13. 13. Syntax: set echo on; set echo off;Set echo on - Displays statement on the SQL*PLUS.Set echo off – Stops the displaying statements on the SQL*PLUS.PAGESIZENumber of Lines in a Page. Maximum Page size is 50,000.Syntax: set pagesize 50000LINESIZENumber of Characters in a Line. Maximum Line size is 32,767.Syntax: set linesize 32767VARIABLESIt is used in place of actual values in the SQL statements.It is also known assubstitutions.Types of Variables(i) Temporary variables.A temporary variable is only valid for the SQL statement in which it is used and
  14. 14. Doesn’t persist. & acts as a temporary variable.Syntax: insert into table_name values (&column_name);(ii) Defined variables.A defined variable persists until you explicitly remove it, redefine it, or exitSQL*PLUS.Syntax: define def_var = 100Syntax: insert into table_name values (&def_var);SUBQUERIESA select statement can be placed with another select, update or delete statement.The inner select statement is called as sub query.SUBQUERY TYPES(i) Single row subqueries:Returns zero or one row to the outer query.Syntax: Select column_name from table_name where column_name operator(Select column_name from table_name);(ii) Multiple row subqueries:Returns one or more rows to the outer query.
  15. 15. Syntax: select column_name from table_name where column_name operator (IN, ANY, ALL, NOT IN) (select column_name fromtable_name);(iii) Multiple column subqueries:Returns more than one columns to the outer query.Syntax: select column_name1, column_name2 from table_name where (column_name1, column_name2) operator (selectcolumn_name1, group function (column_name2) fromtable_name group by column_name);(iv) Correlated subqueries: Refers more than one column in the outer query.Syntax: select column_name1, column_name2 from table_name outer whereoperator (select column_name1 from table_name inner where inner.table_name =outer.table_name);(v) Nested subqueries:Sub queries placed with in another queries.Syntax: Select column_name from table_name where column_name operator(Select column_name from table_name where column_name operator (Selectcolumn_name from table_name));OPERATORS USED WITH SUBQUERIES(i) Operators used in Single row subqueries:<,>,<>, =, <=,>=
  16. 16. Syntax: select column_name from table_name where column_name1 = (select column_name1 from table_name where column_name =values);(ii) Operators used in multiple row subqueries:In, any, all, not inSyntax: select column_name from table_name where column_name (IN, ANY, ALL, NOT IN) (select column_name from table_name);(iii) Operators used in Correlated subqueries:Exists, not exists, in, not inSyntax: select column_name1, column_name2 from table_name outer where(exists, not exists, in, not in) (select column_name1 fromtable_name inner where inner.table_name = outer.table_name);INLINE VIEWSub queries can be placed in from clause, where clause or having clause. Subqueries placed in from clauses of the outer query is called as an inline view.Syntax: Select column_name from (Select column_name from table_name);