0% found this document useful (0 votes)
5 views

PART a dbms-1

The document outlines various database programs including creating E-R diagrams for college and company databases, performing SQL operations such as creating tables, inserting records, and executing queries for an inventory and salary database. It details the steps for altering tables, dropping or truncating them, and managing constraints. Additionally, it includes examples of SQL queries for aggregating data and managing employee records.

Uploaded by

Keerthana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PART a dbms-1

The document outlines various database programs including creating E-R diagrams for college and company databases, performing SQL operations such as creating tables, inserting records, and executing queries for an inventory and salary database. It details the steps for altering tables, dropping or truncating them, and managing constraints. Additionally, it includes examples of SQL queries for aggregating data and managing employee records.

Uploaded by

Keerthana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

PART-A

Program-1

* Draw E-R diagram and convert entities and relationships to relation table for the given
scenario.

SOL:-Consider College and Company Database

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

Tables (With and Without Constraints), Inserting/Updating/Deleting Records in a Table,

Saving (Commit) and Undoing (rollback)

Sol :- Consider the inventory data base with the following table

Product(PID,Number,Name,Text,Price)
Purchase(PO,Number,PID,QTY,)

Steps :-

1. Create database Inventory

2. Create table Product and Purchase with and without constraint

3. View all tables in inventory database

4. Insert five tuples into each relation

5. Display all the tuples in the product and purchase table

6. Update the product name for the PID=40 as CAMERA

7. Delete information about the product whose PID=50

8. Perform saving and undoing

Step1:- Create table Product and Purchase with and without constriant

SQL>CREATE TABLE PRODUCT(PID NUMBER(10)PRIMARY KEY,NAME


VARCHAR2(20)NOTNULL,PRICE NUMBER(8,2));

TABLE CREATED.

SOL>CREATE TABLE PURCHASE(PO NUMBER(10)PRIMARY KEY,PRODUCT_ID


NUMBER(10)REFERENCES PID ,QTY NUMBER(5));

TABLE CREATED.

SQL> DESC PROUDCT;

NAME NULL? TYPE

PID NOTNULL NUMBER10

NAME NOTNULL VARCHAR2(20)

PRICE NUMBER(8,2)

SOL>DESC PURCHASE;

NAME NULL? TYPE


PO NOT NULL NUMBER(10)

PRODUCT_ID NUMBER(10)

QTY NUMBER(5)

Step 2:- Insert five tuples into each relation

SQL>Insert into Product(PID,NAME,PRICE)values(10,’PRINTER’,20000);

1 ROW INSERTED

SQL>Insert into Product(PID,NAME,PRICE)values(20,’KEYBOARD’,20000);

1 ROW INSERTED

SQL>Insert into Product(PID,NAME,PRICE)values(30,’MONITOR’,15000);

1 ROW INSERTED

SQL>Insert into Product(PID,NAME,PRICE)values(40,’TABLE’,25000);

1 ROW INSERTED

SQL>Insert into Product(PID,NAME,PRICE)values(50,’SCANNER’,14000);

1 ROW INSERTED

SQL>Insert into Purchase(PO,PRODUCT_ID,QTY)values(101,10,25);

1 ROW INSERTED

SQL>Insert into Purchase(PO,PRODUCT_ID,QTY)values(102,40,20);

1 ROW INSERTED

SQL>Insert into Purchase(PO,PRODUCT_ID,QTY)values(107,30,40);

1 ROW INSERTED

SQL>Insert into Purchase(PO,PRODUCT_ID,QTY)values(104,40,50);

1 ROW INSERTED

SQL>Insert into Purchase(PO,PRODUCT_ID,QTY)values(105,40,10);


1 ROW INSERTED Step 3:- Display all the tuples in the Product and Purchase table

SQL> SELECT*FROM PRODUCT SQL>SELECT*FROM PURCHASE

PID NAME PRICE PO PRODUCT_ID QTY

10 PRINTER 20000 101 10 25

20 KEYBOAR 20000 102 40 20


D
107 30 40
30 MONITAR 15000
104 40 50
40 TABLE 25000
105 40 10
50 SCANNER 14000

