0% found this document useful (0 votes)
1K views11 pages

DBMS Cep 6

This document contains questions and answers related to database management systems. It includes SQL queries to retrieve data from tables, descriptions of database commands like GRANT, REVOKE, ROLLBACK and COMMIT. It also covers aggregation functions, cursors and triggers - providing examples of each.

Uploaded by

GEM FOR GAMES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views11 pages

DBMS Cep 6

This document contains questions and answers related to database management systems. It includes SQL queries to retrieve data from tables, descriptions of database commands like GRANT, REVOKE, ROLLBACK and COMMIT. It also covers aggregation functions, cursors and triggers - providing examples of each.

Uploaded by

GEM FOR GAMES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SILVER OAK COLLEGE OF ENGINEERING AND TECHNOLOGY

AFFILIATED TO GUJARAT TECHNOLOGICAL UNIVERSITY


COMPUTER ENGINEERING / INFORMATION TECHNOLOGY DEPARTMENT

Subject: Database Management System Semester: B.E III


Subject Code: 2130703

31. Write SQL statements (Query) for following tables:

T1(rollno, stuname, age, city, branchcode)


T2(branchcode, branchname)

1. Retrieve students details whose branchcode is 5.


Select * from T1 where branchcode=5;

2. Find an average age of all students.


Select avg(age) from T1;

3. Add new branch in T2 table.


Alter table T2 add (new_branch varchar2(15));

4. Display rollno, stuname and age of students whose city is Chennai.


Select rollno, stuname, age from T1 where city=’Chennai’;

5. Change age of student to 20 whose rollno is 1.


Update T1 set age=20 where rollno=1;

6. Delete student details whose age is 18.


Delete from T1 where age=18;

7. Retrieve branch information in descending order.


Select * from T2 order by branchcode desc;

32. Describe following commands.


I) GRANT
II) REVOKE
III) ROLLBACK
IV) COMMIT

GRANT:
• This command gives rights to user for a data items.
Syntax:-
• GRANT privilege ON object TO user [WITH GRANT OPTION]
REVOKE:

• This command takes back rights from user for a data items.
Syntax:- REVOKE privilege ON object FROM user {RESTRICT/CASCADE}
• Privileges are various operations that we will perform on table or view E.g.
INSERT, UPDATE, DELETE, SELECT etc.
• Object is table or view or any data item.
• User is the name of user.
• With grant option is used if user wants to pass the rights to other user. Means if a
user get a rights with the grant option then he/she can give this rights to another
user.
• When user executes a REVOKE command with the cascade option then it will
take back given rights from all the users who get those rights from this user.

ROLLBACK:
• The execution of ROLLBACK erases all the modification made by the current
transaction.
• once the ROLLBACK statement is executed the database reaches its previous
state.
• Example: Before the execution of first statement of the transaction.
• ROLLBACK occurs when the transaction is aborted in middle of the execution.
• Syntax:- ROLLBACK;
COMMIT:
• COMMIT validates the modification made by the current transaction .
• After execution of COMMIT statement the transaction can not be ROLLBACK.
• COMMIT occurs when the transaction gets executed successfully.
• Syntax:- COMMIT;
33. List and explain aggregation functions with suitable example
Aggregate Functions are all about
• Performing calculations on multiple rows
• Of a single column of a table
• And returning a single value.
The ISO standard defines five (5) aggregate functions namely;
1) COUNT
2) SUM
3) AVG
4) MIN
5) MAX

COUNT Function

➔ The COUNT function returns the total number of values in the specified field. It
works on both numeric and non-numeric data types. All aggregate functions
bydefault exclude nulls values before working on the data.
➔ COUNT (*) is a special implementation of the COUNT function that returns the
count of all the rows in a specified table. COUNT (*) also considers Nulls and
duplicates.

DISTINCT Keyword

➔ The DISTINCT keyword that allows us to omit duplicates from our results
➔ SELECT COUNT(DISTINCT column_name) FROM tablename

MIN function

➔ The MIN function returns the smallest value in the specified table field.
➔ SELECT MIN(column_name) FROM tablename;
MAX function

➔ Just as the name suggests, the MAX function is the opposite of the MIN
function. It returns the largest value from the specified table field.
➔ SELECT MAX(column_name) FROM tablename;

SUM function

➔ SUM function which returns the sum of all the values in the specified column.
SUM works on numeric fields only. Null values are excluded from the result
returned.
➔ SELECT SUM(column_name) FROM tablename;

AVG function

➔ AVG function returns the average of the values in a specified column. Just like
the SUM function, it works only on numeric data types.
➔ SELECT AVG(column_name) FROM tablename;

34. Write Short note on with examples


1) Cursor with types
2) Trigger with advantage

(1) Cursors with types


A cursor is a pointer to the context area. PL/SQL controls the context area through a
cursor. A cursor holds the rows (one or more) returned by a SQL statement. The set of
rows the cursor holds is referred to as the active set.
You can name a cursor so that it could be referred to in a program to fetch and process
the rows returned by the SQL statement, one at a time. There are two types of cursors
 Implicit cursors
 Explicit cursors
