You are on page 1of 61

1

Oracle
Introduction to Oracle
The oracle corporation is the worlds leading supplier of software for information management and worlds second largest independent software company. Oracle is the top selling multi user RDBMS. Oracle application covers business intelligence, ecommerce, financials, human resources, manufacturing, mobile, project, and sales.

Features of Oracle
Client/Server Architecture Large database and space management Concurrent Processing

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

High transaction processing performance High Availability Controlled availability Openness industry standards Manageable security Database enforced integrity Distributed system Portability Compatibility Replicated environment

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

SQL
Introduction to SQL
SQL stands for Structured Query Language. SQL is a simple and powerful language used to create access and manipulate data and structure in the database. SQL is like plain English, which is easy to understand and to write. The history of SQL begins in an IBM laboratory in San Jose, California, where SQL was developed in the late 1970s. The initials stand for Structured Query Language, and the language itself is often referred to as "sequel." It was originally developed for IBM's DB2 product (a relational database management system, or RDBMS, that can still be bought today for various platforms and environments). In fact, SQL makes an RDBMS possible. SQL is a nonprocedural language, in contrast to the procedural or third-generation languages (3GLs) such as COBOL and C that had been created up to that time. SQL is the de facto standard language used to

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

manipulate and retrieve data from these relational databases. SQL enables a programmer or database administrator to do the following:

Modify a database's structure Change system security settings Add user permissions on databases or tables Query a database for information Update the contents of a database

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

Types of SQL statements


There are three types of SQL statements:1. Data Definition Language statements: - DDL statements are used to define, alter or drop database objects. The following table gives an overview about DDL statements.

Sr.No
1. 2. 3. 4. 5.

Need and Usages


Create schema objects Alter schema objects Delete schema objects

the SQL DDL statement


create alter delete rename grant, revoke

Rename schema objects Grant and take away privileges And rights

6.

Add a comment to the data Dictionary comment

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

2. Data Manipulation Language statements: - Once the tables have been created, the DML statements enable users to query or manipulate data in existing schemas objects. DML statements are normally the most frequently used commands. The following table gives an overview about DML statements:

Sr.No
1.

Need and Usages


Remove rows from tables or Views

The SQL DML statement


delete

2.

Add new rows of data into Table or view

insert

3.

Retrieve data from one or more Tables

select

Change column values in existing Rows of a table or view

update

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

3. Transaction Control Language statements: - TCL statements manage the Changes made by DML statements and group DML statements into transaction. The Following table gives an overview about TCL

Sr.No
1.

Need and Usages


Make a transaction changes Permanent

The SQL TCL statement


commit

2.

Undo changes in a transaction Either since the transaction started Or since a savepoint

rollback

3.

Set point to which transaction can be Rolled back

savepoint

4.

Establish properties for a transaction

set transaction

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

Data Types
The information in a database is maintained in the form of table and each table consist of rows and columns, which store data and therefore this data must have some data type i.e. the type of data, which is stored in a table. The different data types available in oracle are:

Data Types
Vachar2 Char Number Date Raw Long

Description
Contains variable length text string of up to 2000 bytes Contains fixed text strings of up to 255 bytes Contains numeric data Contains date data Contains binary data up to 255 bytes Contains text data of up to 2gigabytes

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

Rules for SQL


SQL statements may be on one or many lines. Command words cannot be split across lines. A; is used to end of SQL statements. Identifier can contain up to 30 characters and must start with an alphabetic character.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

10

The basic Select statements


The SELECT statement is the most commonly used statement in SQL and it is used to retrieve information already stored in the database. To retrieve data you can either select all the column values or name specific columns in the SELECT clause to retrieve data. Selecting all columns

Selecting specific columns

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

11

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

12

Using arithmetic operators


Oracle allows arithmetic operators to be used while viewing records from a table or while performing data manipulation operations such as insert, update and delete. These are:1. 2. 3. 4. 5. 6. + / * ** () Addition Subtraction Division Multiplication Exponentiation Enclosed operation

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

13

Using column aliases


Column aliases are used to rename column headings.

Using concatenation operator


It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

14

We can link columns to other columns, arithmetic expressions, or constant values to create a character expression by using the concatenation operator.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

15

Eliminating duplicate rows


To eliminate duplicate rows in the result, include the distinct keyword in the select clause immediately after the select keyword

Limiting rows using a selection


You can restrict the rows returned from the query by using the where clause. A where clause contains a condition that must be met, and it directly follows the from clause. If the condition is true the row meeting the condition is returned.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

16

Using Between operator


