DBMS 1st Yr 2nd Sem
DBMS 1st Yr 2nd Sem
E-Mail: [email protected],Website:tsatamilcas.edu.in
Affiliated to Bharathiar University, Approved by UGC under Section 2(f) & 12(B)
I - B.COM (CA)
SEMESTER – II
NAME : ……………………………….
REGISTER NO : ……………………………….
THAVATHIRU SANTHALINGA ADIGALAR ARTS SCIENCE AND TAMIL COLLEGE
PERUR, COIMBATORE– 641 010
CERTIFICATE
Place : Perur
Date :
INDEX
1. IMPLEMENTATION OF DDL
COMMANDS
3.
EMPLOYEE DATABASE
4.
STUDENT DATABASE
5. COLLEGE DATABASE
6. BANK DATABASE
8. PALINDROMEAND NUMBER
REVERSE
9. EXCEPTION HANDLING
AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
ALGORTHIM:
CREATION OF TABLE:
Table created.
1 row created.
1 row created.
OUTPUT:
Table created.
1 row created.
ID NAME GAME
1 Mercy Cricket
1 Mercy cricket
2 Sharmi Tennis 19
OUTPUT:
MODIFY
desc student;
Sid Varchar(10)
Name Varchar(20)
Game Varchar(25)
Age Number(4)
DROP
SQL>Table dropped.
TRUNCATE TABLE
RESULT:
AIM;
To study the various DML commands and implement them on the database.
ALGORITHM:
Q1: Insert a single record into dept table.
1 row created.
Q2: Insert more than a record into emp table using a single insert command.
1 row created.
1 row created.
1 row created.
Q3: Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP
1 Mathi AP 1 10000
1 Mathi AP 1 10000
Q4: Create a pseudo table employee with the same structure as the table emp and insert rows
into the table using select clauses.
Table created.
SQL>desc employee;
EMPNO NUMBER(6)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Q7: List the records in the emp table orderby salary in ascending
order.
Ans: SQL> select * from emp orderby sal;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q8: List the records in the emp table orderby salary in descending
order.
Ans: SQL> select * from emp order by sal desc;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
4 Karthik Prof 2 30000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
1 Mathi AP 1 10000
5 Akalya AP 1 10000
Q10: Display deptno from the table employee avoiding the duplicated
values.
Solution:
1. Use SELECT FROM syntax.
2.Select should include distinct clause for the deptno.
Ans: SQL> select distinct deptno from emp;
DEPTNO
----------
1
TCL COMMAND
Queries:
departments;
Grant succeeded.
Savepoint created.
1 Mathi AP 1 10000
1 row created.
1 Mathi AP 1 10000
5 Raja AP 3 12000
1 Mathi AP 1 10000
Commit complete.
RESULT
Thus the DML, DCL,TCL commands was performed successfully and
executed.
EX.NO: 3 EMPLOYEE DATABASE
DATE:
AIM:
ALGORITHM:
Table created.
1 row created.
1 row created.
1 row created.
EMPNAME
------------------------------
aaa
bbb
SQL> select * from itbprg1 where salary in(select max(salary) from itbprg1);
SQL> select empname from itbprg1 where salary>(select salary from itbprg1 where
empname='ravi');
EMPNAME
bbb
RESULT:
AIM:
ALGORITHM:
Table created.
AVG(PERCENT)
----------------
86.633333
STUNAME
---------------
amutha
radhika
SQL> select * from student where percent>(select percent from student where rollno='12ca34');
STUNAME GENDER ROLLNO DEPT ADDR PERCENT
-----------------------------------------------------------------------------------------------------------
rahul m 12ca14 it tgnagar 75.5
amutha f 12it01 it as street 95
radhika f 12it10 it kl nagar 89.4
RESULT:
AIM:
To create staff and department detail tables and perform required query operation.
ALGORITHM:
Step 2: Create staff and department detail tables with necessary fields.
Table created.
Table created.
SQL>descdept;
Name Null? Type
-----------------------------------------------------------------
DEPTCODE NOT NULL VARCHAR2(4)
DEPTNAME VARCHAR2(30)
DEPT DEPTNAME
---- ------------------------------
d101 aeronautical
d102 cse
d103 ECE
d104 IT
d105 Mechanical
SQL>desc staff;
Name Null? Type
----------------------------------------------------------------------------
STAFFNO NOT NULL VARCHAR2(6)
STAFFNAME VARCHAR2(30)
DOB DATE
DEPTCODE VARCHAR2(4)
DESIG VARCHAR2(15)
BASIC NUMBER(7,2)
DOJ DATE
SQL> insert into staff values('s101','arjun','15-mar-73','d101','professor',25000);
1 row created.
7 rows selected
.
DOJ STAFFNAME
--------- ------------------------------
12-FEB-12 Dharun
13-DEC-11 Prabhavathy
13-DEC-11 Vasu
12-JAN-12 dinesh
26-JUL-12 Surya
STAFFNAME DEPTNAME
------------------------------ ------------------------------
arjun aeronautical
ramesh cse
Dharun cse
Prabhavathy ECE
Vasu IT
dinesh IT
Surya Mechanical
7 rows selected.
DEPT SUM(BASIC)
--------------------------
d101 25000
d102 37000
d103 13000
d104 28000
d105 15000
RESULT:
AIM:
To create account and borrower details tables and perform required query operation.
ALGORITHM:
SQL> create table bor1(acc_no number(4) constraint fk_ac1 references acc1(acc_no), amount number(8,2));
Table created.
SQL>insert into bor1 values(1001,25000)
1 row created.
SQL> insert into bor1 values(1002,50000)
1 row created.
SQL>insert into bor1 values(1003,36000)
1 row created.
SQL>insert into bor1 values(1004,70000)
1 row created.
Equi-join
SQL> select acc1.acc_no,cust_name,br_name,city,amount from acc1,bor1
where acc1.acc_no=bor1.acc_no;
Non-equi join
Outer-join
Self-join
ACC_NO AMOUNT
--------- ----------------
1002 25000
1003 25000
1004 25000
1004 50000
1002 36000
1004 36000
ACC_NO AMOUNT
--------- ---------
1001 25000
1002 50000
1003 36000
1004 70000
RESULT:
AIM:
ALGORITHM:
SQL> declare
2 fact number:=1;
3 num number:=#
4 i number;
5 begin
6 for i in 1 ..num loop
7 fact:=fact*i;
8 end loop;
9 dbms_output.put_line(fact);
10 end;
11 /
Enter value for num: 5
old 3: num number:=#
new 3: num number:=5;
120
RESULT:
Thus the Factorial and Fibonacci of given number has been executed successfully in PL/SQL.
EX.NO:8 PALINDROMEAND NUMBER REVERSE
DATE:
AIM:
ALGORITHM:
AIM:
ALGORITHM:
sql> declare
2 no integer;
3 rev integer:=0;
4 begin
5 no:=&no;
6 while (no>0) loop
7 rev:=rev*10+mod(no,10);
8 no:=trunc(no/10);
9 end loop;
10 dbms_output.put_line('reverse no. is'||' : '||rev);
11 end;
12 /
RESULT:
Thus the Palindrome and Number Reverse of given number has been executed successfully in
PL/SQL.
EX. NO: 9 EXCEPTION HANDLING
DATE:
AIM:
To generate a user defined exception in the user table using PL/SQL.
ALGORITHM:
Step 1: Start the process.
Step 2: Create the table master with the given fields.
Step 3: Insert the value for all the fields.
Step 4: Declare an exception zero_balance and a variable balance.
Step 5: Assign the value of balance into price.
Step 6: Check if price=0. If true then transfer the control to exception part.
Step 7: Execute the statements in exception part.
Step 8: Print an exception information.
Step 9: Stop the process.
SQL> create table master(client_id number(6), client_name varchar2(30), addr varchar2(50), phone
number(10), balance number(10,2));
Table created.
SQL>desc master;
SQL> setserveroutput on
SQL> declare
2 zerobal exception;
3 bal number (8,2);
4 begin
5 select balance into bal from master where client_id=101;
6 if bal=0 then
7 raise zerobal;
8 end if;
9 exception
10 when zerobal then
11 dbms_output.put_line ('client cannot have zero balance');
12 end;
13 /
client cannot have zero balance
PL/SQL procedure successfully completed
RESULT:
AIM:
To create a table and to create trigger to be fired when the record is deleted and inserted.
ALGORITHM:
Step 1: Start the process.
Step 2: Create tables product and vendor.
Step 3: Insert the values for the fields of the two tables.
Step 4: Create a trigger to be fired when the record from product table is deleted.
Step 5: Display an error message if the record is deleted on a particular day.
Step 6: Create a trigger to be fired when the record in vendor table is inserted
Step 7: Display a message if a record is inserted in vendor table.
Step 8: Stop the process.
SQL>create table product(product_code varchar2(7) constraint cpp primary key, product_name
varchar2(30),price number(6,2),quantity number(4));
Table created.
SQL>desc product;
SQL>descsvendor;
Output:
SQL> delete from product where product_code='101';
delete from product where product_code='101'
*
ERROR at line 1:
ORA-20050: Record cannot be deleted from product table
ORA-06512: at "SYSTEM.DELETE_TRIGGER", line 2
ORA-04088: error during execution of trigger 'SYSTEM.DELETE_TRIGGER'
Output:
SQL> insert into vendor values('Anu','DB Road','112');
Record inserted successfully into table vendor
RESULT:
AIM:
To create a table named voter list and new list to perform required query operation.
ALGORITHM:
Table created.
Table created.
SQL> create or replace trigger v_trig after delete on ab for each row
2 declare
3 vid number(5);
4 begin
5 vid:=:old.voter_id;
6 update cd set descri='shifted' where voter_id=vid;
7 end;
8 /
Trigger created.
1 row deleted.
RESULT:
DATE:
AIM:
To create table to store salary details of the employee and use cursor to update the employee details using
PL/SQL.
ALGORITHM:
Step 5: Calculate and update the employee’s net pay using the cursor.
Table created.
SQL>descemp_salary;
Name Null? Type
--------------------- -------- --------------------
EMP_NO NOT NULL NUMBER(4)
EMP_NAME VARCHAR2(30)
DESIGNATION VARCHAR2(25)
DEPT VARCHAR2(30)
BASIC NUMBER(5)
SQL>set serveroutput on
SQL>declare
2 netpay integer;
3 hra integer;
4 da integer;
5 pf integer;
6 detec integer;
7 allow integer;
8 basic integer;
9 cursor cursor_salary is select * from emp_salary;
10 salemp_salary%rowtype;
11 begin
12 open cursor_salary;
13 dbms_output.put_line('enoenamedeptdesigpfgross_paynet_pay');
14 loop
15 fetch cursor_salary into sal;
16 exit when cursor_salary%notfound;
17 hra:=sal.basic*(5/100);
18 da:=sal.basic*(13/100);
19 pf:=sal.basic*(20/100);
20 allow:=sal.basic+hra+da;
21 detec:=pf;
22 netpay:=allow-detec;
23 update emp_salary set Net_pay = netpay where
Emp_no=sal.Emp_no;
dbms_output.put_line(sal.emp_no||','||sal.emp_name||','||sal.dept||','||
sal.designation||’,’||allow||','||pf||','||netpay);
24 end loop;
25 close cursor_salary;
26 end;
27 /
OUTPUT:
eno ename dept desig pf gross_pay net_pay
202, Abi, Test Bench, Tester, 2360, 400, 1960
203,Bharathi,Design, Designer,1770, 300, 1470
204,Azar, Development, Analyst,2950, 500, 2450
RESULT:
AIM:
To create a procedure and check if the date of the last purchase is greater than 1 year from the current date
using PL/SQL.
ALGORITHM:
Step 4: If last_purchase date for the given item_id is greater than 1 year delete the item
Step 5: If date is less than 1 year update the stock with the new value.
Step 6: Display the details of the stock table to view the updated details.
SQL>desc stock;
OUTPUT:
RESULT:
Thus the Procedure creation and seeking record has been executed successfully in PL/SQL.
EX. NO: 14 FUNCTION CREATION : RECORD SEEK
DATE:
AIM:
To create a function and to search for address using phone number using PL/SQL.
ALGORITHM:
Step 2: To create a table phone book with given fields such as phone no, user_
name, door no, street, city and pin code.
Step 5: Pass the phone number as argument and find the address
SQL>desc phone;
OUTPUT:
Nandhu,TNagar,CHE
PL/SQL procedure successfully completed.
RESULT: