Pages 4 36
Pages 4 36
Experiment 1:
Consider following databases and draw ER diagram and convert entities and relationships
to relation table for a given scenario.
1. COLLEGE DATABASE:
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
2. COMPANY DATABASE:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
SOLUTION:
Mapping
entities and
relationships
to relation
table
(Schema
Diagram)
COMPANY DATABASE:
E-R Diagram
Schema Diagram
Experiment 2
SOLUTION:
Operation Purpose
Select(σ) The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition
Projection(π) The projection eliminates all attributes of the input relation but those
mentioned in the projection list.
Set Difference(-) - Symbol denotes it. The result of A - B, is a relation which includes
all tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation consisting of a set of all tuple that are
in both A and B.
Inner Join Inner join, includes only those tuples that satisfy the matching
criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is
denoted by symbol θ.
EQUI Join When a theta join uses only equivalence condition, it becomes a equi
join.
Natural Join(⋈) Natural join can only be performed if there is a common attribute
(column) between the relations.
Outer Join In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( In the left outer join, operation allows keeping all tuple in the left
) relation.
Right Outer join( In the right outer join, operation allows keeping all tuple in the right
) relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in the
result irrespective of the matching condition.
Experiment 3
SOLUTION:
1. Creating a Database
CREATE DATABASE Company;
NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter department
table to add foreign constraint MGRSSN using sql command
Update
COMMIT;
On execution of this command all changes to the database made by you are made
permanent and cannot be undone.
A COMMIT is automatically executed when you exit normally from SQL*Plus.
However, it does no harm to occasionally issue a COMMIT command.
A COMMIT does not apply to any SELECT commands as there is nothing to
commit.
A COMMIT does not apply to any DDL commands (eg CREATE TABLE,
CREATE INDEX, etc). These are automatically committed and cannot be rolled
back.
If you wished to rollback (ie undo) any changes made to the database since the
last commit, you can issue the command:
ROLLBACK;
A group of related SQL commands that all have to complete successfully or otherwise be
rolled back, is called a transaction. Part of your research for Outcome 3 includes
investigating transaction processing and the implications of rollback and commit.
Experiment 4
SOLUTION:
Create Table
2. Add a new column PINCODE with not null constraints to the existing table DEPT
Table altered.
3. All constraints and views that reference the column are dropped automatically, along
with the column.
Table altered.
Table altered.
SQL> DESC DEPARTMENT;
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(38)
DEPT_NAME VARCHAR2(10)
LOC VARCHAR2(4)
PINCODE NOT NULL NUMBER(6)
6. Delete table
SQL> DROP TABLE DEPARTMENT;
Table dropped.
Experiment 5A
Consider Employee table
AVG(SALARY)
-----------
89400
COUNT(*)
----------
5
6. Retrieve total salary of employee group by employee name and count similar names
EMP_NAME SUM(SALARY)
------------------------------ -----------
mahesh 145000
sunita 187000
EMP_NAME
------------------------------
sunita
sunita
mahesh
Amit
Amit
9. Display details of employee whose name is AMIT and salary greater than 50000;
Experiment 5B
For a given tables
SOLUTION
Table altered.
MGRSTARTDATE DATE
MGRSSN VARCHAR2(20)
Note: update entries of employee table to fill missing fields SUPERSSN and DNO
UPDATE EMPLOYEE SET SUPERSSN=NULL, DNO=‘3‘ WHERE
SSN=‘RNSECE01‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE02‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE01‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE03‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE02‘;
UPDATE EMPLOYEE SET SUPERSSN=‘RNSCSE04‘, DNO=‘5‘ WHERE
SSN=‘RNSCSE03‘;
2. 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 this
department
SQL> SELECT SUM(E.SALARY),MAX(E.SALARY),MIN(E.SALARY),
AVG(E.SALARY)FROM EMPLOYEE1 E,DEPARTMENT D WHERE
E.DNO=D.DNUMBER AND D.DNAME='RESEARCH';
2 FROM EMPLOYEE1 E
4. Retrieve the name of each dept and number of employees working in each
department which has at least 2 employees
SELECT DNAME, COUNT(*)
FROM EMPLOYEE E, DEPARTMENT D
WHERE D.DNO=E.DNO
AND D.DNO IN (SELECT E1.DNO
FROM EMPLOYEE E1
GROUP BY E1.DNO
having count(*)>2 )
ORDER BY DNO;
6. Retrieve the name of employees and their dept name (using JOIN)
Experiment 5C
Perform the String Functions, Date functions and Mathematical functions supported
by Oracle
ASCII('T')
----------
116
ASCII('A')
----------
97
ASCII('A')
----------
65
ASCII('Z')
----------
90
ASCII('Z')
----------
122
SQL> SELECT UPPER('bldea sb arts and kcp science college') from dual;
UPPER('BLDEASBARTSANDKCPSCIENCECOLLEG
-------------------------------------
BLDEA SB ARTS AND KCP SCIENCE COLLEGE
LOWER('WELCOMETODBM
-------------------
welcome to dbms lab
LOWER('WELCOMETODB
------------------
welcome to dbmslab
REPLA
-----
KELLO
REPLACE(
--------
KOMPUTER
REPLA
-----
HEAAO
TRIM('
--
NACOND
LTRIM('
-------
NACONDA
LTR
---
NIL
RTRI
----
ANIT
RTRIM('
-------
ANACOND
RTRIM('ANAC
-----------
ANACONDA
Date Functions
SQL> SELECT CURRENT_DATE FROM DUAL;
CURRENT_D
---------
14-AUG-19
EXTRACT(YEARFROMSYSDATE)
------------------------
2019
EXTRACT(DAYFROMSYSDATE)
-----------------------
14
EXTRACT(MONTHFROMSYSDATE)
-------------------------
8
SYSDATE
---------
14-AUG-19
Mathematical Functions
SQL> select ABS(-100) from dual;
ABS(-100)
----------
100
SQL> select ABS(-6) from dual;
ABS(-6)
----------
6
SQL> select FLOOR(2345.78) FROM DUAL;
FLOOR(2345.78)
--------------
2345
SQL> SELECT GREATEST(23,67,90,123,78,50) FROM DUAL;
GREATEST(23,67,90,123,78,50)
----------------------------
123
LEAST(34,21,67,11,89,9)
-----------------------
9
ROUND(5.86)
-----------
6
----------
-1.9952004
MOD(4,3)
----------
1
MOD(4,2)
----------
0
SQL> SELECT EXP(2) FROM DUAL;
EXP(2)
----------
7.3890561
EXP(-2)
----------
.135335283
EXP(0)
----------
1
Experiment 6
For a given EMPLOYEE tables
SOLUTION:
Creating View
The query that defines the sales_staffview references only rows in department 5.
Furthermore, the CHECK OPTION creates the view with the constraint (named
sales_staff_cnst) that INSERT and UPDATE statements issued against the view cannot
result in rows that the view cannot select.
View created.
3. Drop View
Experiment 7
Write a Pl/SQL program to print integers from 1 to 10 by using PL/SQL FOR loop
SOLUTION:
PL/SQL Block
SET SERVEROUTPUT ON SIZE 1000000;
DECLARE
n_times NUMBER := 10;
BEGIN
FOR n_i IN 1..n_times LOOP
DBMS_OUTPUT.PUT_LINE(n_i);
END LOOP;
END;
Output Table
Experiment 8
Given the table EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID) write a cursor to
select the five highest paid employees from the table.
SOLUTION:
get e:/p8.sql;
1 declare
2 i number:=0;
3 cursor ec is select empno,name,salary from employee order by gross_salary desc;
4 r ec%rowtype;
5 begin
6 open ec;
7 loop
8 exit when i=5;
9 fetch ec into r;
10 dbms_output.put_line(r.emp_no||' '||r.employee_name||' '||r.salary);
11 i:=i+1;
12 end loop;
13 close ec;
14* end;
15 .
SQL> /
1 rajesh 31000
2 paramesh 15000
3 pushpa 14000
4 vijaya 14000
5 keerthi 13000
Experiment 10
Given an integer i, write a PL/SQL procedure to insert the tuple (i, 'xxx') into a given relation.
SOLUTION:
CREATE TABLE T2 (
a INTEGER,
b CHAR(10));