Step 4:- Update the Product name PID=40 as CAMERA

SQL>UPADTE PRODUT SET NAME=’CAMERA’ WHERE PID=40;

1 ROW UPDATED.

SQL>SELECT*FROM PRODUCT;

PID NAME PRICE

10 PRINTER 20000

20 KEYBOAR 20000
D

30 MONITAR 15000

40 CAMERA 25000

50 SCANNER 14000

Step 5:- Delete information about the product whose PID=50

SQL>DELETE FROM PRODUCT WHERE PID=50;

1 ROW DELETED
SQL>SELECT*FROM PRODUCT;

PID NAME PRICE

10 PRINTER 20000

20 KEYBOARD 20000

30 MONITOR 15000

40 CAMERA 25000

Step 6:-Perform Saving and Undoing

SQL>Insert into Proudct(PID,NAME,PRICE)values(50,’mobile’,35000)

1 row inserted

SQL>Insert into Proudct(PID,NAME,PRICE)values(60,’laptop’,70000)

1 row inserted

SQL>COMMIT;

COMMIT COMPLETED

SQL>SELECT*FROM PRODUCT;

PID NAME PRICE

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

1.create Lib table by property specifying the constraint

2.rename the LIB as LIBRARY

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

5.Rename the BID to BOOKID in the library table

6.Change the data type of the column Year_Of _PUblicatiomn as Text with size 15.

7.insert data into Library table

8.Truncate table to delete the records

9.Drop table

Step 1:-create Lib table by properly specifying the constraint .

Sql> CREATE TABLE LIB

2 (BID VARCHAR2(8) PRIMARY KEY,

3 TITLE VARCHAR2(20) NOT NULL,

4 AUTHOR VARCHAR2(20)

5 PUBLICATION VARCHAR2(20)

6 YEAR_OF_PUBLICATION NUMBER(4)

7 );

Name Null? Type


BID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(20)

AUTHOR VARCHAR2(20)

PUBLICATION VARCAHR2(20)

YEAR_OF_PUBLICATION NUMBER(4)

Table created

SQL>DESC LIB;

Step2:Rename the LIB as LIBRARY

SQL>ALTER TABLE LIB RENAME TO LIBRARY ;

Table altered.

SQL>DESC LIBRARY;

NAME NULL? TYPE

BID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(2)

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

SQL>ALTER TABLE LIBRARY ADD PRICE NUMBER(8,2) NOT NULL;

Table altered,

SQL>DESC LIBRARY;

NAME NULL? TYPE

BID NOT NULL VARCHAR2(8)


TITLE NOT NULL VARCHAR2(20)

AUTHOR VARCHAR2(20)

PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION NUMBER(4)

PRICE NOT NULL NUMBER (8,2)

Step4: All the constraints and views that references the column are dropped
automatically along with the column

SQL>ALTER TALBE LIBRARY DROPO COLUMN AUTHOR CASCADE CONSTRAINTS;

Table altered.

SQL> DESC LIBRARY;

NAME NULL? TYPE

BID NOT NULL VARCAHR2(8)

TITLE NOT NULL VARCHAR2(20)

PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION NUMBER(4)

Step 5: rename the BID to9 BOOKID in the library table

SQL>ALTER TABLE LIBRARY RENAME COLUMN BID TO BOOKID;

Table altered

SQL>DESC LIBRARY;

NAME NULL? TYPE

BOOKID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(20)


PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION NUMBER(4)

PRICE NOT NULL NUMBER(8,2)

STEP 6:change the data type of thr column YEAR_OF_PUBLICATION as Text with size
15

SQL>ALTER TABLE LIBRARY MODIFY YEAR)OF_PUBLICATION VARCHAR2(15);

Table altered.

AQL>DESC LIBRARY;

NAME NULL? TYPE

BOOKID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(20)

PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION VARCHAR2(15)

PRICE NOT NULL NUMBER(8,2)

Step 7: insert data into Library table

