DBMS Practicals
DBMS Practicals
Originally based upon relational algebra and tuple relational calculus, SQL consists of a data
definition language and a data manipulation language. The scope of SQL includes data insert,
query, update and delete, schema creation and modification, and data access control.
Although SQL is often described as, and to a great extent is, a declarative language (4GL), it
also includes procedural elements.
Page 1
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 1
As with most Windows installation programs, inserting the CD-ROM (ordered or purchased
from Oracle) into the CD-ROM drive automatically executes the setup program. (If this does
not happen, use the Windows Explorer to view the contents of your CD-ROM drive and
double click on the “setup” program there).
There are many versions of Oracle Database like Oracle Database 10g, Oracle Database
10g,Oracle database 11g, oracle database 12c, we will learn how to install version 11g on
windows.
Options Menu.
Page 2
Roll No. 2028192 Subject code: BTCS-505-18
Step 2: Click the Download Button and Scroll Down to Database Section.
Step 3:- Click Database 11g Enterprise/Standard Editions, after which you will find
different versions of oracle for different OS. Download the Files According to your OS.
Step 4: After clicking the Download Button, the page will be directed to login screen where
you „ll need to sign in in Oracle Account. If you don‟t have one, then you must Sign Up,
because without that you won‟t able to download the files.
Page 3
Roll No. 2028192 Subject code: BTCS-505-18
Step 5: Repeat the same steps for both the files and download them.
After downloading the files Successfully, you will find both files in Downloads Folder where
both of them will be in Compressed form, so you will be need to Extract them.
Step 1: Extract both the files with the use of WinRAR or any other extraction tool of your
choice in the same directory.
Step 2: Go to folder database-> Stage-> Components Copy all the content of this Folder
Page 4
Roll No. 2028192 Subject code: BTCS-505-18
Step 1: Go to Main Database-> Stage Paste the copied content in Components Folder.
Step 1: Go to Main Database folder where you will find Setup. Right click the setup.exe
file and choose Run as Administrator.
Step 2: Click Yes to continue. This will start Oracle Universal Installer.
Page 5
Roll No. 2028192 Subject code: BTCS-505-18
Step 3: Provide your Email Address to receive all the notification and news alerts from
oracle.
IN case you don’t want to receive any alerts from oracle, then Simply leave these fields empty and
click on next to move ahead.
Step 4: Select any of the three different Installation options according to your needs.
Page 6
Roll No. 2028192 Subject code: BTCS-505-18
Option 1- If you want to install Oracle Server Software and want to create Database
also.
Option 2- if you want to install Oracle Server only.
Option 3-If you want to Upgrade your Existing Database.
Step 5: Choose between Server Class and Desktop Class as per your requirement and click
on Next.
Step 6: Configure the basic settings and create a Password for your database. Once the
configuration is done click on Next to continue.
Page 7
Roll No. 2028192 Subject code: BTCS-505-18
Step 7: Here, Oracle Universal Installer(OUI) will check for the Prerequisites such as
Hardware compatibility.
If there will be any Errors, then OUI will show them here and will recommend the changes.
Step 8: Click on Finish to start the Installation process. This installation might take some
time depending on your Hardware.
Page 8
Roll No. 2028192 Subject code: BTCS-505-18
Page 9
Roll No. 2028192 Subject code: BTCS-505-18
Step 10: Copy the localhost link provided to open your Enterprise Manager.
Click the close Button and you are done with the Installation Process.
One way to launch Enterprise Manager is using the Link provided in last step of Installation
process.
NOTE: In case if you misplace this link in the future , follow the next step to start it the other
way.
Page 10
Roll No. 2028192 Subject code: BTCS-505-18
Advanced->Proceed to Localhost
Login with your Credentials and begin using Oracle Database 11g on your Windows.
Page 11
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 2
The CHAR data type stores fixed-length character strings. When you create a table with a
CHAR column, you must specify a string length (in bytes or characters) between 1 and 2000
bytes for the CHAR column width. The default is 1 byte. Oracle then guarantees that:
When you insert or update a row in the table, the value for the CHAR column has the
fixed length.
If you give a shorter value, then the value is blank-padded to the fixed length.
If a value is too large, Oracle Database returns an error.
The VARCHAR2 data type stores variable-length character strings. When you create a table
with a VARCHAR2 column, you specify a maximum string length (in bytes or characters)
between 1 and 4000 bytes for the VARCHAR2 column. For each row, Oracle Database stores
each value in the column as a variable-length field unless a value exceeds the column's
maximum length, in which case Oracle Database returns an error. Using VARCHAR2 and
VARCHAR saves on space used by the table.
For example, assume you declare a column VARCHAR2 with a maximum size of 50
characters. In a single-byte character set, if only 10 characters are given for the VARCHAR2
Page 12
Roll No. 2028192 Subject code: BTCS-505-18
column value in a particular row, the column in the row's row piece stores only the 10
characters (10 bytes), not 50.
Globalization support allows the use of various character sets for the character data types.
Globalization support lets you process single-byte and multisystem character data and
convert between character sets. Client sessions can use client character sets that are different
from the database character set.
Consider the size of characters when you specify the column length for character data types.
You must consider this issue when estimating space for tables with columns that contain
character data.
The length semantics of character data types can be measured in bytes or characters.
Byte semantics treat strings as a sequence of bytes. This is the default for character
data types.
Character semantics treat strings as a sequence of characters. A character is
technically a code point of the database character set.
For single byte character sets, columns defined in character semantics are basically the same
as those defined in byte semantics. Character semantics are useful for defining varying-width
multi byte strings; it reduces the complexity when defining the actual length requirements for
data storage. For example, in a Unicode database (UTF8), you must define a VARCHAR2
column that can store up to five Chinese characters together with five English characters. In
byte semantics, this would require (5*3 bytes) + (1*5 bytes) = 20 bytes; in character
semantics, the column would require 10 characters.
VARCHAR2 (20 BYTE) and SUBSTRB (<string>, 1, 20) use byte semantics. VARCHAR2 (10 CHAR)
and SUBSTR (<string>, 1, 10) use character semantics.
NCHAR and NVARCHAR2 are Unicode data types that store Unicode character data. The
character set of NCHAR and NVARCHAR2 data types can only be either AL16UTF16 or UTF8 and
is specified at database creation time as the national character set. AL16UTF16 and UTF8 are
both Unicode encoding.
The NCHAR data type stores fixed-length character strings that correspond to the
national character set.
The NVARCHAR2 data type stores variable length character strings.
Page 13
Roll No. 2028192 Subject code: BTCS-505-18
When you create a table with an NCHAR or NVARCHAR2 column, the maximum size specified
is always in character length semantics. Character length semantics is the default and only
length semantics for NCHAR or NVARCHAR2.
For example, if national character set is UTF8, then the following statement defines the
maximum byte length of 90 bytes:
This statement creates a column with maximum character length of 30. The maximum byte
length is the multiple of the maximum character length and the maximum number of bytes in
each character.
NCHAR
NVARCHAR2
NCHAR:-
The maximum length of an NCHAR column is 2000 bytes. It can hold up to 2000 characters.
The actual data is subject to the maximum byte limit of 2000. The two size constraints must
be satisfied simultaneously at run time.
NVARCHAR2:-
The maximum length of an NVARCHAR2 column is 4000 bytes. It can hold up to 4000
characters. The actual data is subject to the maximum byte limit of 4000. The two size
constraints must be satisfied simultaneously at run time.
The LOB data types for character data are CLOB and NCLOB. They can store up to 8 terabytes
of character data (CLOB) or national character set data (NCLOB).
LONG data type columns are used in the data dictionary to store the text of view definitions.
You can use LONG columns in SELECT lists, SET clauses of UPDATE statements, and VALUES
clauses of INSERT statements.
Page 14
Roll No. 2028192 Subject code: BTCS-505-18
The NUMBER data type stores fixed and floating-point numbers. Numbers of virtually any
magnitude can be stored and are guaranteed portable among different systems operating
Oracle Database, up to 38 digits of precision.
column_name NUMBER
The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete
data:
UPDATE My_table
SET field1 = 'updated value'
WHERE field2 = 'N';
Page 15
Roll No. 2028192 Subject code: BTCS-505-18
MERGE is used to combine the data of multiple tables. It combines the INSERT and
UPDATE elements. It is defined in the SQL:2003 standard; prior to that, some databases
provided similar functionality via different syntax, sometimes called "upsert".
The Data Definition Language (DDL) manages table and index structure. The most basic
items of DDL are the CREATE, ALTER, RENAME, DROP and TRUNCATE statements:
ALTER modifies the structure of an existing object in various ways, for example, adding a
column to an existing table or a constraint, e.g.:
TRUNCATE deletes all data from a table in a very fast way, deleting the data inside the table
and not the table itself. It usually implies a subsequent COMMIT operation, i.e., it cannot be
rolled back (data is not written to the logs for rollback later, unlike DELETE).
DROP deletes an object in the database, usually irretrievably, i.e., it cannot be rolled back,
e.g.:
The Data Control Language (DCL) authorizes users to access and manipulate data. Its two
main statements are:
Page 16
Roll No. 2028192 Subject code: BTCS-505-18
Example:
1).CREATES DATABASE :-
The following SQL statement creates a database called "my_db":
2).Creating Tables:-
The CREATE TABLE statement is used to create a table in a database.
Tables are organized into rows and columns; and each table must have a name.
Now we want to create a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City.
Page 17
Roll No. 2028192 Subject code: BTCS-505-18
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The LastName, FirstName, Address, and City columns are of type varchar and will hold
characters, and the maximum length for these fields is 255 characters.
OUTPUT:-
Figure No.1
Page 18
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.2
The SELECT statement is used to select data from a database. The result is stored in a result
table, called the result-set.
SELECT Syntax:-
SELECT column_name,column_name
FROM table_name;
SELECT * Example:-
Page 19
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.3
Figure No.4
Conditional Statement:-
5).WHERE Clause :-
The WHERE clause is used to extract only those records that fulfill a specified criterion.
WHERE Syntax:-
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Page 20
Roll No. 2028192 Subject code: BTCS-505-18
The following SQL statement selects all the customers from the country "Mexico", in the
"Customers" table:
OUTPUT:-
Figure No.5
The AND operator displays a record if both the first condition AND the second condition are
true.
The OR operator displays a record if either the first condition OR the second condition is
true.
OR Operator Example:-
OUTPUT:-
Figure No.6
7).ORDER BY Keyword:-
The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in a descending order, you can use the DESC keyword.
ORDER BY Syntax:-
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
ORDER BY Example:-
Page 22
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.7
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
My SQL / Oracle:
Page 23
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.8
OUTPUT:-
Figure No.9
OUTPUT:-
Figure No.10
Page 24
Roll No. 2028192 Subject code: BTCS-505-18
UPDATE Syntax:-
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
UPDATE Example:-
Assume we wish to update customer "Alfreds Futterkiste" with a new contact person & city.
OUTPUT:-
Figure No.11
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
OUTPUT:-
Figure No.12
Page 25
Roll No. 2028192 Subject code: BTCS-505-18
DELETE Syntax:-
DELETE Example:-
Assume we wish to delete the customer "Alfreds Futterkiste" from the "Customers" table.
OUTPUT:-
Figure No.13
OUTPUT:-
Figure No.14
Page 26
Roll No. 2028192 Subject code: BTCS-505-18
Indexes, tables, and databases can easily be deleted/removed with the DROP statement.
OUTPUT:-
Figure No.15
Page 27
Roll No. 2028192 Subject code: BTCS-505-18
What if we only want to delete the data inside the table, and not the table itself?
OUTPUT:-
Figure No.16
The rename operation is done atomically, which means that no other session can access any
of the tables while the rename is running. For example, if you have an existing table old_table,
you can create another table new_table that has the same structure but is empty, and then
replace the existing table with the empty one as follows (assuming that backup_table does not
already exist):
Page 28
Roll No. 2028192 Subject code: BTCS-505-18
13) DESCRIBE:-
The describe SQL command is used to list all of the fields in a table and the data format of each field.
It is phrased as: describe ;
Examples:
This would list all of the fields and their formats for the table called 'Colors':
Describe Colors;
OUTPUT:-
Figure No.17
Page 29
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 3
SQL/Oracle aggregate functions return a single value, calculated from values in a column.
AVG () Syntax:-
SELECT AVG(column_name) FROM table_name;
AVG () Example:-
SELECT AVG(Price) AS PriceAverage FROM Products;
OUTPUT:-
Figure No.1
Page 30
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.2
The MAX () function returns the largest value of the selected column.
MAX() Example:-
OUTPUT:-
Figure No.3
Figure No.4
The MIN() function returns the smallest value of the selected column.
MIN() Syntax:-
Page 31
Roll No. 2028192 Subject code: BTCS-505-18
MIN() Example:-
SELECT MIN(Price) AS SmallestOrderPrice FROM Products;
OUTPUT:-
Figure No.5
Figure No.6
SUM() Example:-
SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;
OUTPUT:-
Figure No.7
Page 32
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.8
GROUP BY () Function:-
The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set by one or more columns.
GROUP BY Syntax:-
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
OUTPUT:-
Figure No.9
Figure No.10
Page 33
Roll No. 2028192 Subject code: BTCS-505-18
LIKE Syntax:-
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
OUTPUT:-
Figure No.11
Figure No.12
Page 34
Roll No. 2028192 Subject code: BTCS-505-18
NULL Values:-
If a column in a table is optional, we can insert a new record or update an existing record
without adding a value to this column. This means that the field will be saved with a NULL
value.
NULL values are treated differently from other values. NULL is used as a placeholder for
unknown or inapplicable values.
Example:-
OUTPUT:-
Figure No.13
OUTPUT:-
Figure No.14
Page 35
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 4
4). SQL/ORACLE Joins, Nested queries using Set operators.
JOIN:-
An SQL JOIN clause is used to combine rows from two or more tables, based on a common
field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN
return all rows from multiple tables where the join condition is met.
OUTPUT:-
Figure No.1
Figure No.2
Example:-
Page 36
Roll No. 2028192 Subject code: BTCS-505-18
OUPTUT:-
Figure No.3
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Figure No.4
Page 37
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.5
Figure No.6
Example:-
FROM Customers
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Page 38
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.7
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows
in the right table (table2). The result is NULL in the right side when there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
Figure No.8
Figure No.9
Page 39
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.10
OUTPUT:-
Figure No.11
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching
rows in the left table (table1). The result is NULL in the left side when there is no match.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
Page 40
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.12
Database:-
Figure No.13
Figure No.14
Example:-
Page 41
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.15
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the
right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
Figure No.16
Database:-
Figure No.17
Page 42
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.18
Example:-
OUTPUT:-
Figure No.19
SET operators:-
INTERSECT, EXCEPT, and UNION are set operators that perform operations between two or more
input sets. UNION has been available in t-sql for some time, while INTERSECT and EXCEPT were
introduced in SQL 2005. All three operators have similar requirements:
UNION Operator:-
The UNION operator is used to combine the result-set of two or more SELECT statements.
Page 43
Roll No. 2028192 Subject code: BTCS-505-18
UNION Syntax:-
Database:-
Figure No.20
Figure No.21
OUTPUT:-
Figure No.22
INTERSECT operator:-
The SQL INTERSECT operator takes the results of two queries and returns only rows that appear in
both result sets. For purposes of duplicate removal the INTERSECT operator does not distinguish
Page 44
Roll No. 2028192 Subject code: BTCS-505-18
between NULLs. The INTERSECT operator removes duplicate rows from the final result set. The
INTERSECT ALL operator does not remove duplicate rows from the final result set.
Syntax:-
DATABASE:-
Figure No.23
Figure No.24
EXAMPLE:-
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
Page 45
Roll No. 2028192 Subject code: BTCS-505-18
INTERSECT
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
OUTPUT:-
Figure No.25
EXCEPT operator:-
The EXCEPT clause/operator is used to combine two SELECT statements and returns rows from the
first SELECT statement that are not returned by the second SELECT statement. This means EXCEPT
returns only rows, which are not available in second SELECT statement.
Syntax:-
EXCEPT
DATABASE:-
Figure No.26
Page 46
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.27
Example:-
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
EXCEPT
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
OUTPUT:-
Figure No.28
Page 47
Roll No. 2028192 Subject code: BTCS-505-18
***************Nested queries***************
A Sub query or Inner query or Nested query is a query within another SQL query and
embedded within the WHERE clause.
A sub query is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Sub queries can be used with the SELECT, INSERT, UPDATE, and DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
Syntax:-
[WHERE])condition
Page 48
Roll No. 2028192 Subject code: BTCS-505-18
DATABASE:-
Figure No.29
EXAMPLE:-
SQL> SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
OUTPUT:-
Figure No.30
Page 49
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 5
Views:-
A view is a virtual table. A view contains rows and columns, just like a real table. The fields in a view
are fields from one or more real tables in the database.
A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database. You can add SQL functions, WHERE, and JOIN
statements to a view and present the data as if the data were coming from one single table.
Syntax:-
DATABASE:-
Figure No.1
EXAMPLE:-
Page 50
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.2
Updating a View:-
EXAMPLE:-
UPDATE CUSTOMERS_VIEW
SET AGE = 35
WHERE name='Ramesh';
OUTPUT:-
Figure No.3
Page 51
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.4
Dropping Views:-
Obviously, where you have a view, you need a way to drop the view if it is no longer needed.
Indexes:-
Indexes are special lookup tables that the database search engine can use to speed up data
retrieval. Simply put, an index is a pointer to data in a table. An index in a database is very
similar to an index in the back of a book.
For example, if you want to reference all pages in a book that discuss a certain topic, you first
refer to the index, which lists all topics alphabetically and are then referred to one or more
specific page numbers.
An index helps speed up SELECT queries and WHERE clauses, but it slows down data input,
with UPDATE and INSERT statements. Indexes can be created or dropped with no effect on
the data.
Single-Column Indexes:-
A single-column index is one that is created based on only one table column. The basic
syntax is as follows:
Page 52
Roll No. 2028192 Subject code: BTCS-505-18
Unique Indexes:-
Unique indexes are used not only for performance, but also for data integrity. A unique index
does not allow any duplicate values to be inserted into the table. The basic syntax is as
follows:
Composite Indexes:
A composite index is an index on two or more columns of a table. The basic syntax is as
follows:
Implicit Indexes:-
Implicit indexes are indexes that are automatically created by the database server when an
object is created. Indexes are automatically created for primary key constraints and unique
constraints.
An index can be dropped using SQL DROP command. Care should be taken when dropping
an index because performance may be slowed or improved.
Database security entails allowing or disallowing user actions on the database and the objects
within it. Oracle uses schemas and security domains to control access to data and to restrict
the use of various database resources.
Each Oracle database has a list of user names. To access a database, a user must use a
database application and attempt a connection with a valid user name of the database. Each
user name has an associated password to prevent unauthorized use.
Page 53
Roll No. 2028192 Subject code: BTCS-505-18
Security Domain:-
Each user has a security domain—a set of properties that determine such things as:
Privileges:-
A privilege is a right to run a particular type of SQL statement. Some examples of privileges
include the right to:
COMMIT Command:-
The COMMIT command is the transactional command used to save changes invoked by a
transaction to the database.
DATABASE:-
Figure No.5
Example:-
COMMIT;
Page 54
Roll No. 2028192 Subject code: BTCS-505-18
OUTPUT:-
Figure No.6
ROLLBACK Command:-
The ROLLBACK command is the transactional command used to undo transactions that have
not already been saved to the database.
DATABASE:-
Figure No.7
EXAMPLE:-
ROLLBACK;
OUTPUT:-
Figure No.8
Page 55
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 6
Originally based upon relational algebra and tuple relational calculus, SQL consists of a data
definition language and a data manipulation language. The scope of SQL includes data insert,
query, update and delete, schema creation and modification, and data access control.
Although SQL is often described as, and to a great extent is, a declarative language (4GL), it
also includes procedural elements.
PL/SQL is available in Oracle Database (since version 7), Times Ten in-memory database
(since version 11.2.1), and IBM DB2 (since version 9.7).[1]
PL/SQL supports variables, conditions, loops and exceptions. Arrays are also supported,
though in a somewhat unusual way, involving the use of PL/SQL collections.
Once the program units have been stored into the database, they become available for
execution at a later time.
While programmers can readily embed Data Manipulation Language (DML) statements
directly into their PL/SQL code using straightforward SQL statements, Data Definition
Language (DDL) requires more complex "Dynamic SQL" statements to be written in the
PL/SQL code. However, DML statements underpin the majority of PL/SQL code in typical
software applications.
Page 56
Roll No. 2028192 Subject code: BTCS-505-18
SQL is a query language that allows you to issue a single query or execute a single
insert/update/delete.
PL-SQL is Oracle's "Programming Language" SQL, which allows you to write a full program (loops,
variables, etc.) to accomplish multiple selects/inserts/updates/deletes. You should learn SQL first,
then move on to PL-SQL. You will probably need both to be an Oracle DBA.
SQL is the only language through which you create or modify data and structures in Oracle.
Although other tools will connect with the database, they all use SQL to read and modify
data.
SQL is used to perform operations on data. There is an ANSI standard for it. Most databases
support it, although each may have differences in the interpretation, and each typically has
some proprietary extensions to it.
PL/SQL Architecture:-
The PL/SQL compilation and run-time system is an engine that compiles and executes
PL/SQL blocks and subprograms. The engine can be installed in an Oracle server or in an
application development tool such as Oracle Forms.
In either environment, the PL/SQL engine accepts as input any valid PL/SQL block or
subprogram. Figure shows the PL/SQL engine processing an anonymous block. The PL/SQL
engine executes procedural statements but sends SQL statements to the SQL engine in the
Oracle database.
Figure No.2
Page 57
Roll No. 2028192 Subject code: BTCS-505-18
Figure No.3
The client can interact with the system through front end application program.When there is a
data oriented operation it invokes the PL/SQL program in single step.In PL/SQL end it
approaches PL/SQL Engine which splits PL and SQL part separately and send the SQL
request to the SQL. Now SQL sends the data request through query to database.Data if
available will be send back to SQL in turn to PL/SQL.Now PL/SQL will send the return
values back to the client application
Assignment Statement:-
An assignment statement sets the current value of a variable, field, parameter, or element. The
statement consists of an assignment target followed by the assignment operator and an expression.
When the statement is executed, the expression is evaluated and the resulting value is stored in the
target. For more information, see "Assigning Values to Variables".
Syntax:-
Assignment statement:-
Figure No.4
Page 58
Roll No. 2028192 Subject code: BTCS-505-18
PL/SQL Expressions:-
Regular expressions enable you to search for patterns in string data by using standardized syntax
conventions. You specify a regular expression by means of the following types of characters:
Literals, which are the characters for which you are searching
Regular expressions are a powerful text processing component of programming languages such as
Perl and Java. For example, a Perl script can process each HTML file in a directory, read its contents
into a scalar variable as a single string, and then use regular expressions to search for URLs in the
string.
Oracle's support of regular expressions enables developers to implement complex match logic in the
database. This technique is useful for the following reasons:
By centralizing match logic in Oracle Database, you avoid intensive string processing of SQL
results sets by middle-tier applications.
Prior to Oracle Database 10g, developers often coded data validation logic on the client,
requiring the same validation logic to be duplicated for multiple clients. Using server-side
regular expressions to enforce constraints solves this problem.
The built-in SQL and PL/SQL regular expression functions and conditions make string
manipulations more powerful and less cumbersome than in previous releases of Oracle
Database.
Declarations
1 This section starts with the keyword DECLARE. It is an optional section and defines all variables, cursors,
subprograms, and other elements to be used in the program.
Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It consists
2
of the executable PL/SQL statements of the program. It should have at least one executable line of code,
which may be just a NULL command to indicate that nothing should be executed.
Page 59
Roll No. 2028192 Subject code: BTCS-505-18
Exception Handling
3 This section starts with the keyword EXCEPTION. This section is again optional and contains
exception(s) that handle errors in the program.
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within
other PL/SQL blocks using BEGIN and END. Here is the basic structure of a PL/SQL block:
Syntax:-
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
The end; line signals the end of the PL/SQL block. To run the code from SQL command line,
you may need to type / at the beginning of the first blank line after the last line of the code.
When the above code is executed at SQL prompt, it produces the following result:
OUTPUT:-
Hello World
Page 60
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 7
Figure No.1
Page 61
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 8
8). PL/SQL Program to Accept Number and Find the Sum of Number.
Figure No.1
Page 62
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 9
Figure No.1
Page 63
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 10
Figure No.1
Page 64
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 11
Figure No.1
Page 65
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 12
Figure No.1
Page 66
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 13
Figure No.1
Page 67
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 14
14). PL/SQL Program that Accepts a Number and Find the Factorial.
Figure No.1
Page 68
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 15
15). PL/SQL Program that Accepts a Number print its Fibonacci series.
Figure No.1
OUTPUT:-
Figure No.2
Page 69
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 16
16). PL/SQL Program that Accepts Three Values and Display the Greater
Value.
Figure No.1
OUTPUT:-
Figure No.2
Page 70
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 17
Figure No.1
OUTPUT:-
Figure No.2
Page 71
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 18
Figure No.1
OUTPUT:-
Figure No.2
Page 72
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 19
Figure No.1
OUTPUT:-
Figure No.2
Page 73
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 20
Figure No.1
Figure No.2
Page 74
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 21
Page 75
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 22
22). PL/SQL Program to insert or update the Ename and Job column of
Employee table using trigger.
Page 76
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 23
Page 77
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 24
OUTPUT:-
Page 78
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 25
25). PL/SQL Program of a local procedure that accept two number using
parameters.
OUTPUT:-
Page 79
Roll No. 2028192 Subject code: BTCS-505-18
PRACTICAL # 26
26) A stored procedure of PL\SQL that accepts two number and display
the product of two numbers.
OUTPUT:-
Page 80