0% found this document useful (0 votes)
25 views17 pages

DBMS Part A 2023

2 semester DBMS

Uploaded by

bhuvanjn37
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)
25 views17 pages

DBMS Part A 2023

2 semester DBMS

Uploaded by

bhuvanjn37
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/ 17

PART-A

PROGRAM-1

Q:Draw E-R diagram for the given scenario.

SOLUTION: 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)

o COLLEGE DATABASE: E-R DIAGRAM


o COMPANY DATABASE:-E R DIAGRAM

PROGRAM-2
Q:Write queries for the following:

Creating user ,Creating a user , Viewing all Tables in a user, Creating Tables (With
and Without Constraints), Inserting/Updating/Deleting Records in a Table, Saving
(Commit) and Undoing (rollback)

Solution: Create following tables

Product(PID,Number,Name,Text,Price)

Purchase(PO,Number,PID,QTY,)

Steps :-

1. Create a user (user name: 3BSC, password: 3bsc)

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 a user (user name: 3BSC, password: 3bsc)

CREATE USER 3BSC identified by 3bsc;

User Created.

GRANT ALL privileges to 3BSC;

Statement processed.

(log out and log in to 3BSC)

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

CREATE TABLE PRODUCT(PID NUMBER(10) PRIMARY KEY,NAME VARCHAR2(20)


NOT NULL,PRICE NUMBER(8,2));

TABLE CREATED.
CREATE TABLE PURCHASE(PO NUMBER(10) PRIMARY KEY,PRODUCT_ID
NUMBER(10) REFERENCES PRODUCT(PID) ,QTY NUMBER(5));

TABLE CREATED.

DESC PRODUCT;

NAME NULL? TYPE

PID NOTNULL NUMBER10

NAME NOTNULL VARCHAR2(20)

PRICE NUMBER(8,2)

DESC PURCHASE;

NAME NULL? TYPE

PO NOT NULL NUMBER(10)

PRODUCT_ID NUMBER(10)

QTY NUMBER(5)

Step 3:-View all tables in inventory database

SELECT TABLE_NAME,STATUS FROM USER_TABLES;

TABLE_NAME STATUS
PRODUCT VALID
PURCHASE VALID

Step 4:- Insert five tuples into each relation

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

1 ROW INSERTED

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

1 ROW INSERTED
Insert into Product(PID,NAME,PRICE)values(30,’MONITOR’,15000);

1 ROW INSERTED

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

1 ROW INSERTED

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

1 ROW INSERTED

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

1 ROW INSERTED

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

1 ROW INSERTED

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

1 ROW INSERTED

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

1 ROW INSERTED

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

1 ROW INSERTED

Step 5:- Display all the tuples in the Product and Purchase table

SELECT * FROM PRODUCT;

PID NAME PRICE

10 PRINTER 20000

20 KEYBOAR 20000
D

30 MONITOR 15000

40 TABLE 25000
50 SCANNER 14000

SELECT * FROM PURCHASE;

PO PRODUCT_ID QTY

101 10 25

102 40 20

107 30 40

104 40 50

105 40 10

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

UPDATE PRODUCT SET NAME=’CAMERA’ WHERE PID=40;

1 ROW UPDATED.

SELECT * FROM PRODUCT;

PID NAME PRICE

10 PRINTER 20000

20 KEYBOAR 20000
D

30 MONITOR 15000

40 CAMERA 25000

50 SCANNER 14000

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

DELETE FROM PRODUCT WHERE PID=50;

1 ROW DELETED

SELECT * FROM PRODUCT;


PID NAME PRICE

10 PRINTER 20000

20 KEYBOARD 20000

30 MONITOR 15000

40 CAMERA 25000

Step 8:-Perform Saving and Undoing

COMMIT;

Statement processed.

Insert into PRODUCT(PID,NAME,PRICE)values(50,’mobile’,35000);

1 row inserted

Insert into PRODUCT(PID,NAME,PRICE)values(60,’laptop’,70000);

1 row inserted

SELECT * FROM PRODUCT;

PID NAME PRICE

10 PRINTER 20000

20 KEYBOARD 20000

30 MONITOR 15000

40 CAMERA 25000

50 MOBILE 35000

60 LAPTOP 70000

ROLLBACK;

Statement processed.
Select * from product;

PID NAME PRICE

10 PRINTER 20000

20 KEYBOARD 20000

30 MONITOR 15000

40 CAMERA 25000

PROGRAM 3 :- Alter a table,dropping/Truncating/Renaming a table,Backing