Implicit Cursors
• Implicit cursors are automatically created by Oracle whenever an SQL statement
is executed, when there is no explicit cursor for the statement. Programmers
cannot control the implicit cursors and the information in it.
• Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an
implicit cursor is associated with this statement. For INSERT operations, the
cursor holds the data that needs to be inserted. For UPDATE and DELETE
operations, the cursor identifies the rows that would be affected.
The following table provides the description of the most used attributes –

1. %FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one
or more rows or a SELECT INTO statement returned one or more rows.
Otherwise, it returns FALSE.

2. %NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT,
UPDATE, or DELETE statement affected no rows, or a SELECT INTO
statement returned no rows. Otherwise, it returns FALSE.

3. %ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the SQL
cursor automatically after executing its associated SQL statement.

4. %ROWCOUNT
Returns the number of rows affected by an INSERT, UPDATE, or DELETE
statement, or returned by a SELECT INTO statement.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over
the context area. An explicit cursor should be defined in the declaration section of the
PL/SQL Block. It is created on a SELECT Statement which returns more than one row.

The syntax for creating an explicit cursor is −


CURSOR cursor_name IS select_statement;

Working with an explicit cursor includes the following steps −

• Declaring the cursor for initializing the memory


• Opening the cursor for allocating the memory
• Fetching the cursor for retrieving the data
• Closing the cursor to release the allocated memory

Declaring the Cursor


Declaring the cursor defines the cursor with a name and the associated SELECT
statement. For example −
CURSOR c_customers IS

SELECT id, name, address FROM customers;

Opening the Cursor


Opening the cursor allocates the memory for the cursor and makes it ready for fetching
the rows returned by the SQL statement into it. For example, we will open the above
defined cursor as follows −
OPEN c_customers;
Fetching the Cursor
Fetching the cursor involves accessing one row at a time. For example, we will fetch
rows from the above-opened cursor as follows −
FETCH c_customers INTO c_id, c_name, c_addr;

Closing the Cursor


Closing the cursor means releasing the allocated memory. For example, we will close
the above-opened cursor as follows −
CLOSE c_customers;

(2) Triggers
Triggers are stored programs, which are automatically executed or fired when some
events occur. Triggers are, in fact, written to be executed in response to any of the
following events −
• A Database Manipulation (DML) statement (DELETE, INSERT OR
UPDATE)
• A database definition (DDL) statement (CREATE, ALTER, or DROP).
• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).
Triggers can be defined on the table, view, schema, or database with which the event
is associated.

Advantages of Triggers
• Triggers can be written for the following purposes −
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions

Creating Triggers
The syntax for creating a trigger is −
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Where,
• CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an
existing trigger with the trigger_name.
• {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be
executed. The INSTEAD OF clause is used for creating trigger on a view.
• {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML
operation.
• [OF col_name] − This specifies the column name that will be updated.
• [ON table_name] − This specifies the name of the table associated with the
trigger.
• [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and
old values for various DML statements, such as INSERT, UPDATE, and
DELETE.
• [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected. Otherwise the trigger will execute just
once when the SQL statement is executed, which is called a table level trigger.
• WHEN (condition) − This provides a condition for rows for which the trigger
would fire. This clause is valid only for row-level triggers.

35. Explain stored procedure with proper example


A stored procedure is a prepared SQL code that you can save, so the code can be
reused over and over again.So if you have an SQL query that you write over and over
again, save it as a stored procedure, and then just call it to execute it.You can also pass
parameters to a stored procedure, so that the stored procedure can act based on the
parameter value(s) that is passed.
A procedure is created with the CREATE OR REPLACE PROCEDURE statement.
The simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as
follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;
Where,
• procedure-name specifies the name of the procedure.
• [OR REPLACE] option allows the modification of an existing procedure.
• The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside and OUT
represents the parameter that will be used to return a value outside of the
procedure.
• procedure-body contains the executable part.
• The AS keyword is used instead of the IS keyword for creating a standalone
procedure.
Example
Demo Database Table
Below is a selection from the "Customers" table as sample database:
CustID CustName Address City PostalCode
1 Alfreds Futterkiste Obere Str. 57 Berlin 12209
Berlin México Avda. de la México
2 Emparedados y Constitución 2222 D.F. 05021
3 Antonio Moreno Mataderos 2312 México 05023
Taquería D.F.

Stored Procedure Example


The following SQL statement creates a stored procedure named "SelectAllCustomers"
that selects all records from the "Customers" table:
Example
EXEC SelectAllCustomers;

Stored Procedure With Multiple Parameters


Setting up multiple parameters is very easy. Just list each parameter and the data type
separated by a comma as shown below. The following SQL statement creates a stored
procedure that selects Customers from a particular City with a particular PostalCode
from the "Customers" table:
Example
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode
nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode =
@PostalCode
GO;
Execute the stored procedure above as follows:

Example
EXEC SelectAllCustomers City = "Mexico", PostalCode = "05021";

36. Write PL/SQL code to print sum of even numbers between 1 to 100 numbers
DECLARE
-- Declare variable
num NUMBER(3) := 0;
sum NUMBER(4) := 0;
BEGIN
WHILE num <= 100 LOOP
sum := sum1 + num;
num := num + 2;
END LOOP;
dbms_output.Put_line('Sum of even numbers is ' || sum);
END;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy