BACHELORS OF SCIENCE IN COMPUTER SCIENCE AND
INFORMATION TECHNOLOGY
IOST, TRIBHUVAN UNIVERSITY
Chardobato, Bhaktapur
Affiliated to Tribhuvan University
Lab Report
Advanced Database
SUBMITTED BY SUBMITTED TO
NAME: Bismita Shrestha. Sumit Bidari
SYMBOL NO/ROLL NO: 24369
PROGRAM: CSIT
8th Semester
Table of Contents
Conver'ng EER model to Rela'onal Model ............................................................................. 3
Object Oriented Database Management System ..................................................................... 6
Map Reduce ............................................................................................................................ 8
Ac've Database Concept ...................................................................................................... 11
Deduc've Database Concepts ............................................................................................... 14
LAB 1
Converting EER model to Relational Model
QUESTION:
Consider the following EER.
Now, write the SQL code to convert EMPLOYEE, DEPARTMENT and PROJECT into
relational model. (Just use CREATE statement)
SOURCE QUERIES FOR EACH TABLE:
1) Employee:
CREATE TABLE EMPLOYEE (
Ssn CHAR(9) PRIMARY KEY,
Fname VARCHAR(50),
Minit CHAR(1),
Lname VARCHAR(50),
Bdate DATE,
Address VARCHAR(100),
Salary DECIMAL(10, 2),
Sex CHAR(1)
);
2) Department:
CREATE TABLE DEPARTMENT (
Number INT PRIMARY KEY,
Name VARCHAR(50),
Locations VARCHAR(100) -- Assuming a single string to store multiple
locations. Alternatively, a separate table could be used for multiple
locations.
);
3) Project:
CREATE TABLE PROJECT (
Number INT PRIMARY KEY,
Name VARCHAR(50),
Location VARCHAR(100)
);
4) Dependant:
CREATE TABLE DEPENDENT (
Dependent_Name VARCHAR(50),
Ssn CHAR(9),
Sex CHAR(1),
Birth_date DATE,
Relationship VARCHAR(25),
PRIMARY KEY (Dependent_Name, Ssn),
FOREIGN KEY (Ssn) REFERENCES EMPLOYEE(Ssn)
);
OUTPUT:
LAB 2
Object Oriented Database Management System
SOURCE CODE
from ZODB import DB, FileStorage
import transaction
from persistent import Persistent
# Define the Student class
class Student(Persistent):
def __init__(self, sid, name):
self.sid = sid
self.name = name
def __str__(self):
return f"{self.sid}, {self.name}"
# Function to create a few students
def create_few_students(root):
student1 = Student(1, "Bismita")
student2 = Student(2, "Bismita Shrestha")
root['students'] = [student1, student2]
transaction.commit()
# Function to print students
def print_students(root):
if 'students' in root:
students = root['students']
print(f"Number of students = {len(students)}\n")
for student in students:
print(student)
else:
print("No students found.")
# Set up the database
storage = FileStorage.FileStorage('student.fs')
db = DB(storage)
connection = db.open()
root = connection.root()
# Create and print students
create_few_students(root)
print_students(root)
# Close the connection
connection.close()
db.close()
OUTPUT
LAB 3
Map Reduce
QUESTION:
In the MapReduce framework, a job reads the text from a set of text files
distributed across a cluster's nodes via the distributed file system. A "mapper"
process converts the input to a set of <key, value> pairs. A "shuffle/sorter"
process sorts these pairs by their key values. A "reducer" process then combines
these sorted pairs in a way that solves a problem, and typically produces a
different set of <key, value> pairs, which is the output of the job.
Create a class MapReduce and perform all the tasks as described above for the
corpus you will be provided by the instructor.
SOURCE CODE:
class MapReduce:
def __init__(self, input_content):
self.input_content = input_content
def split_and_map(self):
# Split input content into lines and words
lines = self.input_content.split("\n")
word_pairs = []
for line in lines:
words = line.split()
for word in words:
word_pairs.append((word, 1))
return word_pairs
def shuffle_and_reduce(self, word_pairs):
# Sort pairs by word (key)
word_pairs.sort(key=lambda x: x[0])
# Reduce: combine counts for each word
result = {}
current_word = None
current_count = 0
for word, count in word_pairs:
if word == current_word:
current_count += count
else:
if current_word:
result[current_word] = current_count
current_word = word
current_count = count
if current_word:
result[current_word] = current_count
return result
if __name__ == "__main__":
input_content = """Deer Bear River
Car Car River
Deer Car Bear"""
mr = MapReduce(input_content)
word_pairs = mr.split_and_map()
word_counts = mr.shuffle_and_reduce(word_pairs)
# Print the final word counts
for word, count in word_counts.items():
print(f"{word}: {count}")
print("interpreted by Bismita Shrestha 24369")
OUTPUT:
LAB 4
Active Database Concept
QUESTION:
• Assume the following tables
Product (BarCode, PName, Price, QuantityInStock)
Sale (SaleID, DeliveryAddress, CreditCard)
SaleItem (SaleID, BarCode, Quantity)
• Create a trigger called updateAvailableQuantity that updates the quantity
in stock in the Product table, for every product sold. The trigger should be
executed after each insert operation on the SaleItem table: for the product
with the given barcode (the one inserted into SaleItem), update the
available quantity in Product table to be the old quantity minus the sold
quantity.
SOURCE QUERY:
create database s_d;
use -- Create the Product table
CREATE TABLE Product (
BarCode VARCHAR(50) PRIMARY KEY,
PName VARCHAR(100),
Price DECIMAL(10, 2),
QuanotyInStock INT
);
-- Create the Sale table
CREATE TABLE Sale (
SaleID INT PRIMARY KEY,
DeliveryAddress VARCHAR(255),
CreditCard VARCHAR(50)
);
-- Create the SaleItem table
CREATE TABLE SaleItem (
SaleID INT,
BarCode VARCHAR(50),
Quanoty INT,
FOREIGN KEY (SaleID) REFERENCES Sale(SaleID),
FOREIGN KEY (BarCode) REFERENCES Product(BarCode)
);
-- Create the trigger to update available quanoty
DELIMITER //
CREATE TRIGGER updateAvailableQuanoty
AFTER INSERT ON SaleItem
FOR EACH ROW
BEGIN
UPDATE Product
SET QuanotyInStock = QuanotyInStock - NEW.Quanoty
WHERE BarCode = NEW.BarCode;
END;
//
DELIMITER ;
-- Insert sample data into Product table
INSERT INTO Product (BarCode, PName, Price, QuanotyInStock)
VALUES ('123', 'Product1', 10.00, 100),
('124', 'Product2', 20.00, 200);
-- Insert sample data into Sale table
INSERT INTO Sale (SaleID, DeliveryAddress, CreditCard)
VALUES (1, '123 Main St', '1234-5678-9012-3456');
-- Select query to check the inioal state of Product table
SELECT * FROM Product;
-- Insert data into SaleItem table to trigger the update
INSERT INTO SaleItem (SaleID, BarCode, Quanoty)
VALUES (1, '123', 5);
-- Select query to check the state of Product table auer sale
SELECT * FROM Product;
OUTPUT:
Before >gger
A@er the update Trigger
LAB 5
Deductive Database Concepts
QUESTION:
Given the following premises find the goal using Prolog
§ Premises
o All oversmart are stupid.
o Children of stupid are naughty.
o Hari is over smart.
o Ram is children of Hari.
§ Goal
o Is Ram naughty?
SOURCE CODE:
# Facts
oversmart_people = ["hari"]
# Stupid individuals are oversmart
def is_stupid(person):
return person in oversmart_people
# Naughty individuals are stupid
def is_naughty(person):
return is_stupid(person)
# Rules
child_of = {"ram": "hari"}
# Query
def is_ram_naughty():
ram = "ram"
return is_naughty(ram)
# Check if Ram is naughty
if is_ram_naughty():
print("Ram is naughty!")
else:
print("Ram is not naughty.")
print("Interpreted / Executed by Bismita Shrestha 24369")
OUTPUT: