DBMS Module 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

1.

Define Stored Procedure, Explain the creating and calling of stored


procedure with suitable example.

 A stored procedure is a set of Structured Query Language (SQL)


statements with an assigned name, which are stored in a relational
database management system (RDBMS) as a group, so it can be reused
and shared by multiple programs.
 Stored procedures are also beneficial for software engineering reasons.
 Once a stored procedure is registered with the database server, different
users can re-use the stored procedure, eliminating duplication of efforts
in writing SQL queries or application logic, and making code maintenance
easy.
Creating a Simple Stored Procedure

 Stored procedures must have a name; this stored procedure has the name
'ShowNumberOfOrders.' Otherwise, it just contains an SQL statement
that is precompiled and stored at the server.

STORED PROCEDURE IN SQL

 Stored procedures can also have parameters.


 These parameters have to be valid SQL types, and have one of three
different modes: IN, OUT, or INOUT.
 IN parameters are arguments to the stored procedure.
 OUT parameters are returned from the stored procedure. It assigns
values to all OUT parameters that the user can process.
 INOUT parameters combine the properties of IN and OUT parameters.
They contain values to be passed to the stored procedures, and the stored
procedure can set their values as return values.
 Stored procedures enforce strict type conformance. If a parameter is of
type INTEGER, it cannot be called with an argument of type VARCHAR.
 Let us look at an example of a stored procedure with arguments. The
stored procedure shown in Figure 2.1 has two arguments: book_isbn and
addedQty. It updates the available number of copies of a book with the
quantity from a new shipment.

STORED PROCEDURE WITH ARGUMENT


 Stored procedures do not have to be written in SQL; they can be written
in any host language. As an example, the stored procedure shown below
is a Java function that is dynamically executed by the database server
whenever it is called by the client.

STORED PROCEDURE IN JAVA


Calling Stored Procedures

 Stored procedures can be called in interactive SQL with the CALL


statement:
 CALL storedProcedureName (argumentl, argument2..., argumentN);
 In Embedded SQL, the arguments to a stored procedure are usually
variables in the host language. For example, the stored procedure Add
Inventory would be called as follows:
2. Types of JDBC (Java Database Connectivity) drive:
Drivers in JDBC are classified into four types depending on the architectural
relationship between the application and the data source:
1. Type I-Bridges:
 This type of driver translates JDBC function calls into function calls
of another API (application programming language) that is not
native to the DBMS.
 An example is the JDBC-ODBC (open database connectivity) bridge.
 An application can use JDBC calls to access an ODBC compliant data
source.
 The application loads only one driver, the bridge.
 Bridges have the advantage that is easy to piggyback the
application onto an existing installation, and no new drivers have
to be installed.
 But using bridges has several drawbacks. The increased number of
layers between data source and application affects performance.
 In addition, the user is limited to the functionality that the ODBC
driver supports.

2. Type II-Direct Translation to the Native API via Non-Java Driver:


 This type of driver translates JDBC function calls directly into method
requests of the API of one specific data source.
 The driver is usually written using a combination of C++ and Java; it is
dynamically linked and specific to the data source.
 This architecture performs significantly better than a JDBC-ODBC
bridge.
 One disadvantage is that the database driver that implements the API
needs to be installed on each computer that runs the application.

3. Type III - Network Bridges:


 The driver talks over a network to a middleware server that translates
the JDBC requests into DBMS-specific method invocations.
 In this case, the driver on the client site (i.e., the network bridge) is not
DBMS-specific.
 The JDBC driver loaded by the application can be quite small, as the
only functionality it needs to implement is sending of SQL statements
to the middleware server.
 The middleware server can then use a Type II JDBC driver to connect
to the data source.

4. Type IV-Direct Translation to the Native API via Java Driver:


 Instead of calling the DBMS API directly, the driver communicates with
the DBMS through Java sockets.
 In this case, the driver on the client side is written in Java, but it is
DBMS-specific.
 It translates JDBC calls into the native API of the database system.
 This solution does not require an intermediate layer, and since the
implementation is all Java, its performance is usually quite good
3. DBMS Architecture: single, two, three tier

1. One Tier Architecture (Single Tier Architecture/ Centralized


architecture)
 Tier Architecture in DBMS is the simplest architecture of Database in
which the client, server, and Database all reside on the same machine.
 A simple one tier architecture example would be anytime you install
a Database in your system and access it to practice SQL queries.
 But such architecture is rarely used in production.
 Benefit
 easily maintained by a central administrator
 Drawback:
 Do not scale to thousands of users
2. Two Tier Architecture (Basic 2-tier Client-Server Architectures)
 2 Tier Architecture in DBMS is a Database architecture where the
presentation layer runs on a client (PC, Mobile, Tablet, etc.), and data
is stored on a server called the second tier.
 Two tier architecture provides added security to the DBMS as it is not
exposed to the end-user directly.
 It also provides direct and faster communication.
 Client connected to the servers via some form of network. (LAN: local
area network, wireless network, etc.)
 Dbms server provides database query and transaction services to the