SQL>INSERT INTO LIBRARY VALUES(‘SP001’,’SKYWARD PUBLISHERS ’,’2022’,300);

1 row inserted

SQL> DESC LIBRARY;

NAME NULL? TYPE

BOOKID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(20)


PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION VARCHAR2(15)

PRICE NOT NULL NUMBER(8,2)

STEP 8; Truncate table to delete the records

SQL>TRUNCATE TABLE LIBRARY;

Table LIBRARY truncated

FOR VERIFICATION

SQL>DESC LIBRARY;

NAME NULL? TYPE

BOOKID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(20)

PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION VARCHAR2(15)

PRICE NOT NULL NUMBER(8,2)

St4ep 9 Drop table

SQL> DROP TABLE LIBRARY;

Table dropped

FOR VERIFICATION

SQL> DESC LIBRARY;

ORA-04043:object LIBRARY does not exist


Program 4: for a given set of relation schemes, create tables and perform the
following simple queries ,Simple Queries with Aggregate functions, Queries with
Aggregate functions (group by and having clause)

Solution

Consider the salary database and execute the following simple queries

SALARYDB(EID:String;NAME:Text ;DEPT:String,DOJ:Date;Salary:Number)

Steps:

1.create table library

2.enter five tuples into the table

3.Display Employee Number and their Salary

4.Find the sum of salaries of all the employees

5.Find the sum and average salaries of employees of a particular department.

6.Find the number of the employees working for each department .

7.Display Employee information in ascending and descending order of their date of


joining .

8.Find the highest salary that an employee draws

9. Find the least salary that an employee draws

10.Display the details of the employee whose name is Rushank and salary is greater
then 50000.

Step1: Create table salary

SQL> CREATE TABLE SALARYDB (ENO VARCAHR2(8) PRIMARY KEY,NAME


VARCHAR2(15) NOT NULL, DEPT VARCAHR2(10),DOJ DATE ,SALARY
NUMBER(10,2));

SQL>DESC SALARYDB;

NAME NULL? TYPE

ENO NOT NULL VARCHAR2(8)


NAME NOT NULL VARCHAR2(15)

DEPT VARCHAR2(10)

DOJ DATE

SALARY NUMBER(10,2)

Step2 : enter five tuples into the table

SQL>INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY);

Enter values for eno:’SC1010’

Enter values for name :’AHANA’

Enter values for dept:’HR’

Enter values for doj :’15-feb-2010’

Enter values for salary:60000

Old 1:INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY)

NEW 1: INSERT INTO SALARYDB VALUES(‘SC1010’,’AHANA’,’HR’,’15-FEB-


2010’,60000)

1 row inserted

SQL>

Enter values for eno:’SC1011’

Enter values for name :’RAMESH’

Enter values for dept:’FINANCE’

Enter values for doj :’10-MAR-2012’

Enter values for salary:45000

Old 1:INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY)

NEW 1: INSERT INTO SALARYDB VALUES(‘SC1011’,’RAMESH’,’FINANCE’,’10-MAR-


2012’,45000);

1 row created
SQL>

Enter values for eno:’SC1013’

Enter values for name :’NAVEEN’

Enter values for dept:’MARKETING ’

Enter values for doj :’8-JAN-2009’

Enter values for salary:55000

Old 1:INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY)

NEW 1: INSERT INTO SALARYDB VALUES(‘SC1013’,’NAVEEN’,’MARKETING’,’8-JAN-


2009’,55000);

1 row created

SQL>

Enter values for eno:’SC1014’

Enter values for name :’ANAGA’

Enter values for dept:’HR ’

Enter values for doj :’14-APR-2012’

Enter values for salary:35000

Old 1:INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY)

NEW 1: INSERT INTO SALARYDB VALUES(‘SC1014’,’ANAGA’,’HR’,’14-APR-


2012’,35000)

1 row created

Enter values for eno:’SC1015’

Enter values for name :’RUSHANK’

Enter values for dept:’ADMIN ’

Enter values for doj :’16-MAY-2011’

Enter values for salary:55000

Old 1:INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY)


