DBMS Lab Manual
DBMS Lab Manual
Faculty of Science
DBMS Lab
LABORATORY MANUAL
Prepared By Approved By
Syllabus Programs
c) List all the bills for the current date with the customer names and item numbers
d) List the total Bill details with the quantity sold, price of the item and the final
amount
e) List the details of the customer who have bought a product which has a
price>200
f) Give a count of how many products have been bought by each customer
i) Create a view which lists out the bill_no, bill_date, cust_id, item_id, price,
qty_sold, amount
Create a view which lists the daily sales date wise for the last one week
Additional Programs
Background Theory
Oracle workgroup or server is the largest selling RDBMS product.it is estimated that the combined sales of
both these oracle database product account for aroud 80% of the RDBMSsystems sold worldwide.
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:
Create table <table name> (<col1> <datatype>(<size>),<col2> <datatype><size>));
Ex:
create table emp(empno number(4) primary key, ename char(10));
Syntax:
Alter table <tablename> add(<new col><datatype(size),<new col>datatype(size));
Ex:
alter table emp add(sal number(7,2));
Syntax:
Alter table <tablename> drop column <col>;
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Ex:
alter table emp drop column sal;
Syntax:
Alter table <tablename> modify(<col><newdatatype>(<newsize>));
Ex:
alter table emp modify(ename varchar2(15));
Syntax:
Rename <oldtable> to <new table>;
Ex:
rename emp to emp1;
Syntax:
Truncate table <tablename>;
Ex:
trunc table emp1;
7. Destroying tables.
Syntax:
Drop table <tablename>;
Ex:
drop table emp;
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:
insert into <tablename> (<col1>,<col2>) values(<exp>,<exp>);
9. Delete operations.
Syntax:
delete from <tablename> where <condition>;
b) unique constraint
Syntax:
Unique constraint at column level.
<col><datatype>(size)unique;
h) check constraint
check constraint constraint at column level.
Syntax: <col><datatype>(size) check(<logical expression>)
7
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
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.
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.
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.
The grant statement provides various types of access to database objects such as tables,views and sequences
and so on.
Syntax:
GRANT <object privileges>
ON <objectname>
TO<username>
[WITH GRANT OPTION];
Syntax:
REVOKE<object privilege>
8
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
ON
FROM<user name>;
9
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
1
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Aim: Create the tables with the appropriate integrity constraints and Insert around 10 records in each of the
tables
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : ORACLE
SQL> create table customer1 (cust_id number(5) primary key, cust_name varchar2(15));
Output: Table created.
Output:
Name Null? Type
----------------------------------------- -------- ----------------
CUST_ID NOT NULL NUMBER(5)
CUST_NAME VARCHAR2(15)
Output:
Name Null? Type
……………………………………………………………………………………………………
Cust_id NOT NULL NUMBER(4)
Item_name VARCHAR2(15)
PRICE NUMBER(6,2)
SQL>insert into item values(&item_id,’&item_name’,&price);
1
1
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
SQL>create table sale(bill_no number(5) primary key,bill_date date, cust_id number(5) references
customer(cust_id), item_id number(4) references item(item_id),qty_sold number(4));
SQL>dsec sale
Output:
Name Null? Type
………………………………………………………………………………………..
BILL_NO NOT NULL NUMBER(4)
BILL_DATE DATE
CUST_ID NUMBER(5)
ITEM_ID NUMBER(4)
QTY_SOLD NUMBER(4)
c) List all the bills for the current date with the customer names and item numbers
SQL> select c.custname, i.itemid, s.billno from customer c, item I, sale s
where c.custid=s.custid and
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;
10 1 10
350 4 1400
e) List the details of the customer who have bought a product which has a price>200
SQL> select c.custid, c.custname from customer c, sale s, item i where i.price>200 and
c.custid=s.custid and i.itemid=s.itemid;
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
ITEMNAME
--------------
Pens
ITEMID ITEMNAME
--------- -------------
1234 pencil
i) Create a view which lists out the bill_no, bill_date, cust_id, item_id, price, qty_sold, amount
SQL>create view cust as (select s.billno, s.billdate, c.custid, i. iitemid, i.price, s.qty from customer
c,sale s item I where c.custid=s.custid and i.iemid=s.itemid);
view created.
j) Create a view which lists the daily sales date wise for the last one week
Viva-Voce:
2. What is database?
A database is a logically coherent collection of data with some inherent meaning, representing
some aspect of real world and which is designed, built and populated with data for a specific
purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other words it is
general-purpose software that provides the users with the processes of defining, constructing and
manipulating the database for various applications.
5. Advantages of DBMS?
Redundancy is controlled.
Unauthorised access is restricted.
Providing multiple user interfaces.
Enforcing integrity constraints.
Providing backup and recovery.
1
4
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
1
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle
SQL>desc student;
Name Null? Type
………………………………………………………………………………………..
STUD_NO NOT NULL NUMBER(5)
STUD_NAME VARCAHR2(15)
Valid Test Data:
STUD_NO STUD_NAME
....................................................................
508 HARISH
513 BALAJI
518 RAKESH
524 PAVAN
534 JOYCE
ERROR at line 1:
1
6
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
SQL>desc book;
SQL>desc iss_rec;
Name Null? Type
………………………………………………………………………………………………………
ISS_NO NOT NULL NUMBER
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
SQL> select i.issno, s.studname, b.bookname from iss_rec I, membership m, student s, book b
2 where i.memno=m.memno and m.studno=s.studno and
i.issdate=to_char(sysdate);
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
SQL> select s.studno, count(i.bookno) from student s.membership m, book b, 2 iss_rec I where
s.studno=m.studno and b.bookno=i.bookno group by s.studno;
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
2 memno in(select memno from membership where
3 studno in(select studno from student where studno=5)));
BOOKNAME
-------------
NT
1
8
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
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:
ORA-02292: integrity constraint (SCOTT.SYS_C00840) violated – child record found
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
Viva-Vice:
5. How is the data structure of System R different from the relational structure?
1
9
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
2
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle
SQL>desc employee;
EMP_ID EMP_NAME
………………………………………………………….
10 Robert
21 Coulthard
30 Fernando Alonso
39 Kartikeyan
87 Kimmi
SQL>desc department;
Name Null? Type
………………………………………………………………………………………..
DEPT_ID NOT NULL NUMBER(5)
DEPT_NAME VARCHAR2(20)
DEPT_ID DEPT_NAME
……………………………………………………………………………..
100 sales
101 accounts
102 administration
103 production
104 supervisor
SQL>desc paydetails;
EMPID DEPTID
…………………………
401 500
402 200
2
2
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
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 aaaaaaaa
EMPID EMPNAME
…………………………….
AKHILA
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
i) Create a view which lists out the emp_name, department, basic, dedeuctions, netsalary
Viva-Vice:
13. What is Data Model?
A collection of conceptual tools for describing data, data relationships data semantics and
constraints.
2
4
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
2
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle
SQL>desc customer;
CUST_NO CUST_NAME
……………………………………………………………….
50 scott
51 pandey
52 varshney
53 naidu
54 bhimbra
SQL>dsec membership;
Name Null? Type
………………………………………………………………………………………………………...
MEM_NO NOT NULL NUMBER(5)
CUST_NO NUMBER(5)
MEM_NO CUST_NO
…………………………………………………
920 50
981 51
897 52
820 53
928 54
2
6
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
SQL>desc cassette;
SQL>desc issu_rec;
Name Null? Type
………………………………………………………………………………………………………...
ISS_NO NOT NULL NUMBER(5)
ISS_DATE DATE
MEM_NO NUMBER(5)
CASS_NO NUMBER(5)
CUSTNAME MEMNO
……………….. ………………..
NIKHIL 51
VIVEK 52
SHRAVAN 58
2
7
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
VAMSI 57
SHIVA 56
d) List all the issues for the current date with the customer names and cassette names
OutPut:
no rows selected.
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
Viva-Vice:
2
8
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
2
9
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle
CLASS VARCHAR2(10)
DESCRIPT VARCHAR2(20)
3
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
SQL> create table allotment(stud_no number(5) references stu(stud_no), match_no number(5) references
lab(mach_no),
Doweek varchar2(20));
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
SQL>select count(machno)from allotment where studno in(select studno from student1 where
class=’CSIT’);
COUNT (MACHNO)
……………………..
1
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 allocatedin Lab_no 1 for the day of the week as
“Monday”
h) How many students class wise have allocated machines in the labs
SQL>select count(studno) “allocated students in the labs”,class from student1 where studno in(select
studno from allotment) group by class;
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”
Viva-Vice:
Relationship type defines a set of associations or a relationship set among a given set of entity
types.
3
3
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
6). Write a program to find largest number from the given three numbers.
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
Algorithm:
Declare
A number;
B number;
C number;
Begin
A:=&a;
B:=&b;
C:=&c;
Else
3
4
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Else
End if;
End if;
End;
OUTPUT:
C is big
Output:
Viva-Vice:
3
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
3
6
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
7). Simple programs using loop, while and for iterative control statement.
AIM: To generate first 10 natural numbers using loop, while and for.
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
Algorithm:
Declare
I number;
Begin
I:=1;
Loop
Dbms_output.put_line(I);
I:=I+1;
End loop;
End;
3
7
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
/* using while */
Declare
I number;
Begin
I:=1;
While (I<=10)
loop
Dbms_output.put_line(I);
I:=I+1;
End loop;
End;
3
8
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
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
3
9
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Viva-Vice:
38. How does Tuple-oriented relational calculus differ from domain-oriented relational calculus
The tuple-oriented calculus uses a tuple variables i.e., variable whose only permitted values
are tuples of that relation. E.g. QUEL
The domain-oriented calculus has domain variables i.e., variables that range over the underlying
domains instead of over relation. E.g. ILL, DEDUCE.
4
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, Pl-SQL
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;
4
1
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
If (DUP=S) then
DBMS_output.put_line(‘number is armstrong’);
Else
End if;
End;
Enter value of n
153
OUTPUT:
number is Armstrong.
Viva-Vice:
X is a super key.
4
3
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
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;
4
4
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Endloop;
End;
OUTPUT:
11
99
Viva-Vice:
52. What are partial, alternate,, artificial, compound and natural key?
Partial Key:
It is a set of attributes that can uniquely identify weak entities and that are related to
same owner entity. It is sometime called as Discriminator.
Alternate Key:
All Candidate Keys excluding the Primary Key are known as Alternate Keys.
Artificial Key:
If no obvious key, either stand alone or compound is available, then the last resort
is to simply create a key, by assigning a unique number to each record or occurrence. Then this is
known as developing an artificial key.
Compound Key:
If no single data element uniquely identifies occurrences within a construct, then
combining multiple elements to create a unique identifier for the construct is known as creating a
compound key.
4
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Natural Key:
When one of the data elements stored within a construct is utilized as the primary
key, then it is called the natural key.
53. What is indexing and what are the different kinds of indexing?
Indexing is a technique for determining how quickly specific data can be found.
Types:
Binary search style indexing
B-Tree indexing
Inverted list indexing
Memory resident table
Table indexing
4
6
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
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
Viva-Vice:
54. What is system catalog or catalog relation? How is better known as?
4
7
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
A RDBMS maintains a description of all the data that it contains, information about every
relation and index that it contains. This information is stored in a collection of relations maintained
by the system called metadata. It is also called data dictionary.
4
8
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
Declare
My_Empno emp.empno%type;
My_Ename emp.ename%type;
My_Emprow emp%rowtype;
No number;
Begin
No:=&no;
If(SQl%rowcount=1) then
Else
Dbms_output.put_line( ‘error’);
End if;
If(SQl%rowcount=1) then
Else
Dbms_output.put_line( ‘error’);
End if;
End;
4
9
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
7788
OUTPUT
Viva-Vice
67. What are the primitive operations common to all record management systems?
5
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
5
1
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
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
End if;
End;
10
0
5
2
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
OUTPUT:
Viva-Vice:
68. Name the buffer in which all the commands that are typed in are stored
‘Edit’ Buffer
70. Are the resulting relations of PRODUCT and JOIN operation the same?
No.
PRODUCT: Concatenation of every row in one relation with every row in another.
JOIN: Concatenation of rows from one relation and related rows from another.
73. Which part of the RDBMS takes care of the data dictionary? How
Data dictionary is a set of tables and database objects that is stored in a special area of the
database and maintained exclusively by the kernel.
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
5
3
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Hard Disk : 40 GB
Software : Oracle, PlSQL
Declare
A number
B number;
C number;
Mydivide_zero EXCEPTION;
Begin
A:=&a;
B:=&b;
If(B=0) then
Raise Mydivide_zero;
else
C:=a/b;
Dbms_output.put_line(‘division is ‘ || C);
End if;
Exception
If (mydivide_zero) then
End if;
End;
10
OUTPUT:
Viva-Vice:
77. Define SQL and state the differences between SQL and other conventional programming
Languages
SQL is a nonprocedural language that is designed specifically for data access operations on
normalized relational database structures. The primary difference between SQL and other
conventional programming languages is that SQL statements specify what data operations should
be performed rather than how to perform them.
78. Name the three major set of files on disk that compose a database in Oracle
There are three major sets of files on disk that compose a database. All the files are binary.
These are
Database files
Control files
Redo logs
The most important of these are the database files where the actual data resides. The control
files and the redo logs support the functioning of the architecture itself.
All three sets of files must be present, open, and available to Oracle for any data on the
database to be useable. Without these files, you cannot access the database, and the database
administrator might have to recover some or all of the database using a backup, if there is one.
80. What are the four Oracle system processes that must always be up and running for the
database to be useable
The four Oracle system processes that must always be up and running for the database to be useable
include DBWR (Database Writer), LGWR (Log Writer), SMON (System Monitor), and PMON (Process
Monitor).
5
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
5
6
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
Declare
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open my_cur;
C:=0;
Loop
If(xsal<1000) then
C:=c+1;
C:=c+1;
5
7
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
End if;
End if;
End loop;
Close my_cur;
End;
Sql>@a.sql;
OUTPUT:
-----------------------------------------------------------------
5
8
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
….
14 rows selected.
Viva-Vice:
81. What are database files, control files and log files. How many of these files should a database
have at least? Why?
Database Files
The database files hold the actual data and are typically the largest in size. Depending on
their sizes, the tables (and other objects) for all the user accounts can go in one database file—but that's not
an ideal situation because it does not make the database structure very flexible for controlling access to
storage for different users, putting the database on different disk drives, or backing up and restoring just part
of the database.
You must have at least one database file but usually, more than one files
are used. In terms of accessing and using the data in the tables and other objects, the
number (or location) of the files is immaterial.
The database files are fixed in size and never grow bigger than the size at which they were
created
Control Files
The control files and redo logs support the rest of the architecture. Any database
must have at least one control file, although you typically have more than one to guard against
loss. The control file records the name of the database, the date and time it was created, the
location of the database and redo logs, and the synchronization information to ensure that all
three sets of files are always in step. Every time you add a new database or redo log file to the
Redo Logs
Any database must have at least two redo logs. These are the journals for the database; the
redo logs record all changes to the user objects or system objects. If any type of failure occurs, the changes
recorded in the redo logs can be used to bring the database to a consistent state without losing any
committed transactions. In the case of non-data loss failure, Oracle can apply the information in the redo
logs automatically without intervention from the DBA.
The redo log files are fixed in size and never grow dynamically from the size at which they
were created.
The ROWID consists of the following three components, the combination of which uniquely
identifies the physical storage location of the row.
Oracle database file number, which contains the block with the rows
Oracle block address, which contains the row
The row within the block (because each block can hold many rows)
The ROWID is used internally in indexes as a quick means of retrieving rows with a particular key
value. Application developers also use it in SQL statements as a quick way to access a row once they know
the ROWID
83. What is Oracle Block? Can two Oracle Blocks have the same address?
Oracle "formats" the database files into a number of Oracle blocks when they are first created—
making it easier for the RDBMS software to manage the files and easier to read data into the memory areas.
The block size should be a multiple of the operating system block size. Regardless of the
block size, the entire block is not available for holding data; Oracle takes up some space to manage
the contents of the block. This block header has a minimum size, but it can grow.
These Oracle blocks are the smallest unit of storage. Increasing the Oracle block size can improve
performance, but it should be done only when the database is first created.
Each Oracle block is numbered sequentially for each database file starting at 1. Two blocks can
have the same block address if they are in different database files.
85. Name two utilities that Oracle provides, which are use for backup and recovery.
Along with the RDBMS software, Oracle provides two utilities that you can use to back up and
restore the database. These utilities are Export and Import.
The Export utility dumps the definitions and data for the specified part of the database to an
operating system binary file. The Import utility reads the file produced by an export, recreates the definitions
of objects, and inserts the data
If Export and Import are used as a means of backing up and recovering the database, all the changes
made to the database cannot be recovered since the export was performed. The best you can do is recover
the database to the time when the export was last performed.
6
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
19. create a procedure which generate all the prime numbers below the given number
and count the no.of prime numbers.
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
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;
6
1
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
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);
end;
sql>set serveroutput on
OUTPUT
sql>/
6
2
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Viva-Vice
86. Name two utilities that Oracle provides, which are use for backup and recovery.
Along with the RDBMS software, Oracle provides two utilities that you can use to back up and
restore the database. These utilities are Export and Import.
The Export utility dumps the definitions and data for the specified part of the database to an
operating system binary file. The Import utility reads the file produced by an export, recreates the definitions
of objects, and inserts the data
If Export and Import are used as a means of backing up and recovering the database, all the changes
made to the database cannot be recovered since the export was performed. The best you can do is recover
the database to the time when the export was last performed.
87. What are stored-procedures? And what are the advantages of using them.
Stored procedures are database objects that perform a user defined operation. A stored
procedure can have a set of compound SQL statements. A stored procedure executes the SQL
commands and returns the result to the client. Stored procedures are used to reduce network traffic.
(a) i & iii because theta joins are joins made on keys that are not primary keys.
90. A B C is a set of attributes. The functional dependency is as follows
AB -> B
AC -> C
C -> B
a) is in 1NF
b) is in 2NF
c) is in 3NF
d) is in BCNF
6
3
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
HW/SW requirements:
Processor : AMD Athelon ™ 1.67 GHz
RAM : 256 MB
Hard Disk : 40 GB
Software : Oracle, PlSQL
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open my_cur;
C:=0;
Loop
If(xsal<1000) then
C:=c+1;
Else
C:=c+1;
End if;
6
4
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
End if;
End loop;
Close my_cur;
End;
Procedure created.
Sql>exec myproc;
OUTPUT:
Number as
C number;
Begin
C:=a+b;
Return c;
End;
Function created.
6
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
/*add_fun specification*/
Declare
Result number;
Begin
Result:=add_fun(10,20);
End;
Sql>/
/*create a function which count total no.of employees having salary less than 6000.*/
/*function body*/
Xno emp.empno%type;
Xsal emp.sal%type;
C number;
Begin
Open vin_cur;
C:=0;
loop
if(xsal<esal) then
6
6
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
c:=c+1;
end if;
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);
End;
OUTPUT
Viva-Vice:
93. Select 'NORTH', CUSTOMER From CUST_DTLS Where REGION = 'N' Order By
CUSTOMER Union Select 'EAST', CUSTOMER From CUST_DTLS Where REGION = 'E'
Order By CUSTOMER
The above is
a) Not an error
b) Error - the string in single quotes 'NORTH' and 'SOUTH'
c) Error - the string should be in double quotes
d) Error - ORDER BY clause
(d) Error - the ORDER BY clause. Since ORDER BY clause cannot be used in UNIONS
PL/SQL uses cursors for all database information accesses statements. The language supports
the use two types of cursors
Implicit
Explicit
101. What is cold backup and hot backup (in case of Oracle)?
Cold Backup:
It is copying the three sets of files (database files, redo logs, and control file) when the
instance is shut down. This is a straight file copy, usually from the disk directly to tape. You must shut down
the instance to guarantee a consistent copy.
If a cold backup is performed, the only option available in the event of data file loss is
restoring all the files from the latest backup. All work performed on the database since the last backup is
lost.
Hot Backup:
Some sites (such as worldwide airline reservations systems) cannot shut down the
database while making a backup copy of the files. The cold backup is not an available option.
So different means of backing up database must be used — the hot backup. Issue a SQL
command to indicate to Oracle, on a tablespace-by-tablespace basis, that the files of the tablespace are to
backed up. The users can continue to make full use of the files, including making changes to the data. Once
the user has indicated that he/she wants to back up the tablespace files, he/she can use the operating system
to copy those files to the desired backup destination.
The database must be running in ARCHIVELOG mode for the hot backup option.
If a data loss failure does occur, the lost database files can be restored using the hot
backup and the online and offline redo logs created since the backup was done. The database is restored to
the most consistent state without any loss of committed transactions.
102. What are Armstrong rules? How do we say that they are complete and/or sound
The well-known inference rules for FDs
Reflexive rule :
If Y is subset or equal to X then X Y.
Augmentation rule:
If X Y then XZ YZ.
Transitive rule:
If {X Y, Y Z} then X Z.
Decomposition rule :
If X YZ then X Y.
Union or Additive rule:
If {X Y, X Z} then X YZ.
Pseudo Transitive rule :
If {X Y, WY Z} then WX Z.
Of these the first three are known as Amstrong Rules. They are sound because it is enough if a set of
FDs satisfy these three. They are called complete because using these three rules we can generate the rest all
inference rules.
103. How can you find the minimal key of relational schema?
Minimal key is one which can identify each tuple of the given relation schema uniquely.
For finding the minimal key it is required to find the closure that is the set of all attributes that are
dependent on any given set of attributes under the given set of functional dependency.
Algo. I Determining X+, closure for X, given set of FDs F
1. Set X+ = X
2. Set Old X+ = X+
3. For each FD Y Z in F and if Y belongs to X+ then add Z to X+
6
9
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
7
0
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
SQL Questions:
1. Which is the subset of SQL commands used to manipulate Oracle Database structures, including tables?
Data Definition Language (DDL)
5. What is the parameter substitution symbol used with INSERT INTO command?
&
6. Which command displays the SQL command in the SQL buffer, and then executes it?
RUN
9. State true or false. !=, <>, ^= all denote the same operation.
True
10. What are the privileges that can be granted on a table by a user to others?
Insert, update, delete, select, references, index, execute, alter, all
11. What command is used to get back the privileges offered by the GRANT command?
REVOKE
12. Which system tables contain information on privileges granted and privileges obtained?
USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
13. Which system table contains information on constraints on all the tables created?
USER_CONSTRAINTS
7
1
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
16. What command is used to create a table by copying the structure of another table?
Answer :
CREATE TABLE .. AS SELECT command
Explanation :
To copy only the structure, the WHERE clause of the SELECT command should contain a FALSE
statement as in the following.
CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;
If the WHERE condition is true, then all the rows or rows satisfying the condition will be copied to
the new table.
20. Which date function is used to find the difference between two dates?
MONTHS_BETWEEN
22. What is the advantage of specifying WITH GRANT OPTION in the GRANT command?
The privilege receiver can further grant the privileges he/she has obtained from the owner to any
other user.
23. What is the use of the DROP option in the ALTER TABLE command?
It is used to drop constraints specified on the table.
24. What is the value of ‘comm’ and ‘sal’ after executing the following query if the initial value of ‘sal’ is
10000?
UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;
sal = 11000, comm = 1000
The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output sorted on
ENAME in descending order.
27. Which function is used to find the largest integer less than or equal to a specific value?
FLOOR
7
3
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
REFERENCES:
<Include the syllabus copy of respective subject after the title page.>
7
4
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
Index
Sr. Page
Name of Experiment/Assignment
No. No.
7
5
Name of Subject (DBMS Lab – MCA/BCA) Laboratory Manual
7
6