In order to select data that is within a range of values, the BETWEEN operator is used. The BETWEEN operator allows the selection of rows that contain values within specific lower and upper limit. The range coded after the word BETWEEN is inclusive. The owner value must be coded first. The two values n between the range must be linked with the keyword AND. A BETWEEN operator can be used with both character and numeric data types. However one cannot mix the data type i.e. the lower value of a range of values from character column and the other from numeric column.

Using the IN condition

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

17

Pattern Matching
The character pattern-matching operation is referred to as a wildcard search. You can select rows that match a character pattern using the like condition. The character pattern matching operation is referred to as a wildcard search. Two symbols can be used to construct the search string. % _ Represents any sequence of zero or more characters. Represents any single character

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

18

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

19

Using the NULL condition


The null condition includes the is null condition and the is not null condition. The is null condition tests for nulls. A null value means the value is unavailable, unassigned, unknown or inapplicable. Therefore, you can not test with = because a null cannot be equal or unequal to any value.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

20

Using the logical operators


Using the AND operator: - Returns true if both component conditions are true.

Using the OR operator: - Returns true if either component condition is true.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

21

Sorting rows
The order of rows returned in a query is undefined. The order by clause can be used to sort the rows. If you use the order by clause it must be the last clause of the sql statement. You can specify an expression, or an alias or column position as the sort condition. In ascending order

In descending order

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

22

Joining two tables


When data from more than one table in the database is required, a join condition is used. Rows in one table can be joined to rows in another table according to common values existing in corresponding columns, that is, usually primary and foreign key columns. To display data from two or more related tables, write a simple join condition in the WHERE clause. In the syntax: Table1.column denotes the table and column from which data is retrieved

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

23

Table1.column1= Table2.column2

is the condition that join the tables together

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

24

Using group functions


Group functions operate on set of rows to give one result per set. MAX Function

MIN Function

COUNT Function

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

25

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

26

Using AVG Function

Using SUM Function

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

27

The Having clause


You use the having clause to specify which groups are to be displayed, and thus, you further restrict the groups on the basic of aggregate information. In the syntax: Group condition: - restricts the groups of rows returned to those groups for which the specified condition is true The Oracle server performs the following steps when you use the HAVING clause: 1. Rows are grouped. 2. The group function is applied to the group. 3. The groups that match the criteria in the HAVING clause are displayed.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

28

Using Sub queries


A sub query is a select statement that is embedded in a clause of another select statement. You can build powerful statement out of simple ones by using sub queries. They can be very useful when you need to select rows from a table with a condition that depends on the data in the table itself. You can place the sub query in a number of SQL clauses 1. The WHERE clause 2. The HAVING clause 3. The FROM clause In the syntax Operator includes a comparison condition such as >, =, or IN

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

29

Creating and managing tables


Create tables to store data by executing the SQL create table statement. To create a table a user must have create table privilege and a storage area in which to create objects. Creating table

Confirming table creation

Creating a table using a sub query

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

30

Alter Table statement


After we create a table, we may need to change the table structure because; you omitted a column, your column definitions need to be changed, or you need to remove columns. You can do this by using the alter table statement. Adding a column

Modifying a column

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

31

Dropping Tables
The drop table statement removes the definition of an oracle table. When you drop a table, the database loses all the data in the table and all the indexes associated with it.

All data is deleted from the table. Any views and synonyms remains but are invalid. Any pending transactions are committed .

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

32

Inserting data into a table


You can add new rows to a table by issuing the insert statement. Because you can insert a new row that contains values for each column, the column list is not required in the insert clause. However if you do not use the column list the values must be listed according to the default order of the columns in the table and a value must be provided for each column.

Specifying data for all columns

Specifying data for selected columns

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

33

Updating data
We can modify existing rows by using the update statement. The update statement modifies specific rows if the where clause is specified.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

34

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

35

Deleting rows
We can remove existing rows by using the delete statement. We can delete specific rows by using the where clause in the delete statement.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

36

Including constraints
The oracle server uses constraints to prevent invalid data entry into tables. We can use constraints to do the following: Enforce rules on the data in a table whenever a row is inserted, updated or deleted from that table. The constraint must be satisfied for the operation to succeed. Prevent the deletion of table if there are dependencies of other tables. Not Null: - The not null constraint ensures that the column contains no null values.

Unique: - A unique key integrity constraint requires that every value in a column be unique- that is, no two rows of a table can have duplicate values in a specified column.
It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

37

Primary key: - A primary key constraint creates a primary key for the table. Only one primary key can be created for each table. The primary key constraint is a column that uniquely identifies each row in a table. A primary key column can not contain a null value.

Foreign key: - The foreign key constraint designates a column as a foreign key and establishes a relationship between a primary key or a unique key in the same table or a different table.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

