DBMS Lab Manual
DBMS Lab Manual
For
3130703
2 Implement Constraints 9
7 Views 32
9 cursors 38
10 Triggers 39
11 Concepts of Normalization
12 Conversion in 3 NF
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 1
AIM:
OBJECTIVES
THEORY
● ALTER – The structure of a table can be modified by using the ALTER TABLE command.
This command is used to add a new column, modify the existing column definition and to
include or drop integrity constraint.
● DROP - It will delete the table structure provided the table should be empty.
● TRUNCATE - If there is no further use of records stored in a table and the structure has to be
retained, then the records alone can be deleted.
SYNTAX:
EXAMPLE:
ALTER TABLE
Syntax:
EXAMPLE:
Syntax:
EXAMPLE:
(c) Dropping a Column from a Table
Syntax:
Syntax:
Syntax:
EXAMPLE:
RENAMING TABLES
Syntax:
Rename <oldtable> to <new table>;
EXAMPLE:
Rename Employee to Employee 1;
TRUNCATE TABLE
Syntax:
Example:
Truncate table Employee;
DESTROYING TABLES
Syntax:
Example:
DESCRIBE TABLES
. Syntax:
Example:
desc employee;
RESULT:
PRODUCT_MASTER
PRODUCTNO Varchar2 6
DESCRIPTION Varchar2 15
UNITMEASURE Varchar2 10
QTYONHAND Number 8
REORDERLVL Number 8
CLIENTNO Varchar2 6
NAME Varchar2 20
ADDRESS1 Varchar2 30
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
SALESMANNO Varchar2 6
SALESMANNAME Varchar2 20
ADDRESS1 Varchar2 30
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
Table Name : STUDENT
SNO Number 5
SNAME Varchar2 20
AGE Number 5
SDOB Date
(a) Add a column called ‘telephone’ of data type ‘number’ and size =’10’ to
the Client _Master table.
Title : “Constraints ”
AIM:
THEORY
Constraints are the business Rules which are enforced on the data being stored
in a table are called Constraints
TYPES OF CONSTRAINTS:
1) Primary key
2) Foreign key/references
3) Check
4) Unique
5) Not null
6) Null
7) Default
PROCEDURE
Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE CONSTRAINT
<constraintname1> PRIMARY KEY, Columnname2 DATATYPE,
columnname3 DATATYPE,. );
EXAMPLE
Syntax:
EXAMPLE
Syntax:
EXAMPLE
Syntax:
EXAMPLE
Syntax
EXAMPLE
Syntax
EXAMPLE
Syntax
Exercise:
PRODUCT_MASTER
ADDRESS1 Varchar2 30
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
ADDRESS2 Varchar2 30
CITY Varchar2 15
PINCODE Number 8
STATE Varchar2 15
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :3
AIM:
OBJECTIVES
THEORY
DML commands are the most frequently used SQL commands and are used to query
and manipulate the existing database objects. Some of the commands are
1. INSERT
This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The
values must be entered in the same order as they are defined.
2. SELECT
It is used to retrieve information from the table. It is generally referred to as
querying the table. We can either display all columns in a table or only specify
columns from the table.
3. UPDATE
It is used to alter the column values in a table. A single column may be updated
or more than one column could be updated.
4. DELETE
After inserting row in a table we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
PROCEDURE
INSERT COMMAND
Syntax:
Example:
SQL>INSERT INTO EMPLOYEE VALUES(101,'MANU','LECTURER',15000);
(b) Inserting more than one record using a single insert commands:
Syntax:
Example:
SELECT COMMAND
Syntax:
Select * from
tablename;
Example:
Example:
Syntax:
Example:
Syntax:
<tablename> Example:
UPDATE COMMAND
Syntax:
Example:
Syntax:
Example:
Syntax:
RESULT
Problems
1. Insert the following data into their respective tables. Data for
CLIENT_MASTER table
c. Retrieve the list of names,city and the state of all the clients
d. List the various products available from the Product _Master table
a. Delete all salesman from the Salesman_master whose salaries are equal to
Rs.3500.
c. Delete from the Client_master where the column state holds the value
‘Tamilnadu’.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :4
AIM:
OBJECTIVES
THEORY:
Data Control Language (DCL) consists of various commands which are related to data
sharing and security of data in database.
They are
GRANT
REVOKE
Granting Privileges:
Objects that are created by a user are owned and controlled by that user. If a user wishes
to access any of the objects belonging to another user, the owner of the object will have
to give permissions for such access. This is called Granting of Privileges.
Granting privileges using the GRANT statements:
The GRANT statements provide various types of access to database objects such as
tables, views.
Syntax:
GRANT {object privileges}
ON object name
TO username;
Object Privileges:
Each object privilege that is granted authorizes the grantee to perform some operation
on the object. The user can grant all the privileges or grant only specific object
privileges.
The list of object privileges is as follows:
• ALTER: allows the grantee to change the table definitions with the ALTER
table command.
• DELETE: allows the grantee to remove the records from the table with the
DELETE command.
• INDEX: allows the grantee to create an index on the table with the CREATE
INDEX command.
• INSERT: allows the grantee to add records to the table with the INSERT
command.
• SELECT: allows the grantee to query the table with SELECT command.
• UPDATE: allows the grantee to modify the records in the table with the
UPDATE command.
Revoking privileges given:
Privileges once given can be denied to a user using the REVOKE command. The
object owner can revoke privileges granted to another user. A user of an object who
is not the owner, but has been granted the GRANT privilege, has the power to
REVOKE the privileges from the grantee.
Revoking permission using the REVOKE statement:
The REVOKE statement is used to deny the grant given on an object.
Syntax:
REVOKE {object privileges} ON
object name
FROM username;
The REVOKE command is used to revoke object privileges that the user previously
granted to the Revoke
The REVOKE command cannot be used to revoke the privileges granted through
operating system.
RESULT:
Familiarized DCL statements.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :5
AIM
To implement computations done on data of the given table
OBJECTIVES
To understand computations done on data of the given table with built in functions
THEORY
Group Functions/Aggregate functions
A group function returns a result based on group of rows.
1. avg
Example: select avg (total) from student;
2.max
Example: select max (percentagel) from student;
2.min
Example: select min (marksl) from student;
4. sum
Example: select sum(price) from product
Count Function
In order to count the number of rows, count function is used.
1. count(*) – It counts all, inclusive of duplicates and nulls.
Example: select count(*) from student;
2. count(col_name)– It avoids null value.
Example: select count(total) from order;
2. count(distinct col_name) – It avoids the repeated and null values.
Example: select count(distinct ordid) from order;
Special Clauses:
Group by clause
This allows us to use simultaneous column name and group functions.
Example: Select max (percentage), deptname from student group by deptname;
Having clause
This is used to specify conditions on rows retrieved by using group by clause.
Example: Select max(percentage), deptname from student group by deptname having
count(*)>=50;
In / not in – used to select a equi from a specific set of values
Any - used to compare with a specific set of values Between / not
between – used to find between the ranges Like / not like – used to do
the pattern matching
PROCEDURE
OUTPUT
RESULT
PROGRAMS
● list the names of all clients having ‘a’ as the second letter in their names.
● listing of clients who stay in a city whose first letter is ‘M’
● list all clients who stay in ‘Bangaluru’ or ‘Mangalore’
● list all clients w.hose BalDue is greater than 10000
● display the order information of clientno ‘C00001’ and’C00002’,list products
whose selling price is greater than 500 and less than or equal to 750
● Listing of names,city and state of clients who are not in the state of
‘maharashtra’.
● Determining the maximum and minimum price for the product prices.
● count the number of products having the price greater than or equal to 500
a. printing the description and total quantity sold for each product.
c. find out the total of all the billed orders for the month of june.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 6
AIM:
OBJECTIVES
THEORY
a)NESTED QUERIES:
A sub query is a query within a query. In Oracle, we can create sub queries
within your SQL statements. These sub queries can reside in the WHERE clause,
the FROM clause, or the SELECT clause.
b) JOINS:
Natural join:
It returns the matching rows from the table that are being joined
.
Syntax:
>select <attribute> from TN where TN1.attribute=TN2.attribute.
Inner join:
It returns the matching rows from the table that are being joined.
Syntax:
>select <attribute> from TN1 innerjoin TN2 on TN1.attribute=TN2.attribute.
Left outer join:
It returns all the rows from the table1 even when they are unmatched.
Syntax:
5. select <attribute> from TN1 left outer join TN2 on
TN1.attribute=TN2.attribute.
2. select <attribute> from TN where TN1.attribute(+)=TN2.attribute.
Syntax:
4. select <attribute> from TN1 right outer join TN2 on
TN1.attribute=TN2.attribute.
2. select <attribute> from TN where TN1.attribute=(+)TN2.attribute.
Full join:
PROCEDURE
NESTED QUERIES -
PNO NUMBER(3)
ENO NUMBER(3)
PJOB CHAR (12)
1 DBMS 2
2 COMPILER 3
3 C1 1
1 1 Programmer
2 1 Analyst
1 2 Analyst
2 2 Programmer
NESTED QUERIES
(i) SQL> select ename from emp_det where dno not in(select dno
from emp_det where ename ='SaravanaKumar');
ENAME
RajKumar
Shirley
(ii) SQL> select ename, dno from emp_det where dno = (select dno
from emp_det where ename ='RajKumar');
ENAME DNO
RajKumar 2
(iii) SQL> select ename from emp_det where eno in(select eno from
work_in where pno = (select pno from pro_det where pname = 'DBMS'))
order by ename;
ENAME
Mahendran
SaravanaKumar
(iv) SQL> select ename, basic_sal from emp_det where dno = 2 and
basic_sal>(select max(basic_sal) from emp_det where dno = 10) order by
ename;
ENAME BASIC_SAL
RajKumar 10000
(v) SQL> select pno,pname from pro_det where exists(select pno from
work_in where work_in.pno =pro_det.pno);
PNO PNAME
1 DBMS
2 COMPILER
MAX(BASIC_SAL)
8000
(ix) SQL> select * from emp_det where basic_sal < (select avg(basic_sal)
from emp_det);
ashu 10000
asma 1200
asif 2000
arif 1000
niyas 3000
SQL> create table emp1(name varchar2(20),empid number(10));
Table created.
.
SQL> select * from emp1;
NAME EMPID
fathi 12
sumi 32
priya 11
wahab 10
sweety 9
asma 1200
6 rows selected.
NATURAL JOIN
asma 1200
asma 1200
asif 2000
arif 1000
niyas 3000
ashu 10000
asma 1200
sweety 9
sumi 32
wahab 10
fathi 12
priya 11,
6 rows selected.
FULL JOIN
SQL>select emp1.name,emp.name,emp1.empid,salary from emp full join emp1
on
emp.name=emp1.name
NAME NAME EMPID SALARY
RESULT:
Thus the nested queries and join operations are executed and verifiedin DBMS.
Exercise :
1. Exercises on sub-queries
b) Find the name and complete address for the customer who has placed
order number ‘o19001’
c) find the clients who have placed orders before the month of may ‘02
d) find the names of clients who have placed orders worth Rs.10000 or more.
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No : 7
Title : “VIEWS”
AIM:
OBJECTIVES
To implement views
THEORY
A view is the tailored presentation of data contained in one or more table and can
also be said as restricted view to the data‟s in the tables. A view is a “virtual
table” or a “stored query” which takes the output of a query and treats it as a
table. The table upon which a view is created is called as base table . A view is a
logical table based on a table or another view. A view contains no data of its own
but is like a window through which data from tables can be viewed or changed.
The tables on which a view is based are called base tables. The view is stored as
a SELECT statement in the data dictionary .
Advantages of a view:
a. Additional level of table security.
b. Hides data complexity.
c. Simplifies the usage by combining multiple tables into a single table
Syntax:
Create or replace view view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Example
Create or replace view empview as select * from emp;
PROCEDURE
1)create a table aa
2) describe the
from aa;
3)create table qq
SQL> create table qq(name varchar2(20),book number(10),author
varchar(20),publisher varchar2(20),ISBN number(20));
4) describe table qq
bb 21 23 dfd 573568
cc 43 55 fg 65839
ee 44 21 dfd 1235798
oo 87 34 gfh 6358379
5)create a view on qq
View created.
21 bb qwa
cc 65839 fg
ee 1235798 dfd
oo 6358379 gfh
SQL> create view ss as select name,book from aa union select name,book from
qq;
View created.
SQL> select * from ss;
NAME BOOK
bb 21
bb 23
cc 43
cc 55
dd 2
ee 21
ee 44
oo 87
8 rows selected.
Result
Thus the view creation commands are executed successfully.
EXERCISE:
Table:Hosp_doc
Doc_code Varchar2(4)
Doc_name Varchar2(4)
Specialization Varchar2(4)
Department Varchar2(4)
Date_of_join Date
Exper Number(2)
AIM:
OBJECTIVES
FUNCTION:
A function is a subprogram that computes a value.
Syntax:
PROGRAM
OUTPUT
SQL> /
Enter value for a: 4
old 2: dbms_output.put_line('the factorial='||fact(&a)) new
2: dbms_output.put_line('the factorial='||fact(4)); the
factorial=24
PL/SQL procedure successfully completed.
RESULT:
Thus the functions and stored procedures are executed in SQL.
Problems;
1) procedure to find whether a given number is odd or even
2) procedure to display 1-10 using while
3) Procedure to display some numbers lesser than given number
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :9
Title : “CURSORS”
AIM
To retrieve all students who have registered for Diploma and store their details into
another table called diploma (id,name) using cursors.
OBJECTIVES
To implement cursor
PROCEDURE
1) TABLE CREATION
SQL>create table student(id number,name varchar2(25),programme varchar2(25));
students;
Id name programme
1 rohan diploma
2 anu MA
3 robert diploma
4 tom btech
5 sunny diploma
SQL>declare
2 cursor stud is select * from students where programme =”diploma’;
3 id students.id%type;
4 name students.name%type
5 prog students.programme%type
6 begin
7 open stud;
8 loop
9 fetch stud into id,name,prog;
10 exit when stud%notfound;
11 insert into diploma values(id,name);
12 endloop;
13 end;
14 /
OUTPUT
RESULT
PROGRAMS
1.A HRD manager has decided to raise the salary of all employees in department
number 20 by 0.05.Whenever such raise is given to the employees,the employee
number ,the date when raise was given and raise amount are maintained in the
emp_raise table.Write a PL/SQL block using cursors to update the salary of each
employee of dept no 20 and insert a record in the emp_raise table as well
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :10
Title : “TRIGGER”
AIM
Create a Trigger for EMP table it will update another table SALARY while inserting
values
OBJECTIVES
To develop and execute a Trigger for Before and After update/Delete/Insert operations
on a table
THEORY.
PROCEDURE
step 1: start
step 2: initialize the trigger with specific table id.
step 3:specify the operations (update, delete, insert) for which the trigger has to be
executed.
step 4: execute the trigger procedure for both before and after sequences step
5: carryout the operation on the table to check for trigger execution. step 6:
stop
PROGRAM
sql> create table emp(iname varchar2(10),iid number(5),salary number(10)); table
created.
vec 1 1000
srm 0 0
Vec 1 1000
srm 1 3000
sql> insert into emp values('vec',100,5000);
1 row created.
vec 2 6000
srm 1 3000
sql> insert into emp values('vec',100,2000);
1 row created.
vec 3 8000
srm 1 3000
sql> insert into emp values('srm',200,8000);
1 row created.
Vec 3 8000
Srm 2 11000
RESULT:
The trigger procedure has been executed successfully for both before and after
sequences.
Problems
1. Write a trigger that stores the details of students changing their program
from CT to CHM.
2. Write an update trigger on CLIENT_MASTER table.The system should
keep track of the records that are being updated.The old values of the updated
record should be added in the AUDIT_TRAIL table
Column name Data type size
Client_no Varchar2 6
name Varchar2 20
Bal_due Number 10,2
Operation Varchar2 8
userid Varchar2 20
olddate date
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :11
Problem Statement:
An exercise to check whether the given database table is normalized or not. If
yes find out the status of normalization and reasoning.
Objective:
To study the concept of various levels of normalization and understand how to
convert into normalized forms.
Library and
222-33-4444 Lynn Lee
Information Science
987-65-4321 Mary Ruiz Pre-Medicine
We can easily verify that this table satisfies the definition of 1NF: viz., it has no
duplicated rows; each cell is single-valued (i.e., there are no repeating groups or
arrays); and all the entries in a given column are of the same kind. In this table
we can see that the key, SSN, functionally determines the other attributes;
i.e.,FirstName, LastName, and Major. .
Government Engineering College Modasa
LAB MANUAL
Course Name: DBMS(3130703) Practical No :11
AIM
An exercise to check whether the given database table is normalized or not. If yes,
find out the status of normalization and reasoning.
Objective:
Design/Theory
Create a database table in SQL with a few rows and columns.Analyze the table
and determine to which normal form it belongs to according to the rules and
regulations of each normal form.
Procedure:
Consider a book database table as given below.
By examining the table, we can infer that books dealing with history, cognitive
psychology, and folksong are assigned to the PCL General Stacks collection; that books
dealing with legal procedures are assigned to the Law Library; that books dealing with Greek
literature are assigned to the Classics Library; that books dealing with library biography are
assigned to the Library and Information Science Collection (LISC);and that books dealing with
music literature are assigned to the Fine Arts Library.
Moreover, we can infer that the PCL General Stacks collection and the
LISC are both housed in the Perry-Castañeda Library (PCL) building; that the
Classics Library is housed in Waggener Hall; and that the Law Library and Fine
Arts Library are housed, respectively, in Townes Hall and the Fine Arts Building.
Thus we can see that a transitive dependency exists in the above table :
any book that deals with history, cognitive psychology, or library biography will
be physically housed in the PCL building (unless it is temporarily checked out to
a borrower); any book dealing with legal procedures will be housed in Townes
Hall; and so on. In short, if we know what subject a book deals with, we also
know not only what library or collection it will be assigned to but also what
building it is physically housed in.
To solve this problem decompose the above table into three different tables as
follows
Table A
Table B
Music Literature
Music Publishing and Collecting
Table C
Music Literature
Fine Arts Library
Table D
1. What is a database?
2. What is DBMS?
3. Give an example for an RDBMS.
4. List the benefits of DBMS.
5. Disadvantage in File Processing System
6. What is a key? what are different keys in database?
7. What is a primary key?
8. What is a secondary key?
9. What is a candidate key?
10. What is an alternate key?
11. What is a super key?
12. What is a composite key?
13. What is a relation?
14. What is a table?
15. What is an attribute?
16. What is a domain?
17. What is a tuple?
18. What is a selection?
19. what is a join operation?
20. What are base operations in relational algebra?
21. What are different DBMS facilities? How many types of facilities are provided by a DBMS?
22. What is Data Definition Language?
23. What is Data Dictionary?
24. What is a DML?
25. What is a query?
26. What is a query language?
27. What are the advantages of DBMS?
28. What is a SQL?
29. What are the features of SQL?
30. How SQL organizes the data?
31. What is data definition?
32. What is data retrieval?
33. What is data sharing?
34. What is a view?
35. What is normalization?
36. What is a first normal form?
37. What is a second normal form?
38. What is a third normal form?
39. What is BCNF?
40. What is fifth normal form?
41. What is Functional Dependency?
42. What is Lossless join property?
43. What are the commands to delete, modify and insert a record in the table?
44. What is time stamping?
45. What is data base schema?
46. What is a self join?
47. What are the different aggregate functions in SQL?
48. What is data integrity?
49. What is data independence?
50. What is dead locking?
51. What is decryption?
52. What is a distributed database?
53. What is an entity?
54. What is a conceptual data model?
55. What is two phase locking?
56. What is projection?
57. What are the different phases of transaction?
58. What is Relational Algebra?
59. What is Relational Calculus?