PART a dbms-1
PART a dbms-1
Program-1
* Draw E-R diagram and convert entities and relationships to relation table for the given
scenario.
1.COLLEGE DATABASE:
STUDENT (USN,Sname,Address,Phone,Gender)
SEMSEC(SSID,Sem,Sec)
CLASS(USN,SSID)
SUBJECT(Subcode,Title,Sem,Credits)
IA MARKS(USN,Subcode,SSID,Test1,Test2,Test3,Final IA)
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)
COLLEGE DATABASE: E-R DIAGRAM
COMPANY DATABASE:-E R DIAGRAM
Program-2
*a. Viewing all databases, Creating a Database, Viewing all Tables in a Database, Creating
Sol :- Consider the inventory data base with the following table
Product(PID,Number,Name,Text,Price)
Purchase(PO,Number,PID,QTY,)
Steps :-
Step1:- Create table Product and Purchase with and without constriant
TABLE CREATED.
TABLE CREATED.
PRICE NUMBER(8,2)
SOL>DESC PURCHASE;
PRODUCT_ID NUMBER(10)
QTY NUMBER(5)
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW INSERTED
1 ROW UPDATED.
SQL>SELECT*FROM PRODUCT;
10 PRINTER 20000
20 KEYBOAR 20000
D
30 MONITAR 15000
40 CAMERA 25000
50 SCANNER 14000
1 ROW DELETED
SQL>SELECT*FROM PRODUCT;
10 PRINTER 20000
20 KEYBOARD 20000
30 MONITOR 15000
40 CAMERA 25000
1 row inserted
1 row inserted
SQL>COMMIT;
COMMIT COMPLETED
SQL>SELECT*FROM PRODUCT;
10 PRINTER 20000
20 KEYBOARD 20000
30 MONITAR 15000
40 CAMERA 25000
50 MOBILE 35000
60 LAPTOP 70000
PROGRAM 3 :- Alter a table,dropping/Truncating/Renaming a table,Backing
up/restornig a database
SOL:-Consider the library database with the following data and execute the queries
Lib(BID:Number,Title:Text,Publication:Text;Year_Of_Publication:Text)
Steps
3.Add a new column Price with Not Null constraint to the existing table library
4.All the constraints and the views that references the coloumn are dropped
automatically along with the column
6.Change the data type of the column Year_Of _PUblicatiomn as Text with size 15.
9.Drop table
4 AUTHOR VARCHAR2(20)
5 PUBLICATION VARCHAR2(20)
6 YEAR_OF_PUBLICATION NUMBER(4)
7 );
AUTHOR VARCHAR2(20)
PUBLICATION VARCAHR2(20)
YEAR_OF_PUBLICATION NUMBER(4)
Table created
SQL>DESC LIB;
Table altered.
SQL>DESC LIBRARY;
AUTHOR VARCAHR2(20)
PUBLICATION VARCHAR2(20)
YEAR_OF_PUBLICATION NUMBER(4)
Step 3: add a new column Pricw with Not Null constraint to the existing table library
Table altered,
SQL>DESC LIBRARY;
AUTHOR VARCHAR2(20)
PUBLICATION VARCHAR2(20)
YEAR_OF_PUBLICATION NUMBER(4)
Step4: All the constraints and views that references the column are dropped
automatically along with the column
Table altered.
PUBLICATION VARCHAR2(20)
YEAR_OF_PUBLICATION NUMBER(4)
Table altered
SQL>DESC LIBRARY;
YEAR_OF_PUBLICATION NUMBER(4)
STEP 6:change the data type of thr column YEAR_OF_PUBLICATION as Text with size
15
Table altered.
AQL>DESC LIBRARY;
PUBLICATION VARCHAR2(20)
YEAR_OF_PUBLICATION VARCHAR2(15)
1 row inserted
YEAR_OF_PUBLICATION VARCHAR2(15)
FOR VERIFICATION
SQL>DESC LIBRARY;
PUBLICATION VARCHAR2(20)
YEAR_OF_PUBLICATION VARCHAR2(15)
Table dropped
FOR VERIFICATION
Solution
Consider the salary database and execute the following simple queries
SALARYDB(EID:String;NAME:Text ;DEPT:String,DOJ:Date;Salary:Number)
Steps:
10.Display the details of the employee whose name is Rushank and salary is greater
then 50000.
SQL>DESC SALARYDB;
DEPT VARCHAR2(10)
DOJ DATE
SALARY NUMBER(10,2)
1 row inserted
SQL>
1 row created
SQL>
1 row created
SQL>
1 row created
1 row inserted .
ENO SALARY
SC 1010 60000
SC1011 45000
SC1013 55000
SC1014 35000
SC1015 55000
SC1016 25000
6 row selected
TOTAL_SALARY
275000
Step5 :find the sum and average salaries of the employees of a particular department
HR 95000 47500
DEPT NUMBER_OF_EMPLOYEES
ADMIN 1
HR 2
FINANCE 2
MARKETING 1
6 row selected
6 row selected
Step 8:Find the highest salary that an employee draws .
HIGHEST_SALARY
___________
60000
LEAST_SALARY
_____________
25000
STEP 10: Display the details of the employee whose name is rushank and salary is
greater than 50000
SQL>SELECT*FROM SALARYDB
EMP(ENO:Number;ENAME;String;EDATE:Date;ADDRESS:Text;SALARY:Number;DEPT
NO;Number)
DEPT(DNO:Number;DNAME;String;DLOCATION;String)
PROJECT(PNO:Number;PNAME:String;DNUM;Number)
WORKS_ON(ENO;Number;PNUM;Number;HOURS:Number)
Table created
Table created
SQL>DESC DEPT;
DLOCATION VARCHAR2(20)
SQL>DESC EMP
EBDATE DATE
ADDRESS VARCAHR2(20)
GENDER CHAR(1)
DEPTNO NUMBER(4)
SQL> CREATE TABLE PROJECT (PNO NUMBER(10)PRIMARY KEY,PNAME
VARCHAR(20) NOT NULL ,DNUM NUMBER (4) REFERNCES DEPT);
DNUM NUMBER(4)
SQL> CREATE TABLE WORKS_ON (ENO NUMBER (6) REFERENCES EMP NOT NULL,
PNUM NUMBER (10) REFERENCES PROJECT NOT NULL,HOURS NUMBER
(3,1) NOT NULL, PRIMARY KEY(ENO,PNUM));
Table created
SQL>
1 row created
1row created
1 row created
SQL>
INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)
VALUES(1001,’ANIRUDH’,’14-NOV-1990’,’BANGLORE’,’M’,45000,4);
1 row created
VALUES(1004,’LAKSHMI’,’4-MAR-1998’,’MYSORE’,’F’,55000,4);
1 row created
VALUES(1002,’SINCHANA’,’22-DEC-1990’,’MANGLORE’,’F’,50000,2);
1 row created
VALUES(1007,’PRASHANT’,’26-JAN-1989’,’DHARWAD’,’M’,20000,4);
1 row created
VALUES(1003,’VINAY’,’26-NOV-1990’,’HUBLI’,’M’,30000,2)
1 row created
VALUES(1005,’VIDYA’,’26-NOV-1978’,’HUBLI’,’F’,35000,4);
1 row created
VALUES(1006,’PRAJWAL’,’2-FEB-1974’,’BANGLORE’,’M’,65000,5);
1 row created
VALUES(1008,’RAJESH’,’2-FEB-2010’,’BANGLORE’,’M’,25000,2);
1 row created
8 row selected
2 ACCOUNTS JAYANAGAR
4 RESEARCH KENGERI
5 ADMION SOUTHEND
SQL>
1row created
1 row created
1 row created
1 row created
1 row created
1 row created
1 row created
SQL>SELECT*FROM PROJECT;
10 ERP 5
20 BANKING 2
30 CONNECT_TECH 4
40 SMART_SEEK 5
50 FINANACE 2
60 ANALYTICA 4
70 MARKET_RESEARCH 4
80 SMART_SEARCH5 4
SQL>
1 row created
1 row created
INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1008,10,4);
1 row created
1 row created
1 row created
1 row created
1 row created
1 row created
1001 10 4.5
1002 10 6
1008 10 4
1006 20 4
1004 20 8
1005 40 8
1003 50 8
1007 60 5
(b)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
Solution
(a) how the resulting salaries if every employee working on the ‘RESEARCH’
departments is given a 10% raise
SQL> SELECT E.ENO,E.ENAME,D.DNAME ,1.1*E.SALARY AS “INC_SALARY” FROM
EMP E,DEPT D
WHERE E.DEPTNO=D.DNO AND D.DNAME =’RESEARCH’;
(b) 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>
Solution
ENAME
PRAJWAL
(b) Retrieve the name of each dept And number of employees working in each
department which has at least 2 employees
SQL>
SELECT D.DNAME ,COUNT(*)FROM EMP E,DEPT D
WHERE E.DEPTNO=D.DNO GROUP BY D.DNAME HAVING COUNT (*)>=2;
DNAME COUNT(*)
ACCOUNTS 3
RESEARCH 4
Solution
(a)For each project retrieve the project number ,the project name ,and the
number of employees who work on that project (use GROUP BY)
SQL> SELECT P.PNO,P.PNAME ,COUNT(*)AS “NO _OF_EMP FROM PROJECT
P ,WORKS_ON W WHERE P.PNO=W.PNUM GROUP BY P.PNO,P.PNAME ;
10 ERP 3
60 ANALYTICA 1
20 BANKING 2
40 SMART_SEEK 1
50 FINANCE 1
(b)Retrieve the name of the employee who born in the year 1990’s
ENAME EBDATE
ANIRUDH 14-JAN-90
SINCHANA 22-DEC-90
VINAY 26-NOV-90
Solution
(1) For each department that has more than five employees, retrieve the
department number and number of employees who are making salary
more then 40000
For each department that has more than two employees, retrieve the
department number and number of employees who are making salary
more then 40000
ACCOUNTS 2 1
RESEARCH 4 2
Solution
SQL>
SELECT P.PNO,P.PNAME,COUNT(*) AS “NO_OF_EMP_WORKING” FROM
PROJECT P,WORKS_ON W
WHERE P.PNO=W.PNUM GROUP BY P.PNO ,P.PNAME HYAVING COUNT(*)>2
10 ERP 3
What is view ?
In oracle ,view is a virtual table that does not physically exist in oracle data
dictionary and do not store any data .it can be executed when called A
view is created by a query joining one or more tables .views are used for
security purpose because they provide encapsulations of the name of the
table .data is in the virtual table ,not stored permanently .views display
only selected data .we can also use join in the select statement in deriving
the data for the view
Note :-
Let us consider the table emp and dept table created for program number
5,6,7,8 and 9 we will create the view on both emp and dept table.
View created
Step 2:- Display all the rows of the view
SQL> SELECT *FROM EMP_DEPT ;
9 row selected
Step 1 let us create simple view non emp table with check option of salary
less than 50000 in where condition
SQL>CREATE VIEW EMP_VIEW AS
2 (SELECT ENO,ENAME ,SALARY FROM EMP WHERE SALARY
<=50000)WITH CHECK OPTION ;
View EMP view
6 row selected
Step 3:- insert a row where employee salary is less than 50000
SQL> INSERT INTO EMP_VIEW (ENO ,ENAME ,SALARY)VALUES
(1011,’SNIGDHA’,39000);
1 row inserted
7 rows selected
Step 5:- insert a row where employee salary is greater than 50000. This
gives an error
Step6:-drop view
SQL> DROP VIEW EMP_VIEW
View dropped