38

Check: - Defines a condition that each row must satisfy

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

39

Views
A view is a virtual or logical table that allows the viewing or manipulating the contents of one or more tables. Views do not really contain or store data, but they take data from the tables on which they are based, called base tables.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

40

Sequences
Oracle provides an object called a sequence that can generate unique numeric value. If the value entered in a column is computer generated it will always fulfill the unique constraint and the row will always be accepted for storage.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

41

PL/SQL
Introduction to PL/SQL
PL/SQL is a procedure language that oracle developed as an extension to standard SQL in order to provide a way to execute procedure logic on the database. PL/SQL provide a mechanism for developer to add a procedure component at the server level PL/SQL is block structure language A PL/SQL block is a syntactically unit that might contain programmer code , variable declaration , error handler , procedure , function and even other PL/SQL block

Syntax for PL/SQL block:


Declare Variable_declarations Begin Program_code
It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

42

End; In this syntax variable declaration are any variable that might want to define cursor definition and nested PL/SQL procedure and function are also define here program code refer to the PL/SQL statement that make up the block.

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

43

Advantage of PL/ SQL


1. PL/SQL is a development tool that only not supports SQL data manipulation but also provides facility of conditional checking, branching and looping. 2. PL/SQL sends an entire block of SQL statement to the oracle engine all in one go. Communication between the program block and oracle engine reduces considerably, reduces network traffic since the oracle engine got the SQL statement as a single block. It processes this code much faster than if it got once as one sentence at a time 3. PL/SQL also permits dealing with error as required and facilitates displaying user friendly message when errors are encountered 4. Via PL/SQL all sort of calculation can be done quickly and efficiently without the use oracle engine. This considerably improve transaction performance 5. Applications written in PL/SQL are portable to any computer, hardware and operating system where as oracle is operational.
It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

44

Programs
Program to calculate sum of two numbers

Program to find greater of two numbers

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

45

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

46

Program to find factorial of a given number

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

47

Program to print Fibonacci series

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

48

Procedures and Functions


A procedure or function is a logically grouped set of SQL and PL statement that performs a specific task. A procedure and function is a named PL/SQL ode block that has been complied and stored in one of the oracle engine system table .procedure and function are made up of three parts : 1. Declarative part 2. Executable part 3. An optional exception handling part

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

49

Procedures
Using local Procedures

Using stored Procedures

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

50

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

51

Functions
Using local function

Using stored function

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

52

Cursors
The oracle engine uses a work area for its internal processing in order to execute an SQL statement this work area is private to SQL operation and is called cursor

Types of cursors
Implicit cursor: - The oracle engine implicitly opens a cursor on the server to process each SQL statement. Since the implicit cursor is opened and managed by oracle server internally the function of reversing an area in memory,

populating this area with appropriate data, processing the data in memory area, releasing the memory area when the processing is complete is taken care of by the oracle engine Explicit cursor: - When individual record in a table have to a process inside a PL/SQL block a cursor is used this cursor will be declared and mapped to an SQL query in declare section of the PLSQL block and used within its

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

53

executable section. A cursor thus created and used is known as an explicit cursor

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

54

Program to demonstrate implicit cursors

Explicit cursor management This step involved in using an explicit cursor and manipulating data 1. Declare a cursor mapped to a SQL select statement that retrieves data for processing 2. Open the cursor 3. Fetch data from the cursor one row at a time into memory variables
It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

55

4. Process the data held in the memory variables as required using a loop 5. Exit from the loop after processing is complete 6. Close the cursor Program to demonstrate implicit cursors

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

56

Triggers
Database trigger are data base object that are stored on the server. The oracle engine allows the definition of procedure that are implicitly executed when an insert, update or delete is issued against the table these procedure are called database trigger These data base object consist of the following distinct section 1. A named data base event 2. A PL/SQL block that executes when the event occurs

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

57

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

58

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

59

Packages
A package is an oracle object, which holds other objects with in it. Objects commonly held with in a package are procedures, functions, variables, constants, cursors and exception handlers. Package can contain PL/SQL block of code, which has been written to perform some process. PLSQL block of code do not require any kind of input from other PLSQL block of code. A package has two components. A specification and a body. A package specification declares the types, memory variables, constants, exceptions, cursors and sub programs that are available for use. A package body fully defines cursors, functions, procedures and thus implements the specification Creating package specification

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

60

Creating package body

Using packages

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

61

It is only meant for Demo of Practical file of MCA-2nd Year (3rd Semester) Students. Nobody will copy it. Verified By: Rajan Manro For any query contact at: rajanmanro@rediffmail.com

You might also like