Dbms Practical Codes
Dbms Practical Codes
• MongoDB Installation:-
1.sudo apt install Mongodb-server
2.sudo apt install Mongodb-clients
Telegram Channel
https://t.me/SPPU_TE_BE_COMP
(for all engineering Resources)
WhatsApp Channel
(for all tech updates)
https://whatsapp.com/channel/0029ValjFriICVfpcV9HFc3b
Insta Page
(for all engg & tech updates)
https://www.instagram.com/sppu_engineering_update
Assignment No.- 2
2(A)
Design and Develop SQL DDL statements which demonstrate the
use of SQL objects such as Table, View, Index, Sequence, Synonym, different
constraints etc.
Ans.
-- Truncate the emp_info table, removing all data but keeping the structure
TRUNCATE TABLE emp_info;
-- Update a record in emp_details and check that it affects the view as well
UPDATE emp_details SET emp_dept = 'coding' WHERE emp_name =
'Mohan';
SELECT * FROM emp_details;
-- Note:
-- MySQL does not support SEQUENCE or SYNONYM objects, which are
available in other RDBMS like Oracle.
-- In MySQL, you can auto-increment fields to mimic sequences.
-- MySQL does not support synonyms, but we use aliases in SELECT
statements for similar functionality.
Practical- 2(B)
Write at least 10 SQL queries on the suitable database application using SQL DML
statements.
Ans.
-- 13. Create a new table 'stud_info' based on selected columns from 'stud_tab'
CREATE TABLE stud_info AS SELECT stud_id, stud_name FROM stud_tab;
-- 14. Use UNION to combine 'stud_id' from both tables and remove duplicates
SELECT stud_id FROM stud_tab UNION SELECT stud_id FROM stud_info;
-- 15. Group by 'stud_dept' and calculate the average 'stud_fees' for each department
SELECT stud_dept, AVG(stud_fees) AS avg_fees FROM stud_tab GROUP BY stud_dept;
-- 17. Order students by 'stud_fees' in descending order to see the highest paying students
first
SELECT * FROM stud_tab ORDER BY stud_fees DESC;
Practical No- 3
SQL Queries - all types of Join, Sub-Query and View: Write at least 10 SQL
queries for suitable database application using SQL DML statements.
Ans.
-- 1. INNER JOIN: Get matching rows between capital and state tables based
on cap_no and state_no
SELECT capital.cap_no, state.state_no
FROM capital
INNER JOIN state ON capital.cap_no = state.state_no;
-- 3. INNER JOIN: Check the updated join between capital and state
SELECT capital.cap_no, state.state_no
FROM capital
INNER JOIN state ON capital.cap_no = state.state_no;
-- 4. LEFT JOIN: Show all capitals, including those without a matching state
SELECT capital.cap_no, state.state_no
FROM capital
LEFT JOIN state ON capital.cap_no = state.state_no;
-- 6. RIGHT JOIN: Show all states, including those without a matching capital
SELECT capital.cap_no, state.state_no
FROM capital
RIGHT JOIN state ON capital.cap_no = state.state_no;
-- 7. INNER JOIN with multiple columns: Retrieve details from both tables
based on matching state_no
SELECT capital.cap_no, capital.cap_name, state.capital, state.state_no
FROM capital
INNER JOIN state ON capital.cap_no = state.state_no;
-- 8. LEFT JOIN: Show all capitals with their state details, if available
SELECT capital.cap_no, capital.cap_name, state.capital, state.state_no
FROM capital
LEFT JOIN state ON capital.cap_no = state.state_no;
-- 9. RIGHT JOIN with UNION: Combine LEFT and RIGHT JOIN results to
simulate a FULL OUTER JOIN
SELECT capital.cap_no, capital.cap_name, state.capital, state.state_no
FROM capital
LEFT JOIN state ON capital.cap_no = state.state_no
UNION
SELECT capital.cap_no, capital.cap_name, state.capital, state.state_no
FROM capital
RIGHT JOIN state ON capital.cap_no = state.state_no;
-- 10. CROSS JOIN: Display all possible combinations of capital and state
records
SELECT * FROM capital c1, state s1;
-- 11. CROSS JOIN with a condition: Show pairs with different cap_no and
state_no
SELECT * FROM capital c1, state s1 WHERE c1.cap_no != s1.state_no;
-- 14. Sub-query using capital: Find states by matching capital with a capital's
state_no
SELECT * FROM state WHERE state_no = (SELECT state_no FROM capital
WHERE cap_name = 'MH');
SELECT * FROM state WHERE state_no = (SELECT state_no FROM capital
WHERE cap_name = 'GUJ');
SELECT * FROM state WHERE state_no = (SELECT state_no FROM capital
WHERE cap_name = 'RAJ');
SELECT * FROM state WHERE state_no = (SELECT state_no FROM capital
WHERE cap_name = 'KAR');
Practical 4-
Write a PL/SQL code block to calculate ne for a library book by accessing borrower
information from the database.
Ans.
-- Create the stored procedure `B` to calculate nes based on overdue days
CREATE PROCEDURE B (roll_new INT, book_name VARCHAR(20))
BEGIN
DECLARE X INT; -- Variable to hold the number of overdue days
DECLARE CONTINUE HANDLER FOR NOT FOUND BEGIN
SELECT 'Borrower Not Found' AS Message;
END;
Practical - 5:
Write a PL/SQL code block to study and implement stored procedure
and function for displaying the result of student based on the grade
obtained.
Ans.
Ans.
CALL cursor_proc_p2(5);
SELECT * FROM N_RollCall; -- Check results after calling cursor_proc_p2(5)
CALL cursor_proc_p2(3);
SELECT * FROM N_RollCall; -- Check results after calling cursor_proc_p2(3)
Practical 7-
Write a PL/SQL code block to create triggers on the Library table to keep
track of updating and deletion of records.
Ans.
DELIMITER ;
Ans.
package MySQLConnectivity;
import java.sql.*;
import java.util.Scanner;
// Insertion
System.out.print("Enter the number of records you want to insert: ");
n = in.nextInt();
for (int i = 0; i < n; i++) {
System.out.print("\nData" + (i + 1) + "\nEnter Sno: ");
sno = in.nextInt();
pstmInsert.setInt(1, sno);
System.out.print("Enter Name: ");
name = in.next();
pstmInsert.setString(2, name);
System.out.print("Enter Telephone: ");
telephone = in.next();
pstmInsert.setString(3, telephone);
System.out.print("Enter Gender: ");
gender = in.next();
pstmInsert.setString(4, gender);
pstmInsert.executeUpdate();
}
// Search operation
System.out.print("Enter Sno to search: ");
sno = in.nextInt();
searchRecord(stmt, sno);
// Update operation
System.out.print("Enter Sno to update: ");
sno = in.nextInt();
System.out.print("Enter new Name: ");
name = in.next();
System.out.print("Enter new Telephone: ");
telephone = in.next();
System.out.print("Enter new Gender: ");
gender = in.next();
pstmUpdate.setString(1, name);
pstmUpdate.setString(2, telephone);
pstmUpdate.setString(3, gender);
pstmUpdate.setInt(4, sno);
pstmUpdate.executeUpdate();
// Deletion
System.out.print("Enter the number of records you want to delete: ");
n = in.nextInt();
for (int i = 0; i < n; i++) {
System.out.print("\nData" + (i + 1) + "\nEnter Sno: ");
sno = in.nextInt();
pstmDelete.setInt(1, sno);
pstmDelete.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
} nally {
in.close();
fi
}
}
Ans.
Practical No- 10
MongoDB - Aggregation and Indexing:
Design and Develop MongoDB Queries using aggregation and indexing with
suitable example using MongoDB.
Ans.
// Create an index on the 'Item_id' eld (index values should be 1 for ascending or -1 for
descending)
db.cust_table.createIndex({'Item_id': 1});
// You should only create a unique index or another index if necessary, using valid eld
values.
// Create another index if needed (for example on 'Cust_Name')
db.cust_table.createIndex({'Cust_Name': 1});
// If you want to drop an index, you should specify the name, not the eld itself.
// Example of dropping the index
db.cust_table.dropIndex("Item_id_1"); // Use the index name shown in getIndexes()
Ans.
show dbs
use bill
db.pay.insert({Cust_ID:"A123",Product:"Milk",Amount:40,Status:"P"});
db.pay.insert({Cust_ID:"A123",Product:"Parle_G",Amount:50,Status:"NP"});
db.pay.insert({Cust_ID:"A123",Product:"Lays Chips",Amount:40,Status:"P"});
db.pay.insert({Cust_ID:"B123",Product:"Mentos",Amount:10,Status:"P"});
db.pay.insert({Cust_ID:"B123",Product:"Maggie",Amount:60,Status:"NP"});
db.pay. nd() // Optional, to see inserted documents
Practical No -12
Database Connectivity:
Write a program to implement MongoDB database connectivity with any front end
language to implement Database navigation operations (add, delete, edit etc.)
Ans
package MongoDB;
import java.util.Scanner;
import com.mongodb.client.*;
import org.bson.Document;
while (ans.equalsIgnoreCase("y")) {
MongoDatabase database = mongoClient.getDatabase("Info");
MongoCollection<Document> collection = database.getCollection("Personal");
collection.insertOne(info);
}
System.out.println("Insert Operation");
displayData(collection, "Name");
displayData(collection, "Mobile Number");
displayData(collection, "Age");
System.out.println("Delete Operation");
displayData(collection, "Name");
displayData(collection, "Mobile Number");
displayData(collection, "Age");
sc.close();
mongoClient.close();
}
}