clients.
 Relational DBMS servers are often called SQL servers, query servers,
or transaction servers
 Example: Contact Management System created using MS-Access or
Railway Reservation System, etc.

 Advantages:
1. Easy to maintain and modification is bit easy
2. Communication is faster
 Disadvantages:
1. In two tier architecture application performance will be degrade
upon increasing the users.
2. Cost-ineffective

3. Three Tier Architecture (Three-tier client /server Architecture)


 A 3 Tier Architecture in DBMS is the most popular client server
architecture in DBMS in which the development and maintenance of
functional processes, logic, data access, data storage, and user
interface is done independently as separate modules.
 Three Tier architecture contains a presentation layer, an application
layer, and a database server.
 3-Tier database Architecture design is an extension of the 2-tier client-
server architecture.
 A 3-tier architecture has the following layers:
a) Presentation layer (your PC, Tablet, Mobile, etc.)
b) Application layer (server)
c) Database Server
 The Application layer resides between the user and the DBMS, which
is responsible for communicating the user’s request to the DBMS
system and send the response from the DBMS to the user.
 The application layer (business logic layer) also processes functional
logic, constraint, and rules before passing data to the user or down to
the DBMS.
 The goal of Three Tier client-server architecture is:
 To separate the user applications and physical database
 To support DBMS characteristics
 Program-data independence
 Supporting multiple views of the data
 Benefit:

1. High performance, lightweight persistent objects


2. Scalability – Each tier can scale horizontally
3. Better Re-use
4. Improve Data Integrity
5. Improved Security – Client is not direct access to database.
6. Easy to maintain and modification is bit easy, won’t affect other
modules
7. In three tier architecture application performance is good.

 Drawback: Increase Complexity/Effort


 Example: Designing registration form which contains text box, label,
button or a large website on the Internet, etc.

4.Difference between SQLJ and JDBC.

SQLJ JDBC
SQLJ (SQL in Java) is a technology JDBC (Java Database Connectivity) is a
that embeds SQL statements Java API for executing SQL statements
directly into Java code.
It provides a way to execute It provides a standard way for Java
database statements as part of a programs to interact with databases.
Java program without using JDBC.
SQLJ is a more concise and JDBC is a more flexible and powerful
convenient way to interact with way to interact with databases
databases
SQLJ is a higher-level JDBC is a lower-level
SQLJ is more suitable for projects JDBC is more suitable for projects that
that require fast performance and require greater control and portability
concise code. across different databases.
5.Difference between dynamic and embedded SQL.
 Static or Embedded SQL are SQL statements in an application that do
not change at runtime and, therefore, can be hard-coded into the
application.
 Dynamic SQL is SQL statements that are constructed at runtime; for
example, the application may allow users to enter their own queries.

Dynamic(interactive) SQL Embedded SQL(static)


SQL statements are compiled at SQL statements are compiled at
run time. compile time.

It is less swift and efficient. It is more swift and efficient.


It is more flexible. It is less flexible.
It is generally used for situations It is generally used for situations
where data is disturbed non where data is disturbed
uniformly uniformly
EXECUTE IMMEDIATE, EXECUTE EXECUTE IMMEDIATE, EXECUTE
and PREPARE statements are used and PREPARE statements are
not used

6.What is CGI? Why was CGI introduced? What are the disadvantages of an
architecture using CGI scripts?

 Common Gateway Interface connects HTML forms with application


programs.
 It is a protocol that defines how arguments from forms are passed to
programs at the server side.
 Programs that communicate with the webserver via CGI are often called
CGI scripts, since many such application programs were written in a
scripting language such Ike.; Perl.
 As an example of a program that interfaces with an HTML form via CGI,
consider the sample page shown in Figure 6.1. This webpage contains a
form where a user can fill in the name of an author.
 If the user presses the 'Send it' button, the Perl script 'findBooks.cgi'
shown in Figure 7.14 is executed as a separate process.
 The CGl protocol defines how the communication between the form and
the script is performed.
 Features of CGI:
a) It is a very well defined and supported standard.
b) CGI scripts are generally written in either Perl, C, or maybe just a
simple shell script.
c) CGI is a technology that interfaces with HTML.
d) CGI is the best method to create a counter because it is currently
the quickest
e) CGI standard is generally the most compatible with today's
browsers
 Advantages of CGI:
i. The advanced tasks are currently a lot easier to perform in CGI than
in Java.
ii. It is always easier to use the code already written than to write your
own.
iii. CGI specifies that the programs can be written in any language, and
on any platform, as long as they conform to the specification.
iv. CGI-based counters and CGI code to perform simple tasks are
available in plenty.
 Disadvantages of CGI:
There are some disadvantages of CGI which are given below:
i. In Common Gateway Interface each page load incurs overhead by
having to load the programs into memory.
ii. Generally, data cannot be easily cached in memory between page
loads.
iii. There is a huge existing code base, much of it in Perl.
iv. CGI uses up a lot of processing time.

