Sie sind auf Seite 1von 6

Aditya Kumar Singh (CSE 2nd year) DBMS Practical

Question no.6

Write SQL query for sub query, nested query.

Subquery

In SQL a Subquery can be simply defined as a query within another query. In


other words we can say that a Subquery is a query that is embedded in WHERE
clause of another SQL query.

Table Name: Database

Table Name: Student

To display NAME, LOCATION, PHONE_NUMBER of the students from


DATABASE table whose section is A

Query:
Select NAME, LOCATION, PHONE_NUMBER from DATABASE
WHERE ROLL_NO IN
(SELECT ROLL_NO from STUDENT where SECTION=’A’);

17
Aditya Kumar Singh (CSE 2nd year) DBMS Practical

Practical No: 7
Write a program by the use of PL/SQL.
Question- Write a PL/SQL code to retrieve the employee name, join- basic, and
designation of an employee whose number input by the user.
SET SERVER OUTPUT ON
PROMPT Enter the employee number
ACCEPT N
DECLARE
DNAME EMP. EMP_NAME % TYPE;
DBASIC EMP.JOIN_BASIC % TYPE;
D DESIGN EMP.DESIG % TYPE
BEGIN
SELECT EMP_NAME, JOIN_BASIC, DESIG
INTO DNAME, DBASIC, DDESIG.
FROM EMP
WHERE EMP_NO =& N;
DBMS_OUTPUT.PUT_LINE (‘NAME’: DNAME);
DBMS_OUTPUT.PUT_LINE (‘salary:’ DBASIC);
DBMS.OUTPUT.PUT_LINE (Designation:’ DDESIG);
END;
SET SERVEROUTPUT OFF
SQL > START EMP
Or
SQR > @ EMP

18
Aditya Kumar Singh (CSE 2nd year) DBMS Practical

Practical No:8.
Concept of Roll Back, Commit & Check Points.
Roll back, commit and save point are collectively considered as Transaction
control commands.
Consider the following query:
SDL> Delete from customer where State = ‘Assam’;
SQL > Commit;
This will delete records permanently.
NQS, if we want to Rollback to a certain point, a save point may be “issued.
For example:
SAVE POINT DI;
DELETE from customer where state = ‘Delhi’;
SAVE POINT D2;
Delete from customer where state = ‘Kerala’;
Rollback to D2;
Commit;
This will undo the second delete operation. (i.e undo the delete state Kerala.)

19
Aditya Kumar Singh (CSE 2nd year) DBMS Practical

Practical No:9
10. (A) Create VIEWS, CURSORS & TRIGGERS and write ASSERTIONS.
VIEW
1) How to create view?
- Create view EView as select EMP Id, Emp- name,
D_ Name from EMP, DEBT
Where Emp. Dept _No = Dept.Dept-b No;
2) How to view the columns in a view?
- DESC EVIEW

3) How to delete a view?


- Drop view Eview;

CURSOR:
When the SQL’s select statement is expected to return more than one row then
cursor is used. Cursor is a buffer which will store the results of the recent query. It
is defined in the DECLARE section of Pl/SQL program.
Syntax – CURSOR <Cursor Name>
Is <Select statement>
To open the cursor: - OPEN<Cursor Name>
To store data in the cursor:-
Fetch <Cursor_ Name > into <Var 1>, <var 2>…….
or
Fetch < Cursor Name> into <Record _ Name>
To close the cursor:- CLOSE<cursor_ Name>

20
Aditya Kumar Singh (CSE 2nd year) DBMS Practical

10 (B) Cursor based program


Write a PL/SQL code to calculate the total and the % marks of the students in 4
subjects from the table ‘Students’ with the schema given below.
Student (Roll, S1, S2, S3, S4. Total, Percentage)
The roll nos. marks in each subject are stored in the database also update the database
Sot.
DECLARE
T Number;
PER Number;
Cursor Cl is select from student;
REC Cl % ROWTYPE;

BEGIN
Open Cl;
Loop
Fetch Cl INTO REC;
EXIT WHEN Cl % NOT FOUND;
T: = REC. Sl + REC.S2 + REC. S3 + REC. S4;
PER: = T/4;
UPDATE Student
SET TOTAL = T, PERCENTAGE = PER
WHERE RNo = REC. RNo;
END Loop;
CLOSE Cl;
END;

21
Aditya Kumar Singh (CSE 2nd year) DBMS Practical

10 (c) TRIGGERS: - A trigger consists of PL/SQL code which defines some action that the
database should take when some database related event occurs. Triggers are executed when a
DML is performed on a table for which the trigger has been written. Triggers are executed
automatically and are transparent to the user.

Q. Write a row trigger to insert the existing values of the salary table into a new
table when the salary table is updated.
Solution-
At first, open the editor & type the code given below in a file (RAGTR)
CREATE Trigger UPDSAL
Before update on salary
For each row
Begin
Insert into salary and values
(: old.Emp_no, :old.basic,
:old.commission, :old. Deduction,
: old. Salary. Date, : old. Department);
End
Now save the file. To execute the file, just type SQL> START RAJTR

22

Das könnte Ihnen auch gefallen