Dbms Lab Programs
Dbms Lab Programs
V SEMESTER
LAB MANUAL
Page 1
DBMS Lab Manual 17CSL58
PART-A:
SQL Programming (Max. Exam Mks. 50)
• Design, develop, and implement the specified queries for the following problems using Oracle, MySQL,
MS SQL Server, or any other DBMS under LINUX/Windows environment.
• Create Schema and insert at least 5 records for each table. Add appropriate database constraints.
PART-B:
Mini Project (Max. Exam Mks. 30)
• Use Java, C#, PHP, Python, or any other similar front-end tool.
• All applications must be demonstrated on desktop/laptop as a stand-alone or web based application
(Mobile apps on Android/IOS are not permitted.)
Solution:
Page 2
DBMS Lab Manual 17CSL58
Entity-Relationship Diagram
Author_Name
Book_id Title
Pub_Year M N
Has
Published-by
N No_of_copies
Branch_id
Publisher_Name
M M
1 Book_Copies In
Branch_Name
Address
Publisher
N Address
Date_out
Book_Lending
Phone
Card_No
Due_date
N
Card
N Library
_Branc
h
Page 3
DBMS Lab Manual 17CSL58
Schema Diagram
Book
Book_Authors
Book_id Author_nam
e
Publisher
Book_Copies
Book_Lending
Library_Branch
Table Creation
CREATE TABLEBOOK
(BOOK_ID INTEGER PRIMARYKEY,
TITLE VARCHAR2(20),
PUB_YEAR VARCHAR2 (20),
PUBLISHER_NAME REFERENCES PUBLISHER (NAME) ON DELETE CASCADE);
Page 4
DBMS Lab Manual 17CSL58
Table Descriptions
DESC PUBLISHER;
DESC BOOK;
Page 5
DBMS Lab Manual 17CSL58
DESC BOOK_AUTHORS;
DESC LIBRARY_BRANCH;
DESC BOOK_COPIES;
DESC CARD;
DESC BOOK_LENDING;
Page 6
DBMS Lab Manual 17CSL58
Page 7
DBMS Lab Manual 17CSL58
Page 8
DBMS Lab Manual 17CSL58
Queries:
1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each branch,etc.
Page 9
DBMS Lab Manual 17CSL58
1. Get the particulars of borrowers who have borrowed more than 3 books, but
from Jan 2017 to Jun2017.
2. Delete a book in BOOK table. Update the contents of other tables to reflect this
data manipulationoperation.
3. Partition the BOOK table based on year of publication. Demonstrate its working
with a simplequery.
Page 10
DBMS Lab Manual 17CSL58
4. Create a view of all books and its number of copies that are currently available
in the Library.
Page 11
DBMS Lab Manual 17CSL58
Solution:
Entity-Relationship Diagram
Page 12
DBMS Lab Manual 17CSL58
Schema Diagram
Salesman
Customer
Table Creation
Page 13
DBMS Lab Manual 17CSL58
Table Descriptions
DESC SALESMAN;
DESC CUSTOMER1;
DESC ORDERS;
Page 14
DBMS Lab Manual 17CSL58
INSERTINTOORDERSVALUES(52,1000,‗24-FEB-17‘,13,2000);
INSERTINTOORDERSVALUES(53,3500,‗13-APR-17‘,14,3000);
INSERT INTO ORDERS VALUES (54, 550, ‗09-MAR-17‘, 12, 2000);
Queries:
Page 15
DBMS Lab Manual 17CSL58
2. Find the name and numbers of all salesmen who had more than onecustomer.
3. List all salesmen and indicate those who have and don’t have customers in
their cities (Use UNIONoperation.)
4. Create a view that finds the salesman who has the customer with the
highest order of aday.
Page 16
DBMS Lab Manual 17CSL58
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also bedeleted.
Use ON DELETE CASCADE at the end of foreign key definitions while creating child table
orders and then execute the following:
Use ON DELETE SET NULL at the end of foreign key definitions while creating child table
customers and then executes the following:
Page 17
DBMS Lab Manual 17CSL58
Solution:
Entity-Relationship Diagram
Dir_id Dir_Name
Act_id Act_Name
Dir_Phone
Act_Gender Actor Director
M
Has
Movie_Cast
N
Role
Rev_Stars
N Movies
Mov_Lang
Mov_id
Mov_Title Mov_Year
Page 18
DBMS Lab Manual 17CSL58
Schema Diagram
Actor
Act_id Act_Name Act_Gender
Director
Dir_id Dir_Name Dir_Phone
Movies
Mov_id Mov_Title Mov_Year Mov_Lang Dir_id
Movie_Cast
Act_id Mov_id Role
Rating
Mov_id Rev_Stars
Table Creation
Page 19
DBMS Lab Manual 17CSL58
Table Descriptions
DESC ACTOR;
DESC DIRECTOR;
DESC MOVIES;
Page 20
DBMS Lab Manual 17CSL58
DESC MOVIE_CAST;
DESC RATING;
Page 21
DBMS Lab Manual 17CSL58
Page 22
DBMS Lab Manual 17CSL58
Queries:
SELECT MOV_TITLE
FROM MOVIES
WHERE DIR_ID IN (SELECT DIR_ID
FROM DIRECTOR
WHERE DIR_NAME = ‗HITCHCOCK‘);
2. Find the movie names where one or more actors acted in two or moremovies.
SELECT MOV_TITLE
FROM MOVIES M, MOVIE_CAST MV
WHERE M.MOV_ID=MV.MOV_ID AND ACT_ID IN (SELECT ACT_ID
FROM MOVIE_CAST GROUP BY ACT_ID HAVING
COUNT (ACT_ID)>1)
GROUP BY MOV_TITLE HAVING
COUNT (*)>1;
3. List all actors who acted in a movie before 2000 and also in a movie after
2015 (use JOINoperation).
Page 23
DBMS Lab Manual 17CSL58
4. Find the title of movies and number of stars for each movie that has at least
one rating and find the highest number of stars that movie received. Sort the
result by movie title.
Page 24
DBMS Lab Manual 17CSL58
Page 25
DBMS Lab Manual 17CSL58
Page 26
DBMS Lab Manual 17CSL58
Schema Diagram
Table Creation
Page 27
DBMS Lab Manual 17CSL58
Table Descriptions
DESC STUDENT;
DESC SEMSEC;
Page 28
DBMS Lab Manual 17CSL58
DESC CLASS;
DESC SUBJECT;
DESC IAMARKS;
Page 29
DBMS Lab Manual 17CSL58
INSERTINTOSEMSECVALUES(‗CSE4A‘,4,‘A‘);
INSERT INTO SEMSEC VALUES ('CSE4B', 4,‘B‘);
INSERTINTOSEMSECVALUES(‗CSE4C‘,4,'C‘);
Page 30
DBMS Lab Manual 17CSL58
INSERTINTOCLASSVALUES(‗1RN13CS020‘,‘CSE8A‘);
INSERTINTOCLASSVALUES(‗1RN13CS062‘,‘CSE8A‘);
INSERTINTOCLASSVALUES(‗1RN13CS066‘,‘CSE8B‘);
INSERTINTOCLASSVALUES(‗1RN13CS091‘,‘CSE8C‘);
INSERTINTOCLASSVALUES(‗1RN14CS010‘,‘CSE7A‘);
INSERTINTOCLASSVALUES(‗1RN14CS025‘,‘CSE7A‘);
INSERTINTOCLASSVALUES(‗1RN14CS032‘,‘CSE7A‘);
INSERTINTOCLASSVALUES(‗1RN15CS011‘,‘CSE4A‘);
INSERTINTOCLASSVALUES(‗1RN15CS029‘,‘CSE4A‘);
INSERTINTOCLASSVALUES(‗1RN15CS045‘,‘CSE4B‘);
INSERTINTOCLASSVALUES(‗1RN15CS091‘,‘CSE4C‘);
INSERTINTOCLASSVALUES(‗1RN16CS045‘,‘CSE3A‘);
INSERTINTOCLASSVALUES(‗1RN16CS088‘,‘CSE3B‘);
INSERTINTOCLASSVALUES(‗1RN16CS122‘,‘CSE3C‘);
Page 31
DBMS Lab Manual 17CSL58
Page 32
DBMS Lab Manual 17CSL58
Page 33
DBMS Lab Manual 17CSL58
Queries:
Page 34
DBMS Lab Manual 17CSL58
SS.SEc=‘C‘;
2. Compute the total number of male and female students in each semester
and in each section.
Page 35
DBMS Lab Manual 17CSL58
4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table for allstudents.
CREATE OR REPLACE PROCEDURE AVGMARKS IS
CURSOR C_IAMARKS IS
SELECT GREATEST(TEST1,TEST2) AS A, GREATEST(TEST1,TEST3) AS B,
GREATEST(TEST3,TEST2) ASC
FROM IAMARKS
WHERE FINALIA IS NULL
FOR UPDATE;
C_ANUMBER;
C_BNUMBER;
C_CNUMBER;
C_SMNUMBER;
C_AVNUMBER;
BEGIN
OPEN C_IAMARKS;
LOOP
FETCH C_IAMARKS INTO C_A, C_B, C_C;
EXIT WHEN C_IAMARKS%NOTFOUND;
--DBMS_OUTPUT.PUT_LINE(C_A || ' ' || C_B || ' ' ||
C_C); IF (C_A != C_B) THEN
C_SM:=C_A+C_B;
ELSE
C_SM:=C_A+C_C;
END IF;
C_AV:=C_SM/2;
--DBMS_OUTPUT.PUT_LINE('SUM = '||C_SM);
--DBMS_OUTPUT.PUT_LINE('AVERAGE = '||C_AV);
UPDATE IAMARKS SET FINALIA=C_AV WHERE CURRENT OF C_IAMARKS;
END LOOP;
CLOSE C_IAMARKS;
END;
/
Note: Before execution of PL/SQL procedure, IAMARKS table contents are:
Page 36
DBMS Lab Manual 17CSL58
Below SQL code is to invoke the PL/SQL stored procedure from the command line:
BEGIN
AVGMARKS;E
ND;
SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,
(CASE
WHEN IA.FINALIA BETWEEN 17 AND 20 THEN'OUTSTANDING' WHEN
IA.FINALIA BETWEEN 12 AND 16 THEN 'AVERAGE' ELSE 'WEAK'
END) AS CAT
FROM STUDENT S, SEMSEC SS, IAMARKS IA, SUBJECT SUB
WHERE S.USN = IA.USN AND
SS.SSID = IA.SSID AND
SUB.SUBCODE = IA.SUBCODE AND
SUB.SEM = 8;
Page 37
DBMS Lab Manual 17CSL58
Page 38
DBMS Lab Manual 17CSL58
Entity-Relationship Diagram
SSN Controlled_by
Name N 1
DNO
Salary
DName
1 N
MgrStartDate
1
Sex 1
N
M Dlocation
Supervisee
Supervisor
Supervision Works_on Controls
N
Hours
Project PName
PNO PLocation
Page 39
DBMS Lab Manual 17CSL58
Schema Diagram
Employee
SSN Fname Lname Address Sex Salary SuperSSN DNO
Depa tment
r
DLocation
DNO DLOC
Project
Works_on
Table Creation
Page 40
DBMS Lab Manual 17CSL58
NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter department
table to add foreign constraint MGRSSN using sql command
Table Descriptions
DESC EMPLOYEE;
Page 41
DBMS Lab Manual 17CSL58
DESC DEPARTMENT;
DESC DLOCATION;
DESC PROJECT;
DESC WORKS_ON;
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSECE01‘,‘JOHN‘,‘SCOTT‘,‘BANGALORE‘,‘M‘, 450000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSCSE01‘,‘JAMES‘,‘SMITH‘,‘BANGALORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSCSE02‘,‘HEARN‘,‘BAKER‘,‘BANGALORE‘,‘M‘, 700000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSCSE03‘,‘EDWARD‘,‘SCOTT‘,‘MYSORE‘,‘M‘, 500000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSCSE04‘,‘PAVAN‘,‘HEGDE‘,‘MANGALORE‘,‘M‘, 650000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSCSE05‘,‘GIRISH‘,‘MALYA‘,‘MYSORE‘,‘M‘, 450000);
Page 42
DBMS Lab Manual 17CSL58
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSCSE06‘,‘NEHA‘,‘SN‘,‘BANGALORE‘,‘F‘, 800000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSACC01‘,‘AHANA‘,‘K‘,‘MANGALORE‘,‘F‘, 350000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSACC02‘,‘SANTHOSH‘,‘KUMAR‘,‘MANGALORE‘,‘M‘, 300000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSISE01‘,‘VEENA‘,‘M‘,‘MYSORE‘,‘M‘, 600000);
INSERT INTO EMPLOYEE (SSN, FNAME, LNAME, ADDRESS, SEX, SALARY) VALUES
(‗RNSIT01‘,‘NAGESH‘,‘HR‘,‘BANGALORE‘,‘M‘, 500000);
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
Page 43
DBMS Lab Manual 17CSL58
Page 44
DBMS Lab Manual 17CSL58
Page 45
DBMS Lab Manual 17CSL58
Queries:
1. Make a list of all project numbers for projects that involve an employee whose
last name is ‘Scott’, either as a worker or as a manager of the department
that controlsthe project.
Page 46
DBMS Lab Manual 17CSL58
2. Show the resulting salaries if every employee working on the ‘IoT’ project is
given a10 percentraise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as
well as the maximum salary, the minimum salary, and the average salary in
thisdepartment
4. Retrieve the name of each employee who works on all the projects
Controlled by department number 5 (use NOT EXISTSoperator).
Page 47
DBMS Lab Manual 17CSL58
WHERE DNO=‘5‘)
MINUS (SELECT PNO
FROM WORKS_ON
WHERE E.SSN=SSN));
5. For each department that has more than five employees, retrieve the
department number and the number of its employees who are making more
than Rs. 6,00,000.
Page 48
DBMS Lab Manual 17CSL58
Viva Questions
1. What isSQL?
Structured Query Language
2. What isdatabase?
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 specificpurpose.
3. What isDBMS?
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.
4. What is a Databasesystem?
The database and DBMS software together is called as Database system.
5. Advantages of DBMS?
Redundancy iscontrolled.
Unauthorized access isrestricted.
Providing multiple userinterfaces.
Enforcing integrityconstraints.
Providing backup andrecovery.
6. Disadvantage in File ProcessingSystem?
Data redundancy &inconsistency.
Difficult in accessingdata.
Dataisolation.
Dataintegrity.
Concurrent access is notpossible.
SecurityProblems.
7. Describe the three levels of dataabstraction?
There are three levels of abstraction:
Physical level: The lowest level of abstraction describes how data arestored.
Logical level: The next higher level of abstraction, describes what data are stored in
database and what relationship among thosedata.
Page 49
DBMS Lab Manual 17CSL58
Page 50
DBMS Lab Manual 17CSL58
A collection of conceptual tools for describing data, data relationships data semantics
and constraints.
13. What is E-Rmodel?
This data model is based on real world that consists of basic objects called entities
and of relationship among these objects. Entities are described in a database by a set of
attributes.
14. What is Object Orientedmodel?
This model is based on collection of objects. An object contains values stored in
instance variables within the object. An object also contains bodies of code that operate on
the object. These bodies of code are called methods. Objects that contain same types of
values and the same methods are grouped together into classes.
15. What is anEntity?
It is an 'object' in the real world with an independent existence.
16. What is an Entitytype?
It is a collection (set) of entities that have same attributes.
17. What is an Entityset?
It is a collection of all entities of particular entity type in the database.
18. What is an Extension of entity type?
The collections of entities of a particular entity type are grouped together into an entity
set.
19. What is anattribute?
It is a particular property, which describes the entity.
20. What is a Relation Schema and aRelation?
A relation Schema denoted by R(A1, A2, …, An) is made up ofthe relation name
R and the list of attributes Ai that it contains. A relation isdefined as a set of tuples. Let r
be the relation which contains set tuples (t1,t2, t3, ...,tn). Each tuple is
an ordered list of n- values t=(v1,v2, ...,vn).
21. What is degree of aRelation?
It is the number of attribute of its relation schema.
22. What isRelationship?
It is an association among two or more entities.
23. What is Relationshipset?
Page 51
DBMS Lab Manual 17CSL58
Page 52
DBMS Lab Manual 17CSL58
Page 53
DBMS Lab Manual 17CSL58
Page 54
DBMS Lab Manual 17CSL58
Page 55
DBMS Lab Manual 17CSL58
Page 56
DBMS Lab Manual 17CSL58
Analysisphase
RedoPhase
Undophase
57. What do you mean by flat filedatabase?
It is a database in which there are no programs or user access languages. It has no
cross- file capabilities but is user-friendly and provides user-interface management.
58. What is "transparentDBMS"?
It is one, which keeps its Physical Structure hidden from user.
59. Brief theory of Network, Hierarchical schemas and theirproperties
Network schema uses a graph data structure to organize records example for such a
database management system is CTCG while a hierarchical schema uses a tree data
structure example for such a system is IMS.
60. What is aquery?
A query with respect to DBMS relates to user commands that are used to interact
with a data base. The query language can be classified into data definition language and
data manipulation language.
61. What do you mean by Correlatedsubquery?
Subqueries, or nested queries, are used to bring back a set of rows to be used by the
parentquery.Dependingonhowthesubqueryiswritten,itcanbeexecutedoncefortheparent
Page 57
DBMS Lab Manual 17CSL58
query or it can be executed once for each row returned by the parent query. If the subquery
is executed for each row of the parent, this is called a correlated subquery.
A correlated subquery can be easily identified if it contains any references to the
parent subquery columns in its WHERE clause. Columns from the subquery cannot be
referenced anywhere else in the parent query. The following example demonstrates a non-
correlated subquery.
E.g. Select * From CUST Where '10/03/1990' IN (Select ODATE From ORDER Where
CUST.CNUM = ORDER.CNUM)
62. What are the primitive operations common to all record managementsystems?
Addition, deletion and modification.
63. Name the buffer in which all the commands that are typed in
arestored ‘Edit’Buffer
64. What are the unary operations in RelationalAlgebra?
PROJECTION and SELECTION.
65. Are the resulting relations of PRODUCT and JOIN operation thesame?
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.
66. What is RDBMSKERNEL?
Two important pieces of RDBMS architecture are the kernel, which is the software,
and the data dictionary, which consists of the system-level data structures used by the
kernel to manage the database
You might think of an RDBMS as an operating system (or set of subsystems),
designed specifically for controlling data access; its primary functions are storing, retrieving,
and securing data. An RDBMS maintains its own list of authorized users and their associated
privileges; manages memory caches and paging; controls locking for concurrent resource
usage; dispatches and schedules user requests; and manages space usage within its table-
space structures.
67. Name the sub-systems of aRDBMS
I/O, Security, Language Processing, Process Control, Storage Management, Logging
and Recovery, Distribution Control, Transaction Control, Memory Management, Lock
Management
Page 58
DBMS Lab Manual 17CSL58
68. 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.
69. What is the job of the information stored indata-dictionary?
The information in the data dictionary validates the existence of the objects, provides
access to them, and maps the actual physical storage location.
70. Not only RDBMS takes care of locating data italso
Determines an optimal access path to store or retrieve the data
71. How do you communicate with anRDBMS?
You communicate with an RDBMS using Structured Query Language (SQL)
72. Define SQL and state the differences between SQL and other conventional
programmingLanguages
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.
73. Name the three major set of files on disk that compose a database inOracle
There are three major sets of files on disk that compose a database. All the files are
binary. These are
Database files
Control files
Redologs
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.
74. What is an OracleInstance?
The Oracle system processes, also known as Oracle background processes, provide
functions for the user processes—functions that would otherwise be done by the user
processes themselves
Page 59
DBMS Lab Manual 17CSL58
Oracle database-wide system memory is known as the SGA, the system global area
or shared global area. The data and control structures in the SGA are shareable, and all the
Oracle background processes and user processes can use them.
The combination of the SGA and the Oracle background processes is known as an
Oracle instance
75. What are the four Oracle system processes that must always be up and running
for the database to beuseable
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).
76. 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
werecreated
ControlFiles
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 redoes 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 database, the information is recorded in the control files.
Page 60
DBMS Lab Manual 17CSL58
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 theDBA.
The redo log files are fixed in size and never grow dynamically from the size at which
they were created.
77. What isROWID?
The ROWID is a unique database-wide physical address for every row on every table.
Once assigned (when the row is first inserted into the database), it never changes until the
row is deleted or the table is dropped.
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 therows
Oracle block address, which contains therow
The row within the block (because each block can hold manyrows)
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
78. What is Oracle Block? Can two Oracle Blocks have the sameaddress?
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.
Page 61
DBMS Lab Manual 17CSL58
81. Name two utilities that Oracle provides, which are use for backup andrecovery.
Along with the RDBMS software, Oracle provides two utilities that you can use to
back up and restore the database. These utilities are Exportand 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 recovering the database to the time when the export was
last performed.
82. What are stored-procedures? And what are the advantages of usingthem.
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.
Page 62
DBMS Lab Manual 17CSL58
a) Are totallyunnormalised
b) Are always in 1NF
c) Can be furtherdenormalised
d) May have multi-valuedattributes
e) Are always in1NF
84. Spurious tuples may occur dueto
i. Badnormalization
ii. Thetajoins
iii. Updating tables fromjoin
a) i&ii b) ii &iii
c)i&iii d) ii &iii
(a) i& iii because theta joins are joins made on keys that are not primary keys.
85. In mapping of ERD toDFD
a) Not anerror
b) Error - the string in single quotes 'NORTH' and'SOUTH'
Page 63
DBMS Lab Manual 17CSL58
Page 64
DBMS Lab Manual 17CSL58
Itis 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 consistentcopy.
If a cold backup is performed, the only option available in the event of data file loss is
restoring all the files
es from the latest backup. All work performed on the database since the
last backup is lost.
HotBackup:
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
no 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
tablespace-by-tablespace
tablespace basis, that the files of
the tablespace are to backed up. The users can continue to mak
makee 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 ru
running
nning 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.
96. How can you find the minimal key of relationalschema?
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
functionaldependency.
Algo. I Determining X+, closure for X, given set of FDs F
1. Set X+ =X
2. Set Old X+=X+
3. For eachFDY Z in F and if Y belongs to X+ then add Z toX+
Algo.IIDetermining
Determining minimal K for relation schema R, given set of FDs F
Page 65
DBMS Lab Manual 17CSL58
((R1(F)) U … U (Rn(F)))+=F+
Page 66
DBMS Lab Manual 17CSL58
SQL Questions:
6. Which command displays the SQL command in the SQL buffer, and then executesit?
RUN
10. What are the privileges that can be granted on a table by a user toothers?
Insert, update, delete, select, references, index, execute, alter, all
11. What command is used to get back the privileges offered by the GRANTcommand?
REVOKE
12. Which system tables contain information on privileges granted and privilegesobtained?
USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
13. Which system table contains information on constraints on all the tablescreated?
USER_CONSTRAINTS
Page 67
DBMS Lab Manual 17CSL58
Both will result in deleting all the rows in the table EMP.
Page 68
DBMS Lab Manual 17CSL58
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 otheruser.
23. What is the use of the DROP option in the ALTER TABLEcommand?
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’ is10000?
UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;
sal = 11000, comm = 1000
Page 69
DBMS Lab Manual 17CSL58
SQL HANDSON
b. EMP:
COLUMNNAME DATATYPE(SIZE)
------------------------------------------------------------------------------------
----------- EMPNO NUMBER (4)
ENAME VARCHAR2 (10)
JOB VARCHAR2 (9)
MGR NUMBER (4)
HIREDATE DATE
SAL NUMBER (7,2)
COMM NUMBER (7,2)
DEPTNO NUMBER (2)
Page 70
DBMS Lab Manual 17CSL58
5. Modify the column job present in the EMP table with the following
information given below:
COLUMNNAME DATATYPE(SIZE)
------------------------------------------------------------------------------------
----------- JOB VARCHAR2 (15)
6. modify the column ENAME present in the EMP table with the following
information given below:
COLUMNNAME DATATYPE (SIZE)
------------------------------------------------------------------------------------
----------- ENAME CHAR (15)
7. Decrease the size for the column EMPNO with the followinginformation:-
COLUMNNAME DATATYPE(SIZE)
------------------------------------------------------------------------------------
----------- EMPNO NUMBER (2)
8. Modify the column name of EMPNO to EMPLOYEE_NUMBER present in the
EMP table verify theresult.
9. Add a new column nationality placed between JOB and MGR columns and
verify theresult
10. Drop the table DEPT andEMP.
11. What is the data type of the column HIREDATE and how many bytes itoccupies.
12. Create a table EMP and DEPT using the followinginformation.
a. DEPT:
COLUMNNAME DATATYPE(SIZE)
-------------------------------------------------------------------------------------------
---- DEPTNO NUMBER(2) CONSTRAINT PK_DEPTNO
PRIMARY KEY
DNAME VARCHAR2(14)
LOC VARCHAR2(14)
b.EMP:
COLUMNNAME DATATYPE(SIZE)
Page 71
DBMS Lab Manual 17CSL58
-------------------------------------------------------------------------------------------
---- EMPNO NUMBER(4) CONSTRAINT PK_EMPNO
PRIMARY KEY
ENAME VARCHAR2(10) CONSTRAINT UQ_DEPTNO
UNIQUE
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE DEFAULTSYSDATE
SAL NUMBER(7,2) CONSTRAINT CK_SAL
CHECK(SAL)
COMM NUMBER(7,2)
DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO
REFERENCE
13. Select all the constraints in the EMPtable
SOL: SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME=‘EMP‘;
14. select the owner, constraints name, constraints type, table name, status for
DEPT table
SOL: SELECT OWNER, CONSTRAINTS_ NAME, CONSTRAINTS_TYPE, TABLE_
NAME,STATUSFROM USER_CONSTRAINTS WHERE
TABLE_NAME=‘DEPT‘;
15. Drop the constraints UQ_FMANE from EMPtable
SOL: ALTER TABLE EMP DROP CONSTRAINT UQ_FNAME;
16. Add a new column PINCODE with not null constraints to the existing tableDEPT
SOL: ALTER TABLE DEPT ADD(PINCODE NUMBER(6) NOT NULL);
18. Enable the constraints PK_DEPTNO which is defined in the DEPTNO column
of DEPTtable;
SOL: ALTER TABLE DEPT ENABLE CONSTRAINTS PK_DEPTNO;
Page 72
DBMS Lab Manual 17CSL58
20. Insert only the records of employee number, name, salary into EMPtable
SOL: INSERT INTO EMP (EMPNO, ENAME, SAL) VALUES (‗7955‘, ‗PAUL‘, 1200);
21. insert two rows into EMP table using parametersubstitution
SOL: INSERT INTO EMP VALUES (&EMPNO, ‗&ENAME‘, ‗&JOB‘, &MGR,
‗&IIIREDATE‘, &SAL, &COMM, &DEPTNO);
Page 73
DBMS Lab Manual 17CSL58
27. List the information of employees and their departments in a single DMIcommand
SOL: SELECT *. FROM EMP;
28. Copy all the records of their columns EMPNO, ENAME, JOB from EMP table and
insert the records into a temp table with column names same as EMPNO,
ENAME, JOB
SOL: INSERT INTO TEMP (EMPNO, ENAME, JOB) SELECT EMPNO, NAME, JOB
30. List the information of all the employees present in the user namedSCOTT
SOL: SELECT * FROM SCOTT. EMP;
31. List the information of the departments from your BATCHMATES DEPTtable
SOL: SELECT * FROM ORA252P. DEPT;
34. Get the information of the maximum available blocks allotted to a particular
user for creating tables from the systemtables
SOL: SELECT * FROM USER_TS_QUOTAS;
Page 74
DBMS Lab Manual 17CSL58
38. Update the salary by 10% hike to analysts working in department number 20 and30
SOL: UPDATE EMP SET SAL = SAL + 0. 1 WHERE DEPTNO IN (10,20) AND JOB
=‗ANALYST‘;
40. PROMOTE ALL THE EMPLOYEES DISCRIBED AS SALESMAN TO SALES OFFICER IF THEIR
GROSSSALARYPER MOUNTH IS GREATER THEN3000
SOL: UPDATE EMP SET JOB = ‗SALESOFFICER‘ WHERE JOB = ‗SALESMAN‘ AND
SAL>3000;
41. Give all the employees of commission ofrs500
SOL: UPDATE EMP SET COMM = 500;
Page 75
DBMS Lab Manual 17CSL58
54. Update a record of EMP table and save the changes permanently in thedatabase
SOL: UPDATE EMP SET SAL=SAL+100 WHERE EMPNO=100;COMMIT;
55. Sql * plus has the facility to automatically save all the records without issuing
the TCL command which isthat?
SOL: SET AUTOCOMMIT ON
56. Give all the privileges you have of a database object to another
SOL: GRANT ALL ON EMP TO ORA253A;
58. List the user’s id and which database object you havegranted
SOL: SELECT * FROM USE_TAB_PRIVS;
Page 76
DBMS Lab Manual 17CSL58
Exercise1: To understand some simple Database Applications and build Conceptual Data
Model.
a) Select an enterprise that you are familiar with (for example, a school, a college, a
company, a small business, a club or association). List all the information that this
enterpriseuses.
b) Describe the steps involved in the database design process using E-R Modeling:
Requirements Analysis, Identify Entity Sets, Identify Relationship Sets, Value Sets
and Attributes, Specifying Primary keys, Building E-R diagram,Implementation
c) For the following mini-world example database applications, Design and Develop
Conceptual Data Model (E-R Diagram) with all the necessary entities, attributes,
constraints andrelationships.
i. Medical Clinic Database – The clinic has a number of regular patients and new
patients come to the clinic regularly. Patients make appointments to see one of
the doctors; several doctors attend the clinic and they each have their own hours.
Some doctors are General Practitioners (GPs) while others are
specialists(cardiologists, dermatologists etc.,). Patients have families and the
family relationships are important. A medical record of each patient needs to be
maintained. Information on prescriptions, insurance, allergies, etc needs to be
maintained. Different doctors may charge different fees. Billing has to be done
forPatients.
Page 77
DBMS Lab Manual 17CSL58
who have registered for that subject by a teacher. A student is awarded a grade
in each subjecttaken.
Exercise 2: Design and build Relational Data Model for each of the application scenarios of
exercise 1 specifying all possible constraints. Extend the same for a database application of
students' choice.
Exercise 3 To understand and demonstrate DDL, DML and DCL Commands of SQL
a. Create a table called EMP with the following structure and describeit.
Name Type
----------------------------------------------
------- EMPNO NUMBER(6)
ENAME VARCHAR2(20)
DOB DATE
JOB VARCHAR2(10)
DEPTNO NUMBER(2)
SAL NUMBER(7,2)
Page 78
DBMS Lab Manual 17CSL58
Allow NULL for all columns except ENAME and JOB. EMPNO is the Primary
Key
b. Add a column EXPERIENCE of type NUMERIC to the EMP table. Allow NULL toit.
c. Modify the column width of the JOB field of EMPtable.
d. Create DEPT table with the following structure and describeit
Name Type
----------------------------------------------
------- DEPTNO NUMBER(2)
DNAME VARCHAR2(15)
LOCN VARCHAR2(10)
DEPTNO is the Primary Key and DNAME cannot be NULL
e. Add constraint to check the SAL value of EMP Table. SAL must be >6000.
f. Drop a column EXPERIENCE from the EMPtable.
g. Insert a single record into DEPT table. Repeat this for inserting at least 3records
h. Insert more than a record into EMP table using a single insert command. Insert at
least 10 records
i. Update the EMP table to set the salary of all employees to Rs. 30000/- for a given
JOB type
j. Create a pseudo table EMPLOYEE with the same structure as the table EMP using
SELECTclause.
k. Delete employees from EMP table for a given JOB type. Delete the first five records
of EMP table
l. Grant all/some privileges of EMP table to DEPT table
m. Revoke some/all privileges of EMP table from DEPTtable
n. Truncate the EMP table and drop the DEPTtable
o. Demonstrate the use of COMMIT, SAVEPOINT and ROLLBACKcommands
Page 79