7.insert, delete, update, alter, drop, select:


CREATE TABLE

 The CREATE TABLE statement is used to create a new table in a database.


In that table, if you want to add multiple columns, use the below syntax.
 Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
 The column parameters specify the names of the columns of the table.
 The data type parameter specifies the type of data the column can hold
(e.g., Varchar, integer, date, etc.).
 Example:
CREATE TABLE Employee (
EmpId int,
LastName varchar (255),
FirstName varchar (255),
Address varchar (255),
City varchar (255)
);
 The EmpId column is of type int and will hold an integer.
 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.
INSERT VALUE

 The INSERT INTO statement is used to insert new records in a table.


 Syntax:
INSERT INTO table_name VALUES (value1, value2, value3, ...);
 Example
INSERT INTO Employee VALUES (3, 'XYZ', 'ABC', 'India', 'Mumbai' );

SELECT STATEMENT
 The SELECT statement is used to select data from a database.
 Syntax:
SELECT column1, column2, ... FROM table_name;
 Here, column1, column2, ... are the field names of the table
you want to select from the data.
SELECT * FROM table_name;
 If the above query is executed, then all record is displayed.
 Example
1. Select EmpId, LastName from Employee;
2. Select * from Employee;

UPDATE TABLE
 The UPDATE statement is used to modify the existing records in a table.
 Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
 Example
UPDATE Employee
SET FirstName= 'KS', City= 'Pune'
WHERE EmpId= 1;
If the above query is executed then for EmpId= 1, "Firstname" and "City"
column data will be updated.

DELETE STATEMENT
 The DELETE statement is used to delete existing records in a table for a
particular Record.
 Syntax
DELETE FROM table_name WHERE condition;
 Example
DELETE FROM Employee WHERE EmpId=1;
In Employee table EmpId = 1 record gets deleted.

DROP STATEMENT:
 The DROP TABLE statement is used to drop an existing table in a database.
 Syntax
DROP TABLE table_name;
 Example
DROP TABLE Shippers;

TRUNCATE TABLE
 The TRUNCATE TABLE command deletes the data inside a table, but not
the table itself.
 Syntax
TRUNCATE TABLE table_name;
 Example
TRUNCATE TABLE Shippers;

The ALTER Command:


 The definition of a base table or of other named schema elements can be
changed by using the ALTER command. For base tables, the possible alter
table actions include adding or dropping a column (attribute), changing a
column definition, and adding or dropping table constraints.
 Syntax:
ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR (12);

 For example, the following command removes the attribute Address from
the EMPLOYEE base table:
ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;
 It is also possible to alter a column definition by dropping an existing
default clause or by defining a new default clause.
 The following examples illustrate this clause:
i. ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn
DROP DEFAULT;
ii. ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn
SET DEFAULT ”333445555‟;
 One can also change the constraints specified on a table by adding or
dropping a named constraint.
 To be dropped, a constraint must have been given a name when it was

8. Aggregate function and types:

Aggregate functions are used to summarize information from multiple


tuples into a single-tuple summary.
TYPES OF AGGREGATE FUNCTION:

1. COUNT FUNCTION
 COUNT function is used to Count the number of rows in a
database table. It can work on both numeric and non-numeric
data types.
 COUNT function uses the COUNT (*) that returns the count of all
the rows in a specified table. COUNT (*) considers duplicate and
Null.
 Syntax
COUNT (*) or COUNT([ALL\DISTINCT] expression)

 Example:
1. Count()
SELECT COUNT (*) FROM PRODUCT;

2. COUNT() with HAVING and GROUP BY


SELECT COMPANY, COUNT(*)
FROM PRODUCT
GROUP BY COMPANY
HAVING COUNT(*)>2;

2. SUM FUNCTION
 Sum function is used to calculate the sum of all selected columns.
It works on numeric fields only.
 Syntax
SUM () or SUM ([ALL|DISTINCT] expression)
 Example:
a) SUM ()
SELECT SUM(COST)
FROM PRODUCT;
b) SUM() with HAVING and GROUP BY
SELECT COMPANY, SUM(COST)
FROM PRODUCT
GROUP BY COMPANY
HAVING SUM(COST)>=170;

3. AVG FUNCTION
 The AVG function is used to calculate the average value of the
numeric type. AVG function returns the average of all non-Null
values.
 Syntax: AVG( ) or AVG( [ALL|DISTINCT] expression )
 Example:
SELECT AVG(COST)
FROM PRODUCT_MAST;
4. MAX FUNCTION
 MAX function is used to find the maximum value of a certain
column. This function determines the largest value of all
selected values of a column.
 Syntax: MAX() or MAX( [ALL|DISTINCT] expression )
 Example:
SELECT MAX(RATE)
FROM PRODUCT;

5. MIN FUNCTION
 MIN function is used to find the minimum value of a certain
column. This function determines the smallest value of all
selected values of a column.
 Syntax: MIN() or MIN( [ALL|DISTINCT] expression )
 Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;

You might also like