NEW 1: INSERT INTO SALARYDB VALUES(‘SC1015’,’RUSHANK’,’ADMIN’,’16-MAY-
2011’,55000)

1 row inserted .

Enter values for eno:’SC1016’

Enter values for name :’RUSHANK’

Enter values for dept:’FINANCE ’

Enter values for doj :’4-JUN-2008’

Enter values for salary:25000

Old 1:INSERT INTO SALARYDB VALUES(&ENO,&NAME ,&DEPT,&DOJ,&SALARY)

NEW 1: INSERT INTO SALARYDB VALUES(‘SC1016’,’RUSHANK’,’FINANCE’,’4-JUN-


2008’,25000)

SQL>SELECT *FROM SALARYDB;

ENO NAME DEPT DOJ SALARY

SC 1010 AHANA HR 15-FEB-10 60000

SC1011 RAMESH FINANCE 10-MAR-12 45000

SC1013 NAVEEN MARKETING 08-JAN-09 55000

SC1014 ANAGA HR 14-APR-10 35000

SC1015 RUSHANK ADMIN 16-MAY-11 55000

SC1016 RUSHANK FEINANCE 04-JUN-08 25000

Step 3 Display Employee Number and their Salary

SQL> SELECT ENO,SALARY FROM SALARYDB;

ENO SALARY

SC 1010 60000

SC1011 45000
SC1013 55000

SC1014 35000

SC1015 55000

SC1016 25000

6 row selected

Step 4: Find the sum of salaries of all the employees

SQL>SELECT SUM(SALARY) AS “TOTAL_SALARY” FROM SALARYDB;

TOTAL_SALARY

275000

Step5 :find the sum and average salaries of the employees of a particular department

SQL> SELECT DEPT,SUM(SALARY) AS “TOTAL_SALARY”,AVG(SALQARY)AS


AVG_SALARY” FROM SALARYDB GROUP BY DEPT;

DEPT TOTAL SALARY AVERAGE SALARY

ADMIN 55000 55000

HR 95000 47500

FINANCE 70000 35000

MARKETING 50000 55000

Step 6: find the number of employees working for each department

SQL>SELECT DEPT,COUNT(*)AS “NUMBER_OF_EMPLOYEES” FROM SALARYDB GROUP


BY DEPT;

DEPT NUMBER_OF_EMPLOYEES

ADMIN 1

HR 2
FINANCE 2

MARKETING 1

Step 7: Display Employee information in ascending and descending order of their


date of joining

SQL> SEWLECT*FROM SALARYDB ORDER BY DOJ ASC;

ENO NAME DEPT DOJ SALARY

SC1016 RUSHANK FINANCE 04-JUN-08 25000

SC1013 NAVEEN MARKETING 08-JAN-09 55000

SC1010 AHANA HR 15-FEB-10 60000

SC1015 RUSHANK ADMIN 16-MAY-11 55000

SC1011 RAMESH FINANCE 10-MAR-10 45000

SC1014 ANAGA HR 14-APR-12 35000

6 row selected

SQL> SELECT *FROM SALARYDB ORDER BY DOJ DESC;

ENO NAME DEPT DOJ SALARY

SC1014 ANAGA HR 14-APR-12 35000

SC1011 RAMESH FINANACE 10-MAR-10 45000

SC1015 RUSHANK ADMIN 16-MAY-11 55000

SC 1010 AHANA HR 15-FEB-10 60000

SC 1013 NAVEEN MARKETING 08-JAN-09 55000

SC1016 RUSHANK FINANCE 14-JUN-08 25000

6 row selected
Step 8:Find the highest salary that an employee draws .

SQL>SELECT MAX(SALARY)AS “HIGHEST_SALARY” FROM SALARYDB

HIGHEST_SALARY

___________

60000

STEP 9:Find the least salary that an employee draws

SQL>SELECT MIN(SALARY)AS “LEAST_SALARY” FROM SALARYDB;

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

2 WHERE NAME=’RUSHANK’ AND SALARY>50000;

ENO NAME DEPT DOJ SALARY

