DBMS Manual
DBMS Manual
DBMS Manual
Table 2: Course
Table 3: Section
Table 4: Grade_report
QUERIES
1|Page
a. Create Tables using create statement
Table 1:-
Table created.
Table 2:-
Table created.
Table 3:-
Table created.
Table 4:-
Table created.
Table 1:-
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Table 3:-
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
3|Page
SQL> insert into section values(135,'CS3380',99,'Lavanya');
1 row created.
6 rows selected.
Table 4:-
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
6 rows selected.
c. Alter table section add new field section and update the records
Update:-
Table altered.
Update :-
1 row updated.
1 row updated.
1 row updated.
5|Page
1 row updated.
1 row updated.
1 row updated.
6 rows selected.
6 rows selected.
4 rows deleted.
6|Page
e. Drop the table section
Table dropped.
7|Page
Activity 2: (Select clause, Arithmetic Operators)
Database: employee
Create Following tables and insert tuples with suitable constraints
EMPLOYEE
EMPID FIRSTANAME LASTNAME Hire_Date ADDRESS CITY
1001 Charan Kumar 11-May-06 83 first street California
1002 Abhi Shek 25-Feb-08 842 Vine Ave Bengaluru
1012 Meher Khanam 12-Sep-05 33 Elm St. California
1015 Naveen Varma 19-Dec-06 11 Red Road California
1016 Pavan Reddy 22-Aug-07 440 MG Road New Delhi
1020 Yogeesh Achar 07-Jun-08 9 Bandra Mumbai
EMPSALARY
EMPID SALARY BENEFITS DESIGNATION
1001 10000 3000 CEO
1002 8000 1200 COO
1012 20000 5000 CFO
1015 6500 1300 Manager
1016 6000 1000 Manager
1020 8000 1200 COO
QUERIES
SQL> create table Employee(empid number(10), firstname varchar(20), lastname varchar(20), Hire_date date,
address varchar(20), city varchar(20), priAbhi key(empid));
Table created
8|Page
To create table “Empsalary”:
SQL> create table Empsalary(eid number(10), salary number, benefits number, designation varchar(20), foreign
key(eid) references Employee(empid));
Table created.
SQL> insert into Employee values(1002,'Abhi', 'Shek', '25-Feb-08','842 Vine Avenue', 'Bengaluru');
1 row created.
SQL> /
Enter value for eid: 1002
Enter value for salary: 8000
Enter value for benefits: 1200
Enter value for designation: COO
1 row created.
SQL>/
Enter value for eid: 1012
Enter value for salary: 20000
9|Page
Enter value for benefits: 5000
Enter value for designation: CFO
1 row created.
SQL> /
Enter value for eid: 1015
Enter value for salary: 6500
Enter value for benefits: 1300
Enter value for designation: Manager
1 row created.
SQL> /
Enter value for eid: 1016
Enter value for salary: 6000
Enter value for benefits: 1000
Enter value for designation: Manager
1 row created.
SQL> /
Enter value for eid: 1020
Enter value for salary: 8000
Enter value for benefits: 1200
Enter value for designation: COO
1 row created.
6 rows selected.
10 | P a g e
1016 6000 1000 Manager
1020 8000 1200 COO
6 rows selected.
Queries:-
FIRSTNAME SALARY
-------------------- ---------
Abhi 8000
Yogeesh 8000
4. To display the FIRSTNAME,LASTNAME and TOTALSALARY of all employees from the table EMPLOYEE
and EMPSALARY. Wher total salary is calculated as SALARY+BENEFITS.
11 | P a g e
FIRSTNAME LASTNAME TOTALSALARY
-------------------- -------------------- -----------
Charan Kumar 13000
Abhi Shek 9200
Meher Khanam 25000
Naveen Varma 7800
Pavan Reddy 7000
Yogeesh Achar 9200
6 rows selected.
4. List the names of employees, who are more than 1 year old in the organization.
FIRSTNAME
--------------------
Charan
Abhi
Meher
Naveen
Pavan
Yogeesh
6 rows selected.
NO_OF_DESIGNATION
-----------------
4
FIRSTNAME
-------------------
Charan
Pavan
Yogeesh
12 | P a g e
SQL> alter table employee add ph_no varchar(10);
Table altered.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
6 rows selected.
SQL> select firstname from employee where hire_date between '16-jun-07' and '15-jun-08';
FIRSTNAME
--------------------
Abhi
Pavan
Yogeesh
6 rows selected.
14 | P a g e
Activity 3: (Logical, Relational Operators)
Database: Library
Create Following tables and insert tuples with suitable constraints
Table: Books
Table : Issued
Book_Id Quantity_Issued
T0001 4
C0001 5
F0001 2
T0002 5
F0002 8
Create table :-
Table 1:-
SQL> create table books (book_id varchar(10), book_name varchar(15), author_name varchar(15), publishers
varchar(10),price number, type varchar(10), quantity number, primary key(book_id));
Table created.
15 | P a g e
Table 2:-
SQL> create table issued(b_id varchar(10), Qty_issued number, foreign key(b_id) references books(book_id));
Table created.
Table 1:-
1 row created.
SQL> /
Enter value for book_id: F0001
Enter value for book_name: The Tears
Enter value for author_name: William Hopkins
Enter value for publishers: First Publ
Enter value for price: 650
Enter value for type: Fiction
Enter value for quantity: 20
1 row created.
SQL> /
Enter value for book_id: T0001
Enter value for book_name: My First C++
Enter value for author_name: Brain & Brooke
Enter value for publishers: ERP
Enter value for price: 350
Enter value for type: Text
Enter value for quantity: 10
16 | P a g e
1 row created.
SQL> /
Enter value for book_id: T0002
Enter value for book_name: C++ Brainworks
Enter value for author_name: A.W.Rossaine
Enter value for publishers: TDH
Enter value for price: 350
Enter value for type: Text
Enter value for quantity: 15
1 row created.
SQL> /
Enter value for book_id: F0002
Enter value for book_name: Thunderbolts
Enter value for author_name: Ana Roberts
Enter value for publishers: First Publ
Enter value for price: 750
Enter value for type: Fiction
Enter value for quantity: 50
1 row created.
Table 2:-
1 row created.
SQL> /
Enter value for b_id: C0001
Enter value for qty_issued: 5
1 row created.
SQL> /
Enter value for b_id: F0001
Enter value for qty_issued: 2
1 row created.
SQL> /
17 | P a g e
Enter value for b_id: T0002
Enter value for qty_issued: 5
1 row created.
SQL> /
Enter value for b_id: F0002
Enter value for qty_issued: 8
1 row created.
B_ID QTY_ISSUED
---------- ----------
T0001 4
C0001 5
F0001 2
T0002 5
F0002 8
Queries:-
1. To show Book name, Author name and price of books of First Publ. publisher
18 | P a g e
2. Display Book id, Book name and publisher of books having quantity more than 8 and price less than
500.
3. Select Book id, book name, author name of books which is published by other than ERP publishers and
price between 300 to 700.
SQL> select book_id,book_name,author_name from books where publishers<>'ERP' and (price between 300 and
700);
4. Generate a Bill with Book_id, Book_name, Publisher, Price, Quantity, 4% of VAT “Total”.
5. Display book details with book id’s C0001, F0001, T0002, F0002 (Hint: use IN operator)
19 | P a g e
T0002 C++ Brainworks A.W.Rossaine TDH 350 Text 15
F0001 The Tears William Hopkins First Publ 650 Fiction 20
C0001 The Klone and I Lata Kappor EPP 355 Novel 5
BOOK_NAME
---------------
The Klone and I
The Tears
My First C++
C++ Brainworks
Thunderbolts
7. Display book details with author name starts with letter ‘A’.
8. Display book details with author name starts with letter ‘T’ and ends with ‘S’.
9. Select BookId, BookName, Author Name , Quantity Issued where Books.BooksId = Issued.BookId.
10. List the book_name, Author_name, Price. In ascending order of Book_name and then on descending
order of price.
20 | P a g e
SQL> select book_name,author_name,price from books order by book_name asc , price desc;
21 | P a g e
Activity 4: (Date Functions)
Database : Lab
Create Following table and insert tuples with suitable constraints
Table : Equipment_Details
No ItemName Costperitem Quantity Dateofpurchase Warranty Operational
1 Computer 30000 9 21/5/07 2 7
2 Printer 5000 3 21/5/06 4 2
3 Scanner 8000 1 29/8/08 3 1
4 Camera 7000 2 13/6/05 1 2
5 UPS 15000 5 21/5/08 1 4
6 Hub 8000 1 31/10/08 2 1
7 Plotter 25000 2 11/1/09 2 2
Create table:-
SQL> create table equip_details(no number,itemname varchar(10),costperitem number,quantity
number,d_o_purchase date,warranty number,operational number,primary key(no));
Table created.
Insert table:-
SQL> insert into equip_details values(&no,'&itemname',&costperitem,&quantity,
'&d_o_purchase',&warranty,&operational);
SQL> /
22 | P a g e
Enter value for no: 2
Enter value for itemname: printer
Enter value for costperitem: 5000
Enter value for quantity: 3
Enter value for d_o_purchase: 21-may-06
Enter value for wqrranty: 4
Enter value for operational: 2
1 row created.
SQL> /
Enter value for no: 3
Enter value for itemname: scanner
Enter value for costperitem: 8000
Enter value for quantity: 1
Enter value for d_o_purchase: 29-aug-08
Enter value for wqrranty: 3
Enter value for operational: 1
1 row created.
SQL> /
Enter value for no: 4
Enter value for itemname: camera
Enter value for costperitem: 7000
Enter value for quantity: 2
Enter value for d_o_purchase: 13-jun-05
Enter value for wqrranty: 1
Enter value for operational: 2
1 row created.
SQL> /
Enter value for no: 5
Enter value for itemname: ups
Enter value for costperitem: 15000
Enter value for quantity: 5
Enter value for d_o_purchase: 21-may-08
Enter value for wqrranty: 1
Enter value for operational: 4
1 row created.
SQL> /
Enter value for no: 6
Enter value for itemname: hub
Enter value for costperitem: 8000
Enter value for quantity: 1
Enter value for d_o_purchase: 31-oct-08
Enter value for wqrranty: 2
23 | P a g e
Enter value for operational: 1
1 row created.
SQL> /
Enter value for no: 7
Enter value for itemname: plotter
Enter value for costperitem: 25000
Enter value for quantity: 2
Enter value for d_o_purchase: 11-jan-09
Enter value for wqrranty: 2
Enter value for operational: 2
1 row created.
7 rows selected.
Queries :-
ITEMNAME
----------
scanner
ups
hub
plotter
7 rows updated.
24 | P a g e
NO ITEMNAME COSTPERITEM QUANTITY D_O_PURCH WARRANTY OPERATION
--------- ---------- ----------- --------- --------- --------- -----------------------------------------------------
1 computer 30000 9 21-MAY-07 2.5 7
2 printer 5000 3 21-MAY-06 4.5 2
3 scanner 8000 1 29-AUG-08 3.5 1
4 camera 7000 2 13-JUN-05 1.5 2
5 ups 15000 5 21-MAY-08 1.5 4
6 hub 8000 1 31-OCT-08 2.5 1
7 plotter 25000 2 11-JAN-09 2.5 2
7 rows selected.
3. Display Itemname , Dateofpurchase and number of months between purchase date and present date.
7 rows selected.
4. To list the ItemName in ascending order of the date of purchase where quantity is more than 3.
SQL> select itemname from equip_details where quantity > 3 order by D_O_Purchase;
ITEMNAME
----------
computer
ups
RECORD_COUNT AVERAGE
------------------------- ----------------
3 14000
25 | P a g e
MIN(WARRANTY) MAX(WARRANTY)
-------------------------- --------------------------
1.5 4.5
DATE_IN_CHARACTERS
---------------------------
Monday 21 May 2007
Sunday 21 May 2006
Friday 29 August 2008
Monday 13 June 2005
Wednesday 21 May 2008
Friday 31 October 2008
Sunday 11 January 2009
7 rows selected.
7 rows selected.
9. To display the next Sunday from the date ’07-JUN-96’
NEXT_SUNDAY_DATE
---------------------------------
08-JUN-96
10. To list the ItemName, which are within the warranty period till present date.
26 | P a g e
Activity : 5 (set operators)
Database : subject
Create Following table and insert tuples with suitable constraints
Table - Physics
Regno Name Year Combination
1441SB01 Abhishek First PCM
1441SB02 Charan Second PMCs
1441SB03 Sonika Third PME
1441SB04 Divya First PMCs
CREATE TABLE:-
TABLE 1:-
Table created.
TABLE 2:-
Table created.
27 | P a g e
INSERT VALUES TO TABLES:-
TABLE 1:-
1 row created.
SQL> /
Enter value for regno: 1441SB02
Enter value for name: Charan
Enter value for year: Second
Enter value for combination: PMCs
1 row created.
SQL> /
Enter value for regno: 1441SB03
Enter value for name: Sonika
Enter value for year: Third
Enter value for combination: PME
1 row created.
SQL> /
Enter value for regno: 1441SB04
Enter value for name: Divya
Enter value for year: First
Enter value for combination: PMCs
1 row created.
TABLE 2:-
1 row created.
SQL> /
Enter value for regno: 1441SB05
Enter value for name: Naveen
Enter value for year: Second
Enter value for combination: BCA
1 row created.
SQL> /
Enter value for regno: 1441SB06
Enter value for name: Pavan
Enter value for year: First
Enter value for combination: BCA
1 row created.
SQL> /
Enter value for regno: 1441SB04
Enter value for name: Divya
Enter value for year: First
Enter value for combination: PMCs
1 row created.
SQL> select name from physics union select name from com_sci;
NAME
----------
Abhishek
Pavan
Divya
Sonika
Charan
Naveen
6 rows selected.
SQL> select name from physics intersect select name from com_sci;
NAME
----------
Divya
Charan
SQL> select * from physics where year='Second' union select * from com_sci where year='Second';
4. Display student those who are studying both physics and computer science in second year
SQL> select name from physics where year='Second' intersect select name from com_sci where year='Second';
no rows selected
30 | P a g e
SQL> select name from physics minus select name from com_sci;
NAME
----------
Abhishek
Sonika
SQL> select name from com_sci minus select name from physics;
NAME
----------
Pavan
Naveen
SQL> select name from physics where combination='PMCs' union select name from com_sci where
combination='PMCs';
NAME
----------
Divya
Charan
SQL> select name from physics where combination='BCA' union select name from com_sci where
combination='BCA';
NAME
----------
Pavan
Naveen
SQL> select name from physics where year='Third' union select name from com_sci where year='Third';
NAME
----------
31 | P a g e
Sonika
Table altered.
32 | P a g e
Activity 6 : (views)
Database: Railway Reservation System
Create Following table and insert tuples with suitable constraints
Table : Availability
Train_no Class Start_Place DESTINATION No_of_seats
n
RJD96 Sleeper Class Banglore Mumbai 15
KRW01 First Class Chennai Mangalore 22
KKE55 First Class AC Bangalore Chennai 15
SHV07 Second Class Hubli Bangalore 8
STB11 Sleeper Class Bangalore Mysore 18
1. Create view sleeper to display train no, start place, DESTINATIONn and perform the following
a. insert new record
b. update DESTINATIONn=’Manglore’ where train no=’RJD96’
c. delete a record which have train no=’KKE55’
2. Create view details to display train no, train name, class
3. Create view total_seats to display train number, start place, use SUM function to no of seats , group by start
place
4. Rename view sleeper to class
5. Delete view details
CREATE TABLE:-
TABLE 1:-
Table created.
TABLE 2:-
TABLE 1:-
1 row created.
SQL> /
Enter value for train_no: KRW01
Enter value for train_name: Karwar Express
Enter value for start_place: Chennai
Enter value for DESTINATIONn: Mangalore
1 row created.
SQL> /
Enter value for train_no: KKE55
Enter value for train_name: Karnataka Express
Enter value for start_place: Bangalore
Enter value for DESTINATIONn: Chennai
1 row created.
SQL> /
Enter value for train_no: SHV07
Enter value for train_name: Shivaji Express
Enter value for start_place: Hubli
Enter value for DESTINATIONn: Bangalore
1 row created.
SQL> /
Enter value for train_no: STB11
Enter value for train_name: Shatabdi Express
Enter value for start_place: Bangalore
Enter value for DESTINATIONn: Mysore
34 | P a g e
1 row created.
TABLE 2:-
1 row created.
SQL> /
Enter value for t_no: KRW01
Enter value for class: First Class
Enter value for strat_place: Chennai
Enter value for DESTINATIONn: Mangalore
Enter value for no_of_seats: 22
1 row created.
SQL> /
Enter value for t_no: KKE55
Enter value for class: First Class AC
Enter value for strat_place: Bangalore
Enter value for DESTINATIONn: Chennai
Enter value for no_of_seats: 15
1 row created.
SQL> /
Enter value for t_no: SHV07
Enter value for class: Second Class
35 | P a g e
Enter value for strat_place: Hubli
Enter value for DESTINATIONn: Bangalore
Enter value for no_of_seats: 8
1 row created.
SQL> /
Enter value for t_no: STB11
Enter value for class: Sleeper Class
Enter value for strat_place: Bangalore
Enter value for DESTINATIONn: Mysore
Enter value for no_of_seats: 18
1 row created.
QUERIES:-
1 row created.
6 rows selected.
1 row updated.
2 rows deleted.
37 | P a g e
SQL> create view details as select train_no,train_name,class from train_details,availability where
train_details.train_no=availability.t_no;
View created.
3. Create view total_seats to display start place, use sum function to no of seats , group by start place
SQL> create view total_seats as select distinct start_place,sum(no_of_seats) as sum from availability group by
start_place;
View created.
START_PLACE sum
--------------- ---------
Bangalore 18
Chennai 22
Hubli 8
Table renamed.
38 | P a g e
5. Delete view details
1 row deleted.
39 | P a g e
Activity 7 : (group by, having clause)
Database: Bank system
Create Following table and insert tuples with suitable constraints
Note:-first create table branch and also while inserting values to tables, first insert into branch table.
Create table:-
Table 1:-
SQL> create table Branch(Branch_ID varchar(15), Branch_Name varchar(15), Branch_City varchar(15), primary
key(Branch_ID));
40 | P a g e
Table created.
Table 2:-
SQL> create table Account(Account_No varchar(15), Cust_Name varchar(15), Branch_ID varchar(15), primary
key(Account_No), foreign key(branch_id) references branch(branch_id));
Table created.
Table 3:-
SQL> create table Depositor(Account_No varchar(15), Branch_ID varchar(15), Balance number, foreign
key(Account_No) references Account(Account_No), foreign key(branch_id) references branch(branch_id));
Table created.
Table 4:-
SQL> create table Loan(Account_No varchar(15), Branch_ID varchar(15), Balance number, foreign
key(Account_No) references Account(Account_No), ), foreign key(branch_id) references branch(branch_id));
Table created.
Insert values:-
Table 1:-
1 row created.
SQL> /
Enter value for branch_id: SB002
Enter value for branch_name: MG Road
Enter value for branch_city: Bangalroe
1 row created.
SQL> /
Enter value for branch_id: SB003
41 | P a g e
Enter value for branch_name: MG Road
Enter value for branch_city: Mysore
1 row created.
SQL> /
Enter value for branch_id: SB004
Enter value for branch_name: Jayanagar
Enter value for branch_city: Mysore
1 row created.
Table 2:-
1 row created.
SQL> /
Enter value for account_no: AE1185698
Enter value for cust_name: Charan
Enter value for branch_id: SB001
1 row created.
SQL> /
Enter value for account_no: AE1203996
Enter value for cust_name: Kumar
Enter value for branch_id: SB004
1 row created.
SQL> /
42 | P a g e
Enter value for account_no: AE1225889
Enter value for cust_name: Meher
Enter value for branch_id: SB002
1 row created.
SQL> /
Enter value for account_no: AE8532166
Enter value for cust_name: Naveen
Enter value for branch_id: SB003
1 row created.
SQL> /
Enter value for account_no: AE8552266
Enter value for cust_name: Pavan
Enter value for branch_id: SB003
1 row created.
SQL> /
Enter value for account_no: AE1003996
Enter value for cust_name: Yogeesh
Enter value for branch_id: SB004
1 row created.
SQL> /
Enter value for account_no: AE1100996
Enter value for cust_name: Prakash
Enter value for branch_id: SB002
1 row created.
8 rows selected.
Table 3:-
1 row created.
SQL> /
Enter value for account_no: AE1203996
Enter value for branch_id: SB004
Enter value for balance: 58900
1 row created.
SQL> /
Enter value for account_no: AE8532166
Enter value for branch_id: SB003
Enter value for balance: 40000
1 row created.
SQL> /
Enter value for account_no: AE1225889
Enter value for branch_id: SB002
Enter value for balance: 150000
1 row created.
Table 4:-
1 row created.
44 | P a g e
SQL> /
Enter value for a_no: AE8552266
Enter value for branch_id: SB003
Enter value for balance: 40000
1 row created.
SQL> /
Enter value for account_no: AE1003996
Enter value for branch_id: SB004
Enter value for balance: 15000
1 row created.
SQL> /
Enter value for account_no: AE1100996
Enter value for branch_id: SB002
Enter value for balance: 100000
1 row created.
QUERIES:-
BRANCH_ID NO_OF_ACC
--------------- ----------------
SB001 1
SB002 3
SB003 2
SB004 2
BID T_LOAN_AMT
----- --------
SB001 102000
SB002 100000
SB003 40000
45 | P a g e
SB004 15000
SQL> Select branch_id,sum(balance) from depositor group by branch_id order by sum(balance) desc;
BRAN_ID SUM(BALANCE)
----------- ------------
SB002 162000
SB004 58900
SB003 40000
6. Display maximum of loan amount in each branch where balance is more than 25000
Branch_ID MAX(BALANCE)
------------- --------
SB001 102000
SB002 100000
SB003 40000
46 | P a g e
7. Display Total Number of accounts present in each city
Branch_CITY NO_OF_ACC
--------------- ---------
Banglore 4
Mysore 4
8 rows selected.
1 row updated.
47 | P a g e
SQL> Select distinct cust_name,branch_name from account,branch where
account.branch_id=branch.branch_id;
CUST_NAME B_NAME
--------------- ---------------
Charan Malleshwaram
Pavan MG Road
Kumar Jayanagar
Abhishek MG Road
Meher MG Road
Yogeesh Jayanagar
Naveen MG Road
Prakash MG Road
8 rows selected.
48 | P a g e