Professional Documents
Culture Documents
DBMS - Lab - Manual Satyam
DBMS - Lab - Manual Satyam
DBMS - Lab - Manual Satyam
Table of Content:
18
PL/SQL Program to Accept a Number and Check it is
Prime Number or Not
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.
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).
If you downloaded the Oracle9i from Oracle Technet, then you need to unzip the files into a
three temporary directories and double click on the setup.exe program in the Disk1 directory)
to get the installation process started.
Figure No.2
It is assumed that this is a fresh install so no other Oracle products should be installed at this
time.
Click on the Next button to move to the File Locations screen as shown below:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No. 3
The Source path should reflect either your CD-ROM or the directory in which you unzipped
the source files. Leave this as the default. In the example above, the source files were
unzipped into c:\source\Oralce\9i\NT20009011\Disk1 directory.
For the Destination, leave the Oracle Home named OraHome90 as the default. Choose a hard
disk drive that has at least 10 gigabytes of free space. In the example here, we are using the
C: drive.
Leave the \Oracle\Ora90 path as it is (the default). If you must change this to a different
drive, only change the drive letter and not the directories.
Click on the Next button to load the Product information. This may take up to 2 minutes
(depending on the speed of your machine). Once the product information has been read, the
following 3 “Available Products” options are presented:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.4
Make sure the Oracle9i Database 9.0.1.1.1 is selected and click on the Next button.
The “Installation Types” screen will appear next. For this installation, we chose the
Enterprise Edition.
Figure No.5
Figure No.6
For this install, we chose the “General Purpose” Database option. Click on the Next button to
continue.
The next step is to identify the database using the Global database name:
Figure No.7
If some default Global database name and SID are given, keep them. Otherwise, use the
following (make a note of these for later on):
Click on the Next button to continue to the “Data File Location ” screen
Figure No.8
In this step, you need to select where the Oracle data files will be located. In large operations,
we typically keep the data files on a separate disk (or disks), however, for this install, we are
storing the data files in the same C:\oracle directory tree. Keep the default as shown and click
on the Next button to continue.
In the next screen, choose the default character set for the database:
Figure No.9
Figure No.10
In this screen (shown above), the products and files that will be installed are summarized.
Note that in this example, it will take 1.5 GB MB of disk space for the installation of the
software. Additional space will be required for the default database.
If everything looks OK at this point, click on the Install button to begin the installation.
Figure No.11
During the install, you will notice various Oracle products being copied over to the hard disk.
For example, in the above figure, a portion of the Oracle Enterprise Manager is being
installed.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Once the installation is completed, the next step will be to configure the various additional
services and the database. This is shown below:
Figure No.12
Each of the configuration programs will be executed in turn. The HTTP Server
(Oracle/Apache) should launch automatically and open up TCP/IP port 80. You should notice
a new Command window as follows:
The Net Configuration Assistant and Oracle Intelligent Agent should run automatically and
not prsent any sustained screens.
The Oracle Database Configuration Assistant will appear for some time while the default
database is created and opened. As below:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.13
Once the default database has been installed, the following screen will appear:
Figure No.14
Clicking on the Password Management button and change the default passwords for the SYS,
SYSTEM, SCOTT and DBSNMP accounts as shown below:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.15
This should conclude the configuration of the database and the “End of Installation” screen
should appear as below. Click on the Exit button and click on the following Yes button to
confirm exiting the Installation program.
Figure No.16
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
At this point, be sure to reboot your computer so any final changes can take affect. In
particular, the path c:\oracle\ora90\bin will be added to the PATH environment variable.
After Oracle9i is installed, you will notice several new program groups in the Start menu.
Figure No.17
These include:
Under Windows NT and Windows 7 or Windows 2000, the Oracle 9i database runs
automatically as a service. Below is a view of the Windows 2000 Services mangement
console showing the services installed by default:
Figure No.18
Note the last one OracleServerORCL is the actual database process itself.
OracleOraHome90TNSListener is the listener process for remote connections to the database.
OracleOraHome90Agent is the Oracle Agent used to communicate with management
services. Finally, the OracleOraHome90HTTPServer is the Oracle/Apache web server.
Since the OracleServerORCL service is set to start automatically, the database wll start and
mount each time the computer is started up. To manage the database processes directly, one
can use the Enterprise Manager Console in Stand-alone mode. From the Start menu, choose
Programs -> Oracle - OraHome90 -> Enterprise Manager Console and select Launch
Standalone. The following screen should appear:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.19
Click on the plus sign to expand the Databases branch and then once again to expand the
ORCL branch. When prompted for username and password, use the SYSTEM account and
choose to log in as SYSDBA. In SYSDBA mode, the SYSTEM user can perform most any
manipulation of the database.
Once logged in, clicking on the Instance tree and then the Configuration tree should display
the current state of the database:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.20
Once the database is running, the SQL*Plus tool can be used to connect to the database.
Go to the Start -> Programs -> Oracle - OraHome90 -> Application Development program
group and run the SQL Plus application. Once SQL Plus is running, you will be prompted for
3 things: Username, password and Host String.
The default Database Administrator’s account has a username of SYSTEM and a default
password of MANAGER. You may have changed the SYSTEM account password in a
previous step so use the new password.
Fill in the Username and password fields with the SYSTEM account and leave the Host
String field blank. Since we are connecting to a local Oracle database, no Host String is
required. Click on the OK button as shown below:
Figure No.21
If the database is running and the username and password are typed correctly, SQL*Plus
should log the SYSTEM user in and present the SQL> prompt as shown below:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.22
By default, you can also log in with the SCOTT/TIGER account. That is, there is a demo
account already set up in the database with username SCOTT and password TIGER. Many of
the examples in the Oracle documentation use the tables included in the SCOTT user’s
account.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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
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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
The NUMBER datatype 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';
• 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:
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.
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
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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:-
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;
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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;
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
ORDER BY Example:-
OUTPUT:-
Figure No.7
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
My SQL / Oracle:
OUTPUT:-
Figure No.8
OUTPUT:-
Figure No.9
OUTPUT:-
Figure No.10
UPDATE Syntax:-
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
UPDATE Example:-
Assume we wish to update the customer "Alfreds Futterkiste" with a new contact person and
city.
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.11
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
OUTPUT:-
Figure No.12
DELETE Syntax:-
DELETE Example:-
Assume we wish to delete the customer "Alfreds Futterkiste" from the "Customers" table.
OUTPUT:-
Figure No.13
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.14
Indexes, tables, and databases can easily be deleted/removed with the DROP statement.
OUTPUT:-
Figure No.15
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):
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:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.17
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 3
SQL/Oracle aggregate functions return a single value, calculated from values in a column.
AVG () Syntax:-
AVG () Example:-
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.1
Figure No.2
The MAX () function returns the largest value of the selected column.
MAX() Example:-
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.3
Figure No.4
The MIN() function returns the smallest value of the selected column.
MIN() Syntax:-
MIN() Example:-
OUTPUT:-
Figure No.5
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Fig
ure No.6
SUM() Example:-
OUTPUT:-
Figure No.7
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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
GROUP BY Syntax:-
OUTPUT:-
Figure No.9
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.10
LIKE Syntax:-
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
OUTPUT:-
Figure No.11
Figure No.12
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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.
Example:-
OUTPUT:-
Figure No.13
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.14
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 4
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:-
OUPTUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.5
Figure No.6
Example:-
FROM Customers
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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;
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.12
Database:-
Figure No.13
Figure No.14
Example:-
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.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Figure No.16
Database:-
Figure No.17
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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.
UNION Syntax:-
Database:-
Figure No.20
Figure No.21
OUTPUT:-
Figure No.22
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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
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
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:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.26
Figure No.27
Example:-
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
EXCEPT
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.28
***************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:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
[WHERE])condition
DATABASE:-
Figure No.29
EXAMPLE:-
SQL> SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.30
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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:-
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
Updating a View:-
EXAMPLE:-
UPDATE CUSTOMERS_VIEW
SET AGE = 35
WHERE name='Ramesh';
OUTPUT:-
Figure No.3
Deleting Rows into a View:-
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:
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:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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.
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;
OUTPUT:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
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:-
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Assignment statement:-
Figure No.4
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.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
• 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.
PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and
written in logical blocks of code. Each block consists of three sub-parts:
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.
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;
/
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
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
PRACTICAL # 7
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 8
8). PL/SQL Program to Accept Number and Find the Sum of Number.
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 9
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 10
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 11
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 12
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 13
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 14
14). PL/SQL Program that Accepts a Number and Find the Factorial
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 15
15). PL/SQL Program that Accepts a Number print its Fibonacci series
Figure No.1
OUTPUT:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 16
16). PL/SQL Program that Accepts Three Values and Display the Greater
Value
Figure No.1
OUTPUT:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 17
Figure No.1
OUTPUT:
Figure No.2
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 18
Figure No.1
OUTPUT:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 19
Figure No.1
OUTPUT:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 20
Figure No.1
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Figure No.2
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 21
PRACTICAL # 22
22). PL/SQL Program to insert or update the Ename and Job column of
Employee table using trigger.
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 23
PRACTICAL # 24
Output:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 25
25). PL/SQL Program of a local procedure that accept two number using
parameters.
Output:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18
PRACTICAL # 26
26) A stored procedure of PL\SQL that accepts two number and display
the product of two numbers.
OUTPUT:
Roll No.:-2028216 SUBJECT CODE:- BTCS501-18