SC1015 RUSHANK ADMIN 16-MAY-11 55000

NOTE TO EXECUTE PROGRAM 5,6,7,8,9&10 CREATE THE FOLLOWING TABLES

consider the company database with following schema

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)

CREATING TABLES BY PROPERLY SPECIFING PRIMARY AND FOREIGN KEYS

SQL< CREATE TABLE DEPT(DNO NUMBER(4) PRIMARY KEY,DNAME VARCHAR2(20)


NOT NULL, DLOCATION VARCHAR2(20));

Table created

SQL>CREATE TABLE EMP(ENO NUMBER(6) PRIMARY KEY,ENAME VARCHAR2(20)


NOT NULL, EBDATE DATE,ADDRESS VARCHAR2(20),GENDER CHAR,SALARY
NUMBER(10) NOT NULL, DEPTNO NUMBER (4)REFERENCES DEPT);

Table created

SQL>DESC DEPT;

NAME NULL? TYPE

DNO NOT NULL NUMBER(4)

DNAME NOT NULL VARCHAR2(20)

DLOCATION VARCHAR2(20)

SQL>DESC EMP

NAME NULL? TYPE

ENO NOT NULL NUMBER(6)

ENAME NOT NULL VARCHAR2(20)

EBDATE DATE

ADDRESS VARCAHR2(20)

GENDER CHAR(1)

SALARY NOT NULL NUMBER(10)

DEPTNO NUMBER(4)
SQL> CREATE TABLE PROJECT (PNO NUMBER(10)PRIMARY KEY,PNAME
VARCHAR(20) NOT NULL ,DNUM NUMBER (4) REFERNCES DEPT);

SQL> DESC PROJECT;

NAME NULL? TYPE

PNO NOT NULL NUMBER(10)

PNAME NOT NULL VARCHAR2(20)

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> DESC WORKS_ON;

NAME NULL? TYPE

ENO NOT NULL NUMBER(6)

PNUM NOT NULL NUMBER(10)

HOURS NOT NULL NUMBER(3,1)

SQL>

INSERT INTO DEPT(DNO,DNAME,DLOCATION)VALUES(2,’ACCOUNTS’,’JAYANAGAR’);

1 row created

INSERT INTO DEPT(DNO.DNAME,DLOCATION)VALUES(4,’RESEARCH’,’KENGERI’);

1row created

INSERT INTO DEPT (DNO,DNAME,DLOCATION)VALUES(5,’ADMIN’,’SOUTHEND’);

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

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1004,’LAKSHMI’,’4-MAR-1998’,’MYSORE’,’F’,55000,4);

1 row created

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1002,’SINCHANA’,’22-DEC-1990’,’MANGLORE’,’F’,50000,2);

1 row created

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1007,’PRASHANT’,’26-JAN-1989’,’DHARWAD’,’M’,20000,4);

1 row created

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1003,’VINAY’,’26-NOV-1990’,’HUBLI’,’M’,30000,2)

1 row created

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1005,’VIDYA’,’26-NOV-1978’,’HUBLI’,’F’,35000,4);

1 row created

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1006,’PRAJWAL’,’2-FEB-1974’,’BANGLORE’,’M’,65000,5);

1 row created

INSERT INTO EMP(ENO,ENAME,EBDATE,ADDRESS,GENDER,SALARY,DEPTNO)

VALUES(1008,’RAJESH’,’2-FEB-2010’,’BANGLORE’,’M’,25000,2);

1 row created

SQL> SELECT *FROM EMP;


ENO ENAME EDATE ADDRESS G SALARY DEPT NO

1001 ANIRUDH 14-JAN-90 BANGALORE M 45000 4

1004 LAKSHMI 04-MAR-98 MYSORE F 55000 4

1002 SINCHANA 22-DEC-90 MANGLORE F 55000 2

1007 PRASHANT 26-JAN-89 DHARWAD M 20000 4

1003 VINAY 29-NOV-90 HUBLI M 30000 2

1005 VIDYA 26-NOV-78 HUBLI F 35000 4

1006 PRAJWAL 02-FEB-74 BANGALORE M 65000 5