up/restoring a database

Solution: 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 properly 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 _Publication 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 .


CREATE TABLE LIB(BID VARCHAR2(8) PRIMARY KEY,TITLE VARCHAR2(20) NOT
NULL,AUTHOR VARCHAR2(20), PUBLICATION VARCHAR2(20),
YEAR_OF_PUBLICATION NUMBER(4) );

Table created.

DESC LIB;

Name Null? Type

BID NOT NULL VARCHAR2(8)

TITLE NOT NULL VARCHAR2(20)

AUTHOR VARCHAR2(20)

PUBLICATION VARCAHR2(20)

YEAR_OF_PUBLICATION NUMBER(4)

Step2:-Rename the LIB as LIBRARY

ALTER TABLE LIB RENAME TO LIBRARY ;

Table altered.

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 Price with Not Null constraint to the existing table library

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


Table altered,

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)

Step 4:- All the constraints and views that references the column are dropped
automatically along with the column

ALTER TABLE LIBRARY DROP COLUMN AUTHOR CASCADE CONSTRAINTS;

Table altered.

DESC LIBRARY;

NAME NULL? TYPE

BID NOT NULL VARCAHR2(8)

TITLE NOT NULL VARCHAR2(20)

PUBLICATION VARCHAR2(20)

YEAR_OF_PUBLICATION NUMBER(4)

PRICE NOT NULL NUMBER(8,2)

Step 5:- rename the BID to BOOKID in the library table

ALTER TABLE LIBRARY RENAME COLUMN BID TO BOOKID;

Table altered.
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 the column YEAR_OF_PUBLICATION as Text with size 15

ALTER TABLE LIBRARY MODIFY YEAR_OF_PUBLICATION VARCHAR2(15);

Table altered.

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

INSERT INTO LIBRARY VALUES(‘SP001’,’DBMS’,’SKYWARD_PUBLISHERS’,’2022’,300);

1 row inserted

SELECT * FROM LIBRARY;

BOOKID TITLE PUBLICATION YEAR_OF_PUBLICATION PRICE


SP001 DBMS SKYWARD_PUBLISHERS 2022 300

STEP 8:- Truncate table to delete the records

TRUNCATE TABLE LIBRARY;

Table LIBRARY truncated.

FOR VERIFICATION

SELECT * FROM LIBRARY;

No data found

Step 9:- Drop table

DROP TABLE LIBRARY;

Table dropped

FOR VERIFICATION

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 SALARYDB

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 than
50000.

Step1:- Create table salary

CREATE TABLE SALARYDB (ENO VARCHAR2(8) PRIMARY KEY,NAME


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

Table created.

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

INSERT INTO SALARYDB VALUES(‘SC1010’,’AHANA’,’HR’,’15-FEB-2010’,60000);

1 row inserted

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


2012’,45000);
1 row created

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


2009’,55000);

1 row created

INSERT INTO SALARYDB VALUES(‘SC1014’,’ANAGA’,’HR’,’14-APR-2012’,35000);

1 row created

INSERT INTO SALARYDB VALUES(‘SC1015’,’RUSHANK’,’ADMIN’,’16-MAY-


2011’,55000);

1 row inserted .

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


2008’,25000);

1 row inserted.

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 FINANCE 04-JUN-08 25000

Step 3 Display Employee Number and their Salary

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

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

SELECT DEPT,SUM(SALARY) AS “TOTAL_SALARY”,AVG(SALARY)AS AVG_SALARY”


FROM SALARYDB GROUP BY DEPT;

DEPT TOTAL SALARY AVERAGE SALARY

ADMIN 55000 55000

HR 95000 47500

FINANCE 70000 35000

MARKETING 55000 55000

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

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

SELECT * 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-12 45000

SC1014 ANAGA HR 14-APR-12 35000

6 row selected

SELECT * FROM SALARYDB ORDER BY DOJ DESC;

ENO NAME DEPT DOJ SALARY

SC1014 ANAGA HR 14-APR-12 35000

SC1011 RAMESH FINANACE 10-MAR-12 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 .

SELECT MAX(SALARY)AS “HIGHEST_SALARY” FROM SALARYDB;

HIGHEST_SALARY

___________

60000

STEP 9:Find the least salary that an employee draws

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

SELECT*FROM SALARYDB WHERE NAME=’RUSHANK’ AND SALARY>50000;

ENO NAME DEPT DOJ SALARY

SC1015 RUSHANK ADMIN 16-MAY-11 55000

You might also like