DBMS Lab Report 3rd Year B.tech
DBMS Lab Report 3rd Year B.tech
LAB MANUAL
Submitted by : Submitted to :
INDEX
LAB PRE-REQUISITE
The basic pre-requisite for Database Management System is the basic programming skills and
knowledge about relational database model.
Developing and managing efficient and effective database applications require understanding
the fundamentals of database management systems, techniques for the design of databases,
and principles of database administration.
LAB MANUAL OBJECTIVE
This manual clearly explains the concepts of D.B.M.S. and the principles deployed behind
databases.
The Objective in writing of this lab manual is to produce a general , comprehensive text that
treats all the essential core area of D.B.M.S.
The Goal is to describe the real world constraint and realities of Database Management
Systems.
I have done my best to hunt down and eradicate all errors in this manual.
INDEX
1. Implementation of cursors
2. Program for Student Grade Calculation
3. Database Design using E-R Model and Normalization.
4. Database Design and Implementation of Pay Roll Processing
5. Design and Implementation of Banking System.
…………………………………………………….
NAME & SIGNATURE OF CONCERNED FACULTY
BASIC CONCEPTS OF PROGRAMS
objects in
the database.
the records
the Records
TYPES OF CONSTRAINTS:
1) Primary key : A PRIMARY KEY constraint is a unique identifier for a row within a
database table. Every table should have a primary key constraint to uniquely identify
each row and only one primary key constraint can be created for each table. The
primary key constraints are used to enforce entity integrity.
2) Foreign key/references : A FOREIGN KEY constraint prevents any actions that would
destroy link between tables with the corresponding data values. A foreign key in one table
points to a primary key in another table. Foreign keys prevent actions that would leave rows
with foreign key values when there are no primary keys with that value. The foreign key
constraints are used to enforce referential integrity.
3) Check : A CHECK constraint is used to limit the values that can be placed in a column.
The check constraints are used to enforce domain integrity.
4) Unique : A UNIQUE constraint enforces the uniqueness of the values in a set of columns,
so no duplicate values are entered. The unique key constraints are used to enforce entity
integrity as the primary key constraints.
5) Not null : A NOT NULL constraint enforces that the column will not accept null values.
The not null constraints are used to enforce domain integrity, as the check constraints.
SQL joins are used to query data from two or more tables, based on a
SQL COMMANDS
from the left table (table_name1), even if there are no matches in the
from the right table (table_name2), even if there are no matches in the
• Provides user security on each view - it depends on your data policy security.
• Using view to convert units - if you have a financial data in US currency, you can create
view to convert them into Euro for viewing in Euro currency.
SQL COMMANDS
UNION
It returns a union of two select statements. It is returning unique (distinct) values of them.
MINUS
MINUS (also known as EXCEPT) returns the difference between the first and second
SELECT statement. It is the one where we need to be careful which statement will be put
first, cause we will get only those results that are in the first SELECT statement and not in the
second.
INTERSECT
INTERSECT is opposite from MINUS as it returns us the results that are both to be found in
first and second SELECT statement.
An aggregate function is a function where the values of multiple rows are grouped together
as input on certain criteria to form a single value of more significant meaning or
measurement such as a set, a bag or a list.
Nested Query can have more than one level of nesting in one single query. A SQL nested
query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE
SQL query.
SQL COMMANDS
declare
n number;
i number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(n||'! = '||f);
end;
Output:
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL procedure successfully completed.
11. Write a PL/SQL code block to implement Indexes.
, . . . ,column_nameN)
[COMPUTE STATISTICS];
A collection is an ordered group of elements having the same data type. Each element is
identified by a unique subscript that represents its position in the collection.
• Nested table
An index-by table (also called an associative array) is a set of key-value pairs. Each key
is unique and is used to locate the corresponding value. The key can be either an integer
or a string.
Where,
• The optional parameter list contains name, mode and types of the parameters. IN
represents that value will be passed from outside and OUT represents that this
parameter will be used to return a value outside of the procedure.
Trigger automatically associated with DML statement, when DML statement execute
trigger implicitly execute.
We can create trigger using the CREATE TRIGGER statement. If trigger activated,
implicitly fire DML statement and if trigger deactivated can't fire.
DECLARE
[declaration_section
variable declarations;
constant declarations;
]
BEGIN
[executable_section
PL/SQL execute/subprogram body
]
EXCEPTION
[exception_section
PL/SQL Exception block
]
END;
SQL> declare
3 b bill %ROWTYPE;
4 begin
5 open c;
7 loop
8 fetch c into b;
10 exit;
11 else
12 if(b.unit<=100) then
4);
20 else
5);
22 end if;
23end if;
24end loop;
25close c;
26end;
27/
Adodc1.Recordset.Update
End Sub
End Sub
Private Sub
clear_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
in request format
3. ADODC CONTEOL FOR ACCOUNT FROM:- Click customs and property window and
window will appear and select ODBC data source name as oracle and click apply as the
some window.
QUERY: 02 Write a query to display the column name and datatype of the
table employee.
EMPNO NUMBER(4)
ENAME VARCHAR2
(10)
DESIGNA VARCHAR2
TIN (10)
SALARY NUMBER(8
,2)
ALTER & MODIFICATION ON TABLE
QUERY: 03 Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO
NUMBER(6).
SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME> <DATATYPE> (SIZE);
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 04 Write a Query to Alter the table employee with multiple columns (EMPNO,
ENAME.)
SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME1> <DATATYPE> (SIZE),
MODIFY <COLUMN NAME2> <DATATYPE> (SIZE)
………………………………………….;
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);
QUERY: 05 Write a query to add a new column in to employee
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME> <DATA TYPE>
<SIZE>);
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME1> <DATA TYPE>
<SIZE>,(<COLUMN NAME2> <DATA TYPE> <SIZE>,
………………………………………………………………);
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
DOJ DATE
REMOVE / DROP
QUERY: 07 Write a query to drop a column from an existing table employee
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
REMOVE
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
INSERT
SELECT
SQL> /
Enter value for empno: 103
Enter value for ename: PANNERSELVAM
Enter value for designatin: ASST. PROF
Enter value for salary: 20000
old 1: INSERT INTO EMP
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP VALUES(103,'PANNERSELVAM','ASST.
PROF','20000')
1 row created.
SQL> /
UPDATE
DELETE
ROLL BACK
COMMIT
QUERY: 03 Write a query to implement the Rollback.
SQL> COMMIT;
SQL> COMMIT;
Commit complete.
Constraints are part of the table definition that limits and restriction on the value entered into
its columns.
TYPES OF CONSTRAINTS:
1) Primary key
2) Foreign key/references
3) Check
4) Unique
5) Not null
6) Null
7) Default
QUERY 2. Write a query to create primary constraints with column level with naming
convention
QUERY 3. Write a query to create primary constraints with table level with naming
convention
QUERY 5. Write a query to create foreign key constraints with column level
Parent Table:
QUERY 6. Write a query to create foreign key constraints with Table level
Parent Table:
Child Table:
Syntax for Table level constraints using foreign key:
QUERY 7. Write a query to create foreign key constraints with Table level with alter
command.
Parent Table:
Child Table:
Check constraint
Unique Constraint
Query 11. Write a query to create unique constraints with column level
Query 12. Write a query to create unique constraints with table level
Syntax for Table level constraints with Check Using Alter
SQL :> CREATE <OBJ.TYPE> <OBJ.NAME> (<COLUMN NAME.1> <DATATYPE>
(SIZE), (COLUMN NAME2 <DATATYPE> (SIZE)) ;
Not Null
Query 13. Write a query to create Not Null constraints with column level
Null
QUERY 14. Write a query to create Null constraints with column level
Syntax for Column level constraints with Null:
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables.
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
Table:1 - ORDERS
O_ID NUMBER(5)
ORDERNO NUMBER(5)
P_ID NUMBER(3)
TABLE SECTION:
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
OUTPUT
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
FULL OUTER JOIN SYNTAX
SQL>SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
INNER JOIN
SQL>SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
SQL COMMANDS
• CREATE VIEW
• INSERT IN VIEW
• DELETE IN VIEW
• UPDATE OF VIEW
• DROP OF VIEW
CREATION OF TABLE
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DISPLAY VIEW:
DELETION OF VIEW:
UPDATE STATEMENT:
Nested Query can have more than one level of nesting in one single query. A SQL nested
query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE
SQL query.
SQL COMMANDS:
• SELECT
• WHERE
• HAVING
• MIN(SALARY)
SELECT "COLUMN_NAME1"
FROM "TABLE_NAME1"
WHERE "COLUMN_NAME2" [COMPARISON OPERATOR]
(SELECT "COLUMN_NAME3"
FROM "TABLE_NAME2"
WHERE [CONDITION])
ENAME
MAHESH
MANOJ
KARTHIK
MANI
VIKI
MOHAN
NAVEEN
PRASAD
AGNESH
10. Write a PL/SQL code block to find factorial of a number.
declare
n number;
i number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(n||'! = '||f);
end;
Output:
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL procedure successfully completed.
11. Write a PL/SQL code block to implement Indexes.
3 ORDER BY vendor_id;
1 row selected.
Index created.
12. Write a PL/SQL code block to implement Procedures.
Begin
i:=1;
tot:=0;
while(i<=n)
loop
j:=1;
c:=0;
while(j<=i)
loop
if(mod(I,j)=0) then
c:=c+1;
end if;
j:=j+1;
end loop;
if(c=2) then
dbms_output.put_line(i);
tot:=tot+1;
end if;
i:=i+1;
end loop;
end;
Sql>procedure created.
declare
t number;
begin
prime_proc(10,t);
end;
sql>set serveroutput on
OUTPUT
sql>/
Begin
2 For I in 1.. 10 loop
3 If mod(i,2) <> 0 then
4 Dbms_output.put_line(' I am an odd number :'||i);
5 else
6 Dbms_output.put_line(' I am an even number :'||i);
7 End if;
8 End loop;
9* end;
SQL> /
I am an odd number :1
I am an even number :2
I am an odd number :3
I am an even number :4
I am an odd number :5
I am an even number :6
I am an odd number :7
I am an even number :8
I am an odd number :9
I am an even number :10