DBMS File
DBMS File
A
PRACTICAL
FILE
ON
DBMS
These products are constantly undergoing change and evolving. The natural
language of this RDBMS product is ANSI SQL,PL/SQL a superset of ANSI
SQL.oracle 8i and 9i also under stand SQLJ.
Oracle corp has also incorporated a full-fledged java virtual machine into its
database engine.since both executable share the same memory space the JVM
can communicate With the database engine with ease and has direct access to
oracle tables and their data.
SQL is structure query language.SQL contains different data types those are
1. char(size)
2. varchar2(size)
3. date
4. number(p,s)
5. long
6. raw/long raw
DDL commands:
1. The Create Table Command: - it defines each column of the table uniquely.
Each column has minimum of three attributes, a name , data type and size.
Syntax:
Ex:
Syntax:
Ex:
Syntax:
Ex:
Syntax:
Ex:
Syntax:
Ex:
Syntax:
Ex:
7. Destroying tables.
Syntax:
Ex:
DML commands:
8. Inserting Data into Tables: - once a table is created the most natural thing
to do is load this table with data to be manipulated later.
Syntax:
9. Delete operations.
Syntax:
Syntax:
Syntax:
Syntax:
Syntax:
<col><datatype>(size)not null
b) unique constraint
Syntax:
<col><datatype>(size)unique;
Syntax:
Syntax:
<col><datatype>(size)primary key;
Syntax:
primary key(col1>,<col2>);
Syntax:
Syntax:
h) check constraint
DQL Commands:
12. Viewing data in the tables: - once data has been inserted into a table,
the next most logical operation would be to view what has been inserted.
Syntax:
13. Filtering table data: - while viewing data from a table, it is rare that all
the data from table will be required each time. Hence, sql must give us a method
of filtering out data that is not required data.
Syntax:
Syntax:
Syntax:
Syntax:
DCL commands:
Oracle provides extensive feature in order to safeguard information stored in its
tables from unauthoraised viewing and damage.The rights that allow the user of
some or all oracle resources on the server are called privileges.
Syntax:
Syntax:
REVOKE<object privilege>
ON
FROM<user name>;
1.: Create the tables with the appropriate integrity constraints and Insert
around 10 records in each of the tables
Output:
CUST_NAME VARCHAR2(15)
Output:
CUST_ID CUST_NAME
---------- ---------------
100 ramu
101 kamal
102 raju
104 lawrence
Output:
……………………………………………………………………………………………………
Item_name VARCHAR2(15)
PRICE NUMBER(6,2)
Output:
……………………………………………………………………………………..
SQL>dsec sale
Output:
………………………………………………………………………………………..
BILL_DATE DATE
CUST_ID NUMBER(5)
ITEM_ID NUMBER(4)
QTY_SOLD NUMBER(4)
Output:
………………………………………………………………………………………………………...
c) List all the bills for the current date with the customer names and item
numbers
s.billdate=to_char(sysdate);
d) List the total Bill details with the quantity sold, price of the item and the
final amount
SQL> select i.price, s.qty,(i.price*s.qty) total from item I, sale s where
i.itemid=s.itemid;
------------ --------
120 2 240
20 3 60
5 2 10
10 1 10
350 4 1400
e) List the details of the customer who have bought a product which has a
price>200
CUSTID CUSTNAME
--------- --------------
4 duffy
f) Give a count of how many products have been bought by each customer
SQL> select custid, count(itemid) from sale group by custid;
CUSTID COUNT(ITEMID)
---------- ---------------------
1 2
3 1
4 1
5 1
SQL> select i.itemname from item i, sale s where s.custid=5 and i.itemid-
s.itemid;
ITEMNAME
--------------
Pens
and s.billdate=to_char(sysdate);
ITEMID ITEMNAME
--------- -------------
1234 pencil
view created.
……………………………………………………………………………………………
SQL>desc student;
………………………………………………………………………………………..
STUD_NAME VARCAHR2(15)
STUD_NO STUD_NAME
....................................................................
508 HARISH
513 BALAJI
518 RAKESH
524 PAVAN
534 JOYCE
SQL>dsec membership;
…………………………………………………………………………………………………….
STUD_NO NUMBER(5)
Errors Observed:
ERROR at line 1:
MEM_NO STUD_NO
………………………………………………………………………..
5440 513
5441 508
5442 518
5443 534
5444 524
SQL>desc book;
………………………………………………………………………………………..
BOOK_NAME VARCHAR2(20)
AUTHOR VARCHAR2(20)
………………………………………………………………………………………………..
SQL>desc iss_rec;
………………………………………………………………………………………………………
ISS_DATE DATE
MEM_NO NUMBER(5)
BOOK_NO NUMBER(5)
…………………………………………………………………………………………………
STUDNAME MEMNO
------------- --------
abhijeet 1001
arun 1002
arvind 1003
ashish 1004
ashwin 1005
d) List all the issues for the current date with student and Book names
i.issdate=to_char(sysdate);
------------------- ---------------
13 arvind P&S
e) List the details of students who borrowed book whose author is CJDATE
SQL> select * from student where studno in(select studno from membership
where memno in
2 (select memno from iss_rec where bookno in(select bookno from book where
author=’CJDATE’)));
STUDNO STUDNAME
---------- -------------
505 ashwin
f) Give a count of how many books have been bought by each student
STUDNO COUNT(I.BOOKNO)
---------- -----------------------
501 5
502 5
503 5
504 5
505 5
SQL> select bookname from book where bookno in (select bookno from iss_rec
where
BOOKNAME
-------------
NT
SQL> delete from book where bookno in(select bookno from iss_rec where
issdate=to_char(sysdate));
delete from book where bookno in (select bookno from iss_rec where
issdate=to_char(sysdate))
Errors Observed:
ERROR at line 1:
i) Create a view which lists out the iss_no, iss _date, stud_name, book
name
j) Create a view which lists the daily issues-date wise for the last one week
SQL>desc employee;
………………………………………………………………………………………..
EMP_NAME VARCHAR2(25)
EMP_ID EMP_NAME
………………………………………………………….
10 Robert
21 Coulthard
30 Fernando Alonso
39 Kartikeyan
87 Kimmi
SQL>desc department;
………………………………………………………………………………………..
DEPT_NAME VARCHAR2(20)
DEPT_ID DEPT_NAME
……………………………………………………………………………..
100 sales
101 accounts
102 administration
103 production
104 supervisor
SQL>desc paydetails;
………………………………………………………………………………………..
EMP_ID NUMBER(5)
DEPT_ID NUMBER(5)
BASIC NUMBER(7,2)
DEDUCTIONS NUMBER(5,2)
ADDITIONS NUMBER(5,2)
DOJ DATE
&basic,&deductions,&additions,&doj);
…………………………………………………………………………………………………………………..
SQL>desc payroll;
………………………………………………………………………………………..
EMP_ID NUMBER(5)
PAY_DATE DATE
EMP_ID PAY_DATE
………………………………………………………….
10 31-JAN-06
21 03-FEB-06
30 15-JAN-06
39 27-JAN-06
87 04-FEB-06
EMPID DEPTID
…………………………
401 500
402 200
403 600
404 400
405 1200
d) List all the employee names who joined after particular date
EMPNAME
…………………
AVINASH
NITIN
PHALGUN
e) List the details of employees whose basic salary is between 10,000 and
20,000
sqL> Select empid,empname from employee where salary between 10000 and
20000;
EMPID EMPNAME
…………………………….
402 AKHILA
403 PRIYANKA
EMPID EMPNAME
…………………………….
AKHILA
………………………………………………………
1 200
1 400
1 500
1 600
1 1200
SQL> select empname from employee where empid in(select empid from paydet
where basic-deduction>10000);
EMPNAME
………………
AVINASH
AKHILA
HARISH
NITIN
PHALGUN
EMPID EMPNAME
------------------------------------------
5 Coulthard
d) List all the issues for the current date with the customer names and
cassette names
e) List the details of the customer who has borrowed the cassette whose title
is “ The Legend”
f) Give a count of how many cassettes have been borrowed by each
customer
g) Give a list of book which has been taken by the student with mem_no as 5
h) List the cassettes issues for today
i) Create a view which lists outs the iss_no, iss_date, cust_name, cass_name
j) Create a view which lists issues-date wise for the last one week
SQL>desc customer;
……………………………………………………………………………………………………………..
CUST_NAME VARCHAR2(20)
CUST_NO CUST_NAME
……………………………………………………………….
50 scott
51 pandey
52 varshney
53 naidu
54 bhimbra
SQL>dsec membership;
………………………………………………………………………………………………………...
CUST_NO NUMBER(5)
MEM_NO CUST_NO
…………………………………………………
920 50
981 51
897 52
820 53
928 54
SQL>create
table cassette(cass_no number(5) primary key,
SQL>desc cassette;
………………………………………………………………………………………..
CASS_NAME VARCHAR2(15)
LANGUAGE VARCHAR2(15)
………………………………………………………………………………………
1 tagore telugu
3 anniyan tamil
4 indra telugu
SQL>desc issu_rec;
………………………………………………………………………………………………………...
ISS_DATE DATE
MEM_NO NUMBER(5)
CASS_NO NUMBER(5)
……………………………………………………………………………………
22 07-JAN-06 920 1
23 10-JAN-00 981 2
26 10-JAN-06 897 5
3 01-JAN-06 820 4
34 31-DEC-05 928 3
CUSTNAME MEMNO
……………….. ………………..
NIKHIL 51
VIVEK 52
SHRAVAN 58
VAMSI 57
SHIVA 56
d) List all the issues for the current date with the customer names and
cassette names
OutPut:
no rows selected.
STUD_NAM VARCHAR2(20)
CLASS VARCHAR2(20)
39 LEON CSE
34 VIKAS CSIT
18 MATHEW ECE
8 HANSEN MECH
24 ALEXIS EEE
CLASS VARCHAR2(10)
DESCRIPT VARCHAR2(20)
LAB_NO NUMBER(5)
DESCRIPTION VARCHAR2(20)
23 7 physics
78 2 chemistry
87 1 edc
12 10 cds
8 3 java lab
Doweek varchar2(20));
-------------- ----------------
STUD_NO NUMBER(5)
MACH_NO NUMBER(5)
DOWEEK VARCHAR2(20)
39 23 sat
34 87 mon
18 78 tue
8 12 wed
24 12 thu
c) List all the machine allotments with the student names, lab and machine
numbers
STUDNAME MACHNO
………………………………………..
ABHIJEET 1
KALYAN 22
ASHWIN 3
ARKA 4
ARVIND 5
……………………………………………………………………
1 UNIX MONDAY
22 UNIX TUESDAY
3 XP WEDNESDAY
4 WINDOWS THRUSDAY
5 ME FRIDAY
e) Give a count of how many machines have been allocated to the ‘CSIT’
class
COUNT (MACHNO)
……………………..
f) Give a machine allotment etails of the stud_no 5 with his personal and
class details
………………………………………………………………………………………………………
g) Count for how many machines have been allocated in Lab_no 1 for the
day of the week as “Monday”
h) How many students class wise have allocated machines in the labs
……………………………………………………………………………
2 CSE
1 ECE
1 EEE
1 IT
i) Create a view which lists out the stud_no, stud_name, mach_no, lab_no,
dayofweek
j) Create a view which lists the machine allotment details for “Thursday”
Algorithm:
Declare
A number;
B number;
C number;
Begin
A:=&a;
B:=&b;
C:=&c;
Else
Else
End if;
End if;
End;
OUTPUT:
C is big
Output:
AIM: To generate first 10 natural numbers using loop, while and for.
Algorithm:
Declare
I number;
Begin
I:=1;
Loop
Dbms_output.put_line(I);
I:=I+1;
End loop;
End;
/* using while */
Declare
I number;
Begin
I:=1;
While (I<=10)
loop
Dbms_output.put_line(I);
I:=I+1;
End loop;
End;
Algorithm:
Step 1: Declare the variable I.
Step 2: Store the value 1 in var. I.
Step 3: Use For… LOOP statement
Step 4: Display the first value of I.
Step 5: Increment the value of I by 1 value.
Step 6: check the value up to 10 no. and repeat the loop
Step 7: if the loop exceeds the value 10 then the loop will be
terminated.
Begin
For I in 1..10
loop
Dbms_output.put_line(I);
End loop;
End;
OUTPUT
1
2
3
4
5
6
7
8
9
10
Algorithm:
Declare
N number;
S number;
D number;
Begin
N:=&n;
S:=0;
While(n!=0)
Loop
D=n%10;
S:=s+(D*D*D);
N:=floor(n/10);
End loop;
If (DUP=S) then
DBMS_output.put_line(‘number is armstrong’);
Else
DBMS_output.put_line(‘number is not armstrong’);
End if;
End;
Enter value of n
153
OUTPUT:
number is Armstrong.
Declare
I number;
J number;
C number;
Begin
While(i<=100)
Loop
C:=0;
J:=1;
While(j<=i)
Loop
If(floor(i%j)=0) then
C:= C+1;
End if;
J:=j+1;
End loop;
If(c=2) then
Dbms_output.put_line(i);
End if;
Endloop;
End;
OUTPUT:
11
99
Declare
I number;
Begin
I:=1;
If(i>=0) then
GOTO here;
Else
Dbms_output.put_line( ‘ I is negative’);
End if;
<<here>>
Dbms_output.put_line( ‘ I is positive’);
End;
OUTPUT:
I is positive
Declare
My_Empno emp.empno%type;
My_Ename emp.ename%type;
My_Emprow emp%rowtype;
No number;
Begin
No:=&no;
Select empno,ename into my_empno,my_ename from emp where
empno=no;
If(SQl%rowcount=1) then
Dbms_output.put_line(‘empno is’ || my_empno || ‘ename is ‘ ||
my_ename);
Else
Dbms_output.put_line( ‘error’);
End if;
Select * into my_emprow from emp where empno=no;
If(SQl%rowcount=1) then
Dbms_output.put_line(‘empno is’ || my_emprow.empno || ‘ename is
‘ || my_emprow.ename);
Else
Dbms_output.put_line( ‘error’);
End if;
End;
7788
OUTPUT
Declare
A number
B number;
C number;
Begin
A:=&a;
B:=&b;
C:=a/b;
Dbms_output.put_line(‘division is ‘ || C);
Exception
If (ZERO_DIVIDE) then
Dbms_output.put_line(‘b could not be zero’);
End if;
End;
10
OUTPUT:
10
OUTPUT:
Declare
Cursor my_cur is select empno,sal from emp;
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open my_cur;
C:=0;
Loop
Fetch my_cur into xno,xsal;
If(xsal<1000) then
Update emp set sal=3000 where empno=xno;
C:=c+1;
Else if(xsal>=2000) and xsa<3000) then
Update emp set sal=4000 where empno=xno;
C:=c+1;
End if;
End if;
Exit when my_cur%NOTFOUND ;
End loop;
Close my_cur;
Dbma_output.put_line(c||’records have been successfully updated’);
End;
Sql>@a.sql;
records have been successfully updated
pl/sql procedure successfully completed.
OUTPUT:
-----------------------------------------------------------------
14 rows selected.
i number;
c number;
j number;
Begin
i:=1;
tot:=0;
while(i<=n)
loop
j:=1;
c:=0;
while(j<=i)
loop
if(mod(I,j)=0) then
c:=c+1;
end if;
j:=j+1;
end loop;
if(c=2) then
dbms_output.put_line(i);
tot:=tot+1;
end if;
i:=i+1;
end loop;
end;
/
Sql>procedure created.
declare
t number;
begin
prime_proc(10,t);
dbms_output.put_line(‘the total prime no .are’||t);
end;
sql>set serveroutput on
OUTPUT
sql>/
2
3
5
7
The total prime no.are 4
Pl/sql procedure successfully completed.
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open my_cur;
C:=0;
Loop
Fetch my_cur into xno,xsal;
If(xsal<1000) then
Update emp set sal=1500 where empno=xno;
C:=c+1;
Else
Is(xsal>=1000 and xsal<=2400) then
Update emp set sal=2500 where empno=xno;
C:=c+1;
End if;
End if;
Exit when my_cur%NOTFOUND;
End loop;
Close my_cur;
Dbms_output.put_line(c||’records have been successfully updated’);
End;
/
Procedure created.
Sql>exec myproc;
OUTPUT:
Number as
C number;
Begin
C:=a+b;
Return c;
End;
/
Function created.
/*add_fun specification*/
Declare
Result number;
Begin
Result:=add_fun(10,20);
Dbms_output.put_line(‘the sum of 10 and 20 is’||result);
End;
Sql>/
The sum of 10 and 20 is 30
Pl/sql procedure successfully completed.
c:=c+1;
end if;
exit when vin_cur%notfound;
end loop;
close vin_cur;
return c;
end;
/
Function created.
/*function specification*/
Declare
Ne number;
Xsal number;
Begin
Ne:=count_emp(xsal);
Dbms_output.put_line(xsal);
Dbma_output.put_line(‘there are ‘||ne||;employees’);
End;
/
OUTPUT