1008 RAJESH 02-FEB-10 BANGALORE M 25000 2

8 row selected

SQL> SELECT* FROM DEPT;

DNO DNAME DLOCATION

2 ACCOUNTS JAYANAGAR

4 RESEARCH KENGERI

5 ADMION SOUTHEND

SQL>

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(10,’ERP’,5);

1row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(20,’BANKING’,2);

1 row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(30,’CONNECT_TECH’,4);

1 row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(40,’SMART_SEEK’,5);


1 row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(50,’FINANCE’,2);

1 row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(60,’ANALYTIC A’,4);

1 row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(70,’MARKET_RESEARCH’,4);

1 row created

INSERT INTO PROJECT (PNO,PNAME,DNUM)VALUES(80,’SMART_SEARCH[‘,4);

1 row created

SQL>SELECT*FROM PROJECT;

PNO PNAME DNUM

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>

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1001,10,4.5);

1 row created

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1002,10,6);

1 row created
INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1008,10,4);

1 row created

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1006,20,4);

1 row created

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1004,20,8);

1 row created

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1005,40,8);

1 row created

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1003,50,8);

1 row created

INSERT INTO WORKS_ON(ENO,PNUM,HOURS VALUES(1007,60,5);

1 row created

SQL>SELECT* FROM WORKS_ON;

ENO PNUM HOURS

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

PROGRAM 5:- Execute the following queries


(a)how the resulting salaries if every employee working on the ‘RESEARCH’
departments is given a 10% raise

(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’;

ENO ENAME DNAME INC_SALARY

1001 ANIRUDH RESEARCH 49500

1004 LAKSHMI RESEARCH 60500

1007 PRASHANT RESEARCH 22000

1005 VIDYA RESEARCH 38500

(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>

SELECT MAX(E.SALARY),MIN(E.SALARY),SUM(E.SALARY),AVG(E.SALARY)FROM EMP


E,DEPT D

WHERE E.DEPTNO=D.DNO AND D.DNMAE=’ACCOUNTS’;

MAX(E.SALARY) MIN(E.SALARY) SUM(E.SALARY) AVBG(E.SALARY)

50000 25000 105000 35000

Program 6;-execute the following queries


(a) Retrieve the name of each employee controlled by department number 5(use
EXISTS operator )
(b) Retrieve the name of each dept And number of employees working in each
department which has at least 2 employees

Solution

(a)Retrieve the name of each employee controlled by department number 5(use


EXISTS operator )

SQL> SELECT E.ENAME FROM EMP E

WHERE EXISTS(SELECT D.DNO FROM DEPTNO=D.DNO AND E.DEPTNO=5);

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

Program 7 :- execute the following quaries


(a) For each project retrieve the project number ,the project name ,and the
number of employees who work on that project (use GROUP BY)
(b)Retrieve the name of the employee who born in the year 1990’s

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 ;

PNO PNAME NO_OF_EMP

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

SQL> SELECT ENAME .EBDATE FROM EMP WHERE EBDATE LIK’%-%-90’;

ENAME EBDATE

ANIRUDH 14-JAN-90

SINCHANA 22-DEC-90

VINAY 26-NOV-90

Program 8:- execute the following quaries


For each department that has more than five employees, retrieve the
department number and number of employees who are making salary
more then 40000

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

SQL>SELECT D.DNAME ,D.DNO,COUNT(*)AS “NO_OF_EMP” FROM EMP


E,DEPT D
WHERE E.DEPTNO =D.DNO AND E.SALARY>40000 AND
D.DNO IN(SELECT DEPTNO FROM EMP GROUP BY DEPTNO HAVING COUNT
(*)>2
GROUP BY D.DNO ,D.DNAME;
No row selected

For each department that has more than two employees, retrieve the
department number and number of employees who are making salary
more then 40000

SQL> SELECT D.DNAME ,D.DNO,COUNT (*)FROM EMP E,DEPT D


WHERE E.DEPT NO=D.DNO AND E.SALARY>40000 AND
D.DNO IN(SELECT DEPT NO FROM EMP14 GROUP BY DEPTNO HAVING
COUNT(*)>2)
GROUP BY D.DNO,D.DNAME ;

DNAME DNO COUNT (*)

ACCOUNTS 2 1

RESEARCH 4 2

Program 9 :- for each project on which more than two employee


work ,retrieve the project number ,project name and the number of
employees who work on that projects

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

PNO PNAM NO_OF_EMP_WORKING


E

10 ERP 3

Program 10:-For a given set of relation takes perform the following


creating views (with and without check option) dropping views ,selecting
from a view
Solution

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.

1.Without check option


Step1 :- create view

SQL> CREATE VIEW EMP_DEPT AS(SELECT


E.ENO ,E.ENAME ,E.SALARY ,E.DEPTNO,D.DNAME
FROM EMP E,DEPT D WHERE E.DEPT NO=D.DNO);

View created
Step 2:- Display all the rows of the view
SQL> SELECT *FROM EMP_DEPT ;

ENO ENAME SALARY DEPT NO DNAME

1001 ANIRUDH 45000 4 RESEARCH

1004 LAKSHMI 55000 4 RESEARCH

1002 SINCHANA 50000 2 ACCOUNTS

1007 PRASHANT 20000 4 RESEARCH

1003 VINAY 30000 2 ACCOUNTS

1005 VIDYA 35000 4 RESEARCH

1006 PRAJWAL 65000 5 ADMIN

1008 RAJESH 25000 2 ACCOUNTS


8 row selected

Step 3:- insert records into the views


SQL> INSERT INTO EMP_DEPT (ENO,ENAME ,SALARY,DEPTNO) VALUES
(1009,’SRKANTH’,90000,5);

Step 4:- display all the rows in the views


SQL> SELECT *FROM EMP_DEPT ;

ENO ENAME SALARY DEPT NO DNAME

1001 ANIRUDH 45000 4 RESEARCH

1004 LAKSHMI 55000 4 RESEARCH

1002 SINCHANA 50000 2 ACCOUNTS

1007 PRASHANT 20000 4 RESEARCH

1003 VINAY 30000 2 ACCOUNTS

1005 VIDYA 35000 4 RESEARCH

1006 PRAJWAL 65000 5 ADMIN

1008 RAJESH 25000 2 ACCOUNTS

1009 SRIKANTH 90000 5 ADMIN

9 row selected

Step 5 :- drop view


SQL> DROP VIEW EMP_DEPT;
View dropped

2. with check view


The WITH CHECK OPTION clause in SQL is a very useful clause for views .it
is applicable to ma updateable view .if view is not updateable ,then there is
no meaning of including this clause in the CREATE VIEW statement
The WITH CHECK OPTION clause is used to prevent the insertion of rows in
the view where the condition in the WHERE clause in the CREATE VIEW
statement is not satisfied
If we have the WITH CHECK OPTION clause in the CREATE VIEW statement
and if the UPDATE or INSERT clause does not satisfy thew condition then
they will return an error

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

Step 2:- display all the rows of a view


SQL> SELECT *FROM EMP_VIEW ;

ENO ENAME SALARY

1001 ANIRUDH 45000

1002 SINCHANA 50000

1007 PRASHANT 20000

1003 VINAY 30000

1005 VIDYA 35000

1008 RAJESH 25000

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

Step 4 :- display all rows of a view


SQL>SELECT *FROM EMP_VIEW ;

ENO ENAME SALARY


1001 ANIRUDH 45000

1002 SINCHANA 50000

1007 PRASHANT 20000

1003 VINAY 30000

1005 VIDYA 35000

1008 RAJESH 25000

1011 SNIGDHA 39000

7 rows selected

Step 5:- insert a row where employee salary is greater than 50000. This
gives an error

SQL>INSERT INTO EMP_VIEW(ENO,ENAME ,SALARY)VALUES


(1012,’SMAYAN’,99999);
ORA-01402:view with check option where clause violation

Step6:-drop view
SQL> DROP VIEW EMP_VIEW
View dropped

You might also like