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

Sample 1

The document outlines the creation and optimization of a relational database for a college, including tasks such as creating tables, inserting data, retrieving metadata, and optimizing queries using indexing. It emphasizes the use of a query optimizer to enhance data retrieval efficiency and includes a step-by-step process for implementing these tasks. Additionally, it discusses the creation of a distributed database system with similar operations across multiple databases, focusing on data aggregation and handling duplicates.
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)
9 views

Sample 1

The document outlines the creation and optimization of a relational database for a college, including tasks such as creating tables, inserting data, retrieving metadata, and optimizing queries using indexing. It emphasizes the use of a query optimizer to enhance data retrieval efficiency and includes a step-by-step process for implementing these tasks. Additionally, it discusses the creation of a distributed database system with similar operations across multiple databases, focusing on data aggregation and handling duplicates.
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/ 79

No.

: 1

QUERY OPTIMIZER

Implement query with optimizer by accessing the meta data.

PROBLEM STATEMENT:

You are given a relational database containing information about a college. Create and insert
the values into the ` database includes tables such as Students, Courses, Enrollments,
Departments, and Professors. Write optimized SQL queries to retrieve data efficiently.

Tasks:

1. Create and Insert the values into the database. And display the values from the
database.
2. Retrieve Table Metadata:
o Write SQL queries to fetch metadata about the Students table, including
column names, data types, and constraints.
o Based on the metadata, identify which columns are indexed and which are not.

Optimize a Query Using Metadata:

 You need to fetch the names of all students enrolled in a specific course. The course is
identified by its CourseID.
 Check if there is an index on the CourseID column in the Enrollments table. If not,
create an index on CourseID.
 Write an optimized query to fetch the student names.

Evaluate Query Performance:

 Use EXPLAIN or a similar tool to analyze the performance of the query before and
after indexing. Record the changes in execution time or cost.

INTRODUCTION: "Query with optimizer by accessing the metadata" refers to the process
in which a database management system (DBMS) uses an internal component called a query
optimizer to determine the most efficient way to execute a database query. The optimizer
makes decisions based on metadata, which is data that provides information about the
structure and characteristics of the database.

Breaking Down the Terms:

1. Query:
o A request to retrieve or manipulate data stored in a database. For example, a
SQL query might request all records from a table where a certain condition is
met.
2. Optimizer:
o A software component within the DBMS that analyzes different ways to
execute a query. It considers various strategies or "execution plans" to find the
one that will complete the task most efficiently, often measured in terms of
time and resource usage (like CPU, memory, and I/O operations).
3. Metadata:
o Data about the data stored in the database. This includes information like:
 The size of tables.
 The number of rows in a table.
 The distribution of values within columns.
 The presence and type of indexes.
 Data types and constraints on columns.
o Metadata helps the optimizer understand the characteristics of the data, which
is crucial for making informed decisions about the best way to execute a
query.

OBJECTIVES:

The objective is to create and insert values into the database, retrieve metadata, optimize
queries using metadata, and evaluate query performance to ensure efficient data retrieval.

APPLICATIONS:

 Relational Databases: SQL Query Optimization and Complex Query Handling.


 Data Warehousing: ETL Processes and Analytical Queries
 Big Data and NoSQL Systems: Query Optimization in Distributed Systems and
MapReduce Optimization:
 4. Business Intelligence (BI) Tools: Dashboard Performance
 E-commerce and Online Transaction Processing (OLTP)
 Cloud Databases and Database as a Service (DBaaS
 Content Management Systems (CMS)
 Internet of Things (IoT)
 Geospatial Databases

STEP-BY-STEP-PROCESS:

1. Create the Database and Insert Values


Step 1: Create the Database

Step 2: Create Tables: Students Table, Courses Table, Enrollments Table, Department

Table and Professors Table.


Step 3: Insert Data: Insert into Students Table, Insert into Courses Table, Insert into
Enrolments Table, Insert into Departments Table and Insert into Professors Table.

Step 4: Display the Values: To display data from the Students table, Courses Table,

Enrolments Table, Departments Table and Professors Table

2. Retrieve Table Metadata

Step 1: Fetch Metadata for the Students Table: Get column names, data types, and
constraints, Get detailed information about the Students table:

Step 2: Identify Indexed Columns

Check for indexes:

3. Optimize a Query Using Metadata


Step 1: Check for an Index on CourseID in Enrollments
Check if there is an index and Create an index if it doesn't exist:

Step 2: Write an Optimized Query

Fetch student names enrolled in a specific course (e.g., CourseID = 1):

4. Evaluate Query Performance


Step 1: Use EXPLAIN to Analyze the Query Before and After Indexing
Before indexing:

AIM:

To design a relational database for a college that contains information about students,
courses, enrollments, departments, and professors.

ALGORITHMS:

Step 1: Design and Create the Database

1. Start by creating a new database named CollegeDB.


2. Create the necessary tables: Students, Courses, Enrollments, Departments, and
Professors.
o Define columns with appropriate data types and constraints.
o Set up primary keys and foreign key relationships to enforce data integrity.

Step 2: Insert Data into the Tables


1. Insert sample data into each table (Departments, Students, Courses, Professors,
Enrollments).
2. Display the inserted data using SELECT queries to verify the correct insertion of
values.

Step 3: Retrieve Table Metadata

1. Write SQL queries to fetch metadata about the Students table, including column
names, data types, and constraints.
o Use the SHOW COLUMNS or DESCRIBE commands to retrieve this
information.
2. Identify indexed and non-indexed columns by querying the
INFORMATION_SCHEMA or using the SHOW INDEX command.

Step 4: Optimize a Query Using Metadata

1. Determine if an index exists on the CourseID column in the Enrollments table by


checking the table's metadata.
o If no index exists, create an index on CourseID.
2. Write an optimized SQL query to retrieve the names of all students enrolled in a
specific course.
o Use a JOIN operation between the Students and Enrollments tables to fetch the
required data.

Step 5: Evaluate Query Performance

1. Use the EXPLAIN command to analyze the query execution plan before and after
indexing the CourseID column.
o Compare the results to observe the impact of indexing on query performance.
2. Record the changes in execution time, cost, and other relevant metrics to assess the
optimization's effectiveness.

Step 6: Conclude

1. Summarize the outcomes of the database creation, data insertion, metadata retrieval,
query optimization, and performance evaluation.
2. End the process.

IMPLEMENTATION:

1. Create the Database and Insert Values


Step 1: Create the Database
CREATE DATABASE CollegeDB;
USE CollegeDB;

Step 2: Create Tables


Students Table:
CREATE TABLE Students (
StudentID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

Courses Table:

CREATE TABLE Courses (


CourseID INT PRIMARY KEY AUTO_INCREMENT,
CourseName VARCHAR(100),
DepartmentID INT,
Credits INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

Enrollments Table:

CREATE TABLE Enrollments (


EnrollmentID INT PRIMARY KEY AUTO_INCREMENT,
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);

Departments Table:

CREATE TABLE Departments (


DepartmentID INT PRIMARY KEY AUTO_INCREMENT,
DepartmentName VARCHAR(100)
);

Professors Table:

CREATE TABLE Professors (


ProfessorID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DepartmentID INT,
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);

Step 3: Insert Data


Insert into Departments Table:

INSERT INTO Departments (DepartmentName) VALUES ('Computer Science'),


('Mathematics'), ('Physics');

Insert into Students Table:

INSERT INTO Students (FirstName, LastName, DateOfBirth, DepartmentID)


VALUES
('Alice', 'Smith', '2000-01-15', 1),
('Bob', 'Johnson', '1999-06-22', 2),
('Carol', 'Williams', '2001-08-30', 3);

Insert into Courses Table:

INSERT INTO Courses (CourseName, DepartmentID, Credits)


VALUES
('Data Structures', 1, 4),
('Calculus', 2, 3),
('Quantum Mechanics', 3, 4);

Insert into Professors Table:

INSERT INTO Professors (FirstName, LastName, DepartmentID)


VALUES
('David', 'Brown', 1),
('Eva', 'Taylor', 2),
('Frank', 'Davis', 3);

Insert into Enrollments Table:

INSERT INTO Enrollments (StudentID, CourseID, EnrollmentDate)


VALUES
(1, 1, '2023-09-01'),
(2, 2, '2023-09-01'),
(3, 3, '2023-09-01');

Step 4: Display the Values


To display data from the Students table:
SELECT * FROM Students;

To display data from the Curses table:

SELECT * FROM Courses;

To display data from the Enrolment table:

SELECT * FROM Department;


To display data from the Department table:

SELECT * FROM Department;


To display data from the Professors table:

SELECT * FROM Professors;

2. Retrieve Table Metadata


Step 1: Fetch Metadata for the Students Table
Get column names, data types, and constraints:

SHOW COLUMNS FROM Students;

Get detailed information about the Students table:


DESCRIBE Students;

Step 2: Identify Indexed Columns


Check for indexes:
SHOW INDEX FROM Students;

3. Optimize a Query Using Metadata


Step 1: Check for an Index on CourseID in Enrollments
Check if there is an index:
SHOW INDEX FROM Enrollments;
Create an index if it doesn't exist:
CREATE INDEX idx_courseid ON Enrollments (CourseID);

Step 2: Write an Optimized Query


Fetch student names enrolled in a specific course (e.g., CourseID = 1):
SELECT s.FirstName, s.LastName
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
WHERE e.CourseID = 1;

4. Evaluate Query Performance


Step 1: Use EXPLAIN to Analyze the Query Before and After Indexing
Before indexing:
EXPLAIN SELECT s.FirstName, s.LastName
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
WHERE e.CourseID = 1;

OUTPUT:
Display the Values
To display data from the Students table:

To display data from the Curses table:

To display data from the Enrolment table:

To display data from the Department table:

To display data from the Professors table:


Retrieve Table Metadata
Fetch Metadata for the Students Table
Get column names, data types, and constraints:

Get detailed information about the Students table::

Identify Indexed Columns

Check for indexes:

Optimize a Query Using Metadata


Step 1: Check for an Index on CourseID in Enrollments
Check if there is an index:

Write an Optimized Query

Fetch student names enrolled in a specific course (e.g., CourseID = 1):

Evaluate Query Performance


Step 1: Use EXPLAIN to Analyze the Query Before and After Indexing
Before indexing:

RESULT: Thus to create to optimize queries for an XML database that stores college data,
specifically focusing on retrieving student information has been successfully executed.
Ex.No.: 2

DISTRIBUTED DATABASE

Create a distributed database and run various queries Use stored procedures

PROBLEM STATEMENT:

Problem Statement:

Tables in Multiple Databases


 Task1: Create tables in two or more distributed databases. Each table should have the
same structure but may contain different subsets of data i.e. Create tables Employees
in Database1 and Database2 .
Insert Data into Tables
 Task: Insert sample data into the Employees tables in each database.

Combine Data from Multiple Tables Using UNION


 Task: Write a query to combine data from the Employees tables in both databases
using the UNION operator.
Handle Duplicate Records
 Task: Modify the query from Q3 to handle duplicate records. Use UNION ALL if
you want to include duplicates.
Create a View to Aggregate Employee Information
 Task: Create a view that aggregates employee information from the combined data of
the Employees tables in both databases. The view should show total salary by
department.
Query the View
 Task: Write a query to retrieve data from the view created in Q5. Display the total
salary for each department.

INTRODUCTION:

A distributed database is a collection of interrelated data that is distributed across multiple


locations, which can be spread across different physical sites or nodes within a network.
Unlike centralized databases where data resides in a single location, a distributed database
system (DDBS) manages data across different sites, ensuring consistency, reliability, and
efficient querying despite the physical distribution.
OBJECTIVES:

Manage tables across distributed databases, perform data insertion, and run various queries to
combine and aggregate data.

APPLICATIONS:

 E-commerce Platforms
 Financial Services
 Healthcare Systems
 Social Media Networks

STEP-BY-STEP EXPLANATION:

Task 1: Create Tables in Two or More Distributed Databases


 Connect to Database1.
 Execute the SQL command to create an Employees table.
 Connect to Database2.
 Execute the SQL command to create an Employees table with the same structure as in
Database1.

Task 2: Insert Sample Data into the Employees Tables in Each Database

 Connect to Database1 and insert sample data into the Employees table.
 Connect to Database2 and insert different sample data into the Employees table.

Task 3: Combine Data from the Employees Tables in Both Databases Using the UNION
Operator

 Connect to both databases.


 Use a UNION query to combine the data from both Employees tables.

Task 4: Handle Duplicate Records

 Modify the previous UNION query to include duplicate records using UNION ALL.

Task 5: Create a View to Aggregate Employee Information

 Create a view to aggregate employee information showing the total salary by


department across both databases.

Task 6: Query the View to Display Total Salary for Each Department
 Write a query to retrieve data from the view EmployeeSalaryByDepartment to display
the total salary for each department.

AIM: To create a distributed database system and perform various operations using stored
procedures,

ALGORITHM:

o Create identical tables in multiple databases.


o Insert data into each database.
o Combine data from the distributed databases.
o Handle duplicate records.
o Aggregate data using a view.
o Query the view to retrieve specific results.

IMPLEMENTATION:
Create Tables

Database1:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
DepartmentID INT,
SalaryID INT
);

CREATE TABLE Departments (


DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);

CREATE TABLE Salaries (


SalaryID INT PRIMARY KEY,
SalaryAmount DECIMAL(10, 2)
);

Database2:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
DepartmentID INT,
SalaryID INT
);
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);

CREATE TABLE Salaries (


SalaryID INT PRIMARY KEY,
SalaryAmount DECIMAL(10, 2)
);

Insert Data

Database1:

INSERT INTO Employees (EmployeeID, Name, DepartmentID, SalaryID) VALUES


(1, 'Alice', 101, 201),
(2, 'Bob', 102, 202);

INSERT INTO Departments (DepartmentID, DepartmentName) VALUES


(101, 'HR'),
(102, 'Finance');

INSERT INTO Salaries (SalaryID, SalaryAmount) VALUES


(201, 50000),
(202, 60000);

Database2:

INSERT INTO Employees (EmployeeID, Name, DepartmentID, SalaryID) VALUES


(3, 'Charlie', 101, 203),
(4, 'David', 103, 204);

INSERT INTO Departments (DepartmentID, DepartmentName) VALUES


(101, 'HR'),
(103, 'IT');

INSERT INTO Salaries (SalaryID, SalaryAmount) VALUES


(203, 55000),
(204, 70000);

Combine Data Using UNION


-- Combining Employees from both databases

SELECT EmployeeID, Name, DepartmentID, SalaryID


FROM Database1.Employees
UNION
SELECT EmployeeID, Name, DepartmentID, SalaryID
FROM Database2.Employees;
Handle Duplicates with UNION ALL

-- Combining Employees from both databases including duplicates


SELECT EmployeeID, Name, DepartmentID, SalaryID
FROM Database1.Employees
UNION ALL
SELECT EmployeeID, Name, DepartmentID, SalaryID
FROM Database2.Employees;

Create a View to Aggregate Information

Assuming we can query across databases:


CREATE VIEW EmployeeSummary AS
SELECT d.DepartmentName,
COUNT(e.EmployeeID) AS EmployeeCount,
SUM(s.SalaryAmount) AS TotalSalary,
AVG(s.SalaryAmount) AS AverageSalary
FROM (Database1.Employees e
JOIN Database1.Departments d ON e.DepartmentID = d.DepartmentID
JOIN Database1.Salaries s ON e.SalaryID = s.SalaryID
UNION ALL
Database2.Employees e
JOIN Database2.Departments d ON e.DepartmentID = d.DepartmentID
JOIN Database2.Salaries s ON e.SalaryID = s.SalaryID)
GROUP BY d.DepartmentName;

Query the View:

SELECT DepartmentName,
EmployeeCount,
TotalSalary,
AverageSalary
FROM EmployeeSummary;

OUTPUT:

Sample Output of Combined Employees (UNION):


Sample Output of Combined Employees (UNION ALL):

Sample Output of Aggregated View:

RESULT: Thus to create and manage tables in two distributed databases, insert data,
combine data using SQL operations, handle duplicates, and aggregate information has been
successfully executed.
Ex.No.: 3

OBJECT-ORIENTED DATABASE

Create OQL Queries to access the data from Object Oriented Database.

PROBLEM STATEMENT:

Creating and manipulating an object-oriented database (OODB) using Object Query


Language (OQL) in a distributed database environment with the following scenario.
Setup and Configuration
1. Install and Configure the Distributed OODB:
o Install a distributed OODB system (e.g., ObjectDB, db4o, Versant).
o Configure the system across multiple nodes.
o Ensure that nodes are synchronized and capable of communicating with each
other.
2. Create a Database and Define Connection:
o Create a database instance and configure connections between distributed
nodes.
Create Basic Classes:
 Define classes and properties for a simple employee management system.

 Create a Department class and establish relationships.


Insert Records into Employee Class:
 Add sample employee records.

Insert Records into Department Class


 Add department records and associate employees.

Data Retrieval
 Retrieve all employees.
 Retrieve employees by department

 Retrieve employees with a salary above a certain threshold.


 Retrieve employees using relationships.
 Retrieve employees sorted by salary.
 Retrieve department-wise employee count.

Update Records:

 Increase the salary of employees in a specific department


 Change an employee’s department.
Delete Records:
 Delete an employee record.
Delete all employees from a department.
INTRODUCTION:

Object Query Language (OQL) is a query language specifically designed for Object-
Oriented Database Management Systems (OODBMS). It is used to query and manipulate
objects stored in an OODBMS, much like how SQL is used in relational databases. OQL
allows you to query data while leveraging the object-oriented features such as inheritance,
encapsulation, and polymorphism.

Key Concepts of OQL

1. Object-Oriented Data Model:


o Objects: In OODBMS, data is represented as objects, similar to objects in
programming languages like Java or C++.
o Classes and Inheritance: Objects are instances of classes, and classes can
inherit properties and methods from other classes.
o Relationships: Objects can have relationships with other objects, enabling
complex data structures.
2. OQL Structure:
o OQL syntax is similar to SQL but is designed to work with objects instead of
relational tables.
o It supports operations like selection, projection, joins, and aggregation, but
these operations are performed on objects and their properties.

Basic OQL Query Structure

An OQL query typically consists of the following components:

 SELECT Clause: Specifies the attributes or objects to retrieve.


 FROM Clause: Specifies the classes from which to retrieve objects.
 WHERE Clause: Specifies the conditions for selecting objects.

OBJECTIVES:

This system will allow operations such as inserting, retrieving, updating, and deleting
employee and department records across the distributed nodes.

STEP-BY-STEP PROCESS:

System Configuration Across Multiple Nodes

 Node Setup: Set up multiple servers or virtual machines as nodes.


 Install Database Software: Install a distributed database management system
(DBMS) like Apache Cassandra, CockroachDB, or MySQL Cluster on each node.
 Synchronization: Configure time synchronization across nodes using NTP (Network
Time Protocol) to ensure consistency.
 Networking: Ensure that all nodes are connected via a reliable network. Configure
firewall rules to allow communication between nodes on necessary ports.
 Cluster Configuration: Set up the cluster configuration in the DBMS, specifying the
nodes and their roles (e.g., master, replica).

Create a Database and Define Connections

 Database Creation: On one of the nodes, create a distributed database instance (e.g.,
EmployeeDB).
 Connection Setup: Define and configure database connections between nodes.
Ensure the database is accessible from all nodes in the cluster.

Create Basic Classes

 Employee Class:
o Properties: EmployeeID, Name, Salary, DepartmentID
 Department Class:
o Properties: DepartmentID, DepartmentName
 Relationships: Define a foreign key relationship between Employee and Department
using DepartmentID.

Insert Records into Employee Class


Insert Records into Department Class

Data Retrieval

 Retrieve all employee records across the distributed nodes.


 Retrieve employees by specific departments.
 Retrieve employees who have a salary above a certain threshold.
 Retrieve employees using the relationship between employees and departments.
 Retrieve and display employees sorted by salary.
 Retrieve and display the employee count for each department.

Update Records

 Increase the salary of all employees within a specific department.


 Change the department for a specific employee.

Delete Records

 Delete a specific employee record.


 Delete all employees associated with a specific department.

Display All Possible Outputs

 Display outputs for all retrieval, update, and delete operations.

AIM:

The aim of this project is to configure a distributed database system across multiple nodes for
an employee management system
ALGORITHM:

1. System Configuration Across Multiple Nodes


o Configure the system to operate across multiple nodes.
o Ensure that all nodes are synchronized and can communicate with each other.
2. Create a Database and Define Connections
o Create a distributed database instance.
o Configure and establish connections between the distributed nodes to ensure
they work together seamlessly.
3. Create Basic Classes
o Define the Employee and Department classes.
o Set up properties such as employee ID, name, salary, department ID for
Employee, and department ID, name for Department.
o Establish relationships between Employee and Department classes, such as
associating employees with departments.
4. Insert Records into Employee Class
o Add sample records for employees, including their name, salary, and
department ID.
5. Insert Records into Department Class
o Add department records and associate each department with relevant
employees using the department ID.
6. Data Retrieval
o Retrieve all employee records across the distributed nodes.
o Retrieve employees by specific departments.
o Retrieve employees who have a salary above a certain threshold.
o Retrieve employees using the relationship between employees and
departments.
o Retrieve and display employees sorted by salary.
o Retrieve and display the employee count for each department.
7. Update Records
o Increase the salary of all employees within a specific department.
o Change the department for a specific employee.
8. Delete Records
o Delete a specific employee record.
o Delete all employees associated with a specific department.
9. Display All Possible Outputs
o Display outputs for all retrieval, update, and delete operations.

IMPLEMENTATION:

Create a Database and Define Connections

 Database Creation: On one of the nodes, create a distributed database instance (e.g.,
EmployeeDB).
 Connection Setup: Define and configure database connections between nodes.
Ensure the database is accessible from all nodes in the cluster.
Create Basic Classes

 Employee Class:
o Properties: EmployeeID, Name, Salary, DepartmentID
 Department Class:
o Properties: DepartmentID, DepartmentName
 Relationships: Define a foreign key relationship between Employee and Department
using DepartmentID.

Database1:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
DepartmentID INT,
SalaryID INT
);

CREATE TABLE Departments (


DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);

CREATE TABLE Salaries (


SalaryID INT PRIMARY KEY,
SalaryAmount DECIMAL(10, 2)
);

Database2:

CREATE TABLE Employees (


EmployeeID INT PRIMARY KEY,
Name VARCHAR(50),
DepartmentID INT,
SalaryID INT
);

CREATE TABLE Departments (


DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50)
);

CREATE TABLE Salaries (


SalaryID INT PRIMARY KEY,
SalaryAmount DECIMAL(10, 2)
);
Insert Records into Employee Class

 Sample Records:

sql
Copy code
INSERT INTO Employee (EmployeeID, Name, Salary, DepartmentID) VALUES (1,
'John Doe', 50000, 101);
INSERT INTO Employee (EmployeeID, Name, Salary, DepartmentID) VALUES (2,
'Jane Smith', 60000, 102);
Insert Records into Department Class

 Sample Records:

sql
Copy code
INSERT INTO Department (DepartmentID, DepartmentName) VALUES (101, 'HR');
INSERT INTO Department (DepartmentID, DepartmentName) VALUES (102, 'IT');
Data Retrieval

 Retrieve All Employees:

sql
Copy code
SELECT * FROM Employee;

 Retrieve Employees by Department:

sql
Copy code
SELECT * FROM Employee WHERE DepartmentID = 101;

 Retrieve Employees with Salary Above a Threshold:

sql
Copy code
SELECT * FROM Employee WHERE Salary > 55000;

 Retrieve Employees Using Relationships:

sql
Copy code
SELECT e.Name, d.DepartmentName FROM Employee e JOIN Department d ON
e.DepartmentID = d.DepartmentID;

 Retrieve Employees Sorted by Salary:

sql
Copy code
SELECT * FROM Employee ORDER BY Salary DESC;
 Retrieve Department-wise Employee Count:

sql
Copy code
SELECT DepartmentID, COUNT(*) as EmployeeCount FROM Employee GROUP
BY DepartmentID;
Update Records

 Increase Salary of Employees in a Specific Department:

sql
Copy code
UPDATE Employee SET Salary = Salary * 1.10 WHERE DepartmentID = 102;

 Change an Employee’s Department:

sql
Copy code
UPDATE Employee SET DepartmentID = 101 WHERE EmployeeID = 2;
Delete Records

 Delete an Employee Record:

sql
Copy code
DELETE FROM Employee WHERE EmployeeID = 1;

 Delete All Employees from a Department:

sql
Copy code
DELETE FROM Employee WHERE DepartmentID = 102;

Display All Possible Outputs

 Retrieve and display outputs from the above SQL queries to show the result of
each operation.

OUTPUT:

Sample Output of Combined Employees (UNION):


Sample Output of Combined Employees (UNION ALL):

Sample Output of Aggregated View:

Retrieve All Employees

Retrieve Employees by Department


Retrieve Employees with Salary Above Threshold

Retrieve Employees Sorted by Salary

Retrieve Department-Wise Employee Count

RESULT: Thus to create and manage tables in two distributed databases, insert data,
combine data using SQL operations, handle duplicates, and aggregate information has been
successfully executed.
Ex.No.: 4

PARALLEL DATABASE

Access database from a programming language such as Java


PROBLEM STATEMENT:

1. Install and configure a parallel database system and Java development environment.
(Ex. Apache HBase)
2. Write Java code to establish a connection to the parallel database.
3. Execute basic SQL queries against the parallel database

o Write Java code INSERT, to execute SELECT, UPDATE, and DELETE


queries.
o Use PreparedStatement for executing parameterized queries.
o Display the results of SELECT queries in the console.

4. Perform parallel data operations in the database.


o Write Java code to perform operations on the database that can leverage
parallelism (e.g., batch inserts or parallel queries).
o Use Java's concurrency utilities (e.g., ExecutorService) to manage parallel
tasks.
o Measure and compare the performance of parallel operations versus sequential
operations.
5. Optimize database queries for performance.

INTRODUCTION:

Parallel databases are designed to handle large volumes of data by distributing the workload
across multiple processors or servers. This architecture allows for faster query processing,
improved scalability, and better fault tolerance compared to traditional single-node databases.

Key Concepts in Parallel Databases

1. Data Partitioning: Data is split across multiple disks or nodes. Common partitioning
methods include:
o Horizontal Partitioning: Dividing rows across different nodes.
o Vertical Partitioning: Dividing columns across different nodes.
o Hash Partitioning: Distributing data based on a hash function.
o Range Partitioning: Dividing data based on a range of values.
2. Parallel Query Execution: Queries are processed simultaneously by different
processors or nodes. Techniques include:
o Intra-query parallelism: Breaking a single query into sub-tasks that run in
parallel.
o Inter-query parallelism: Running multiple queries simultaneously across
different processors.
3. Load Balancing: Ensuring that data and query processing is evenly distributed across
nodes to prevent bottlenecks.
4. Fault Tolerance: If one node fails, the system can continue processing using the
remaining nodes, often with data replication to ensure no data is lost.

OBJECTIVES:

To develop an application in Java that can efficiently interact with a parallel database system
to perform various data operations, including querying, updating, and managing data across
distributed nodes.

APPLICATION:

 Data Warehousing
 Big Data Analytics
 Scientific Research
 Financial Services
 Telecommunications
 Health Care

STEP-BY-STEP EXPLANATION:

1. Schema Design

2. Sample Data Insertion:

3. Parallel Setup
 Sharding: Distribute the tables across different nodes based on UnitID or other criteria
to balance the load.
 Replication: Implement replication to ensure fault tolerance and high availability.

4. Basic Queries

 Retrieve all personnel information, including their unit names and roles:

 Find all missions conducted by a specific unit, including details about the personnel
assigned to those missions and the equipment used:

 alculate the total number of missions conducted by each unit and the average duration
of these missions:

 Generate a report listing the top 5 units with the highest number of missions,
including the total number of missions and average mission duration for each unit:
Aim:

 To set up a parallel database system using Apache HBase.


 To perform basic CRUD (Create, Read, Update, Delete) operations using Java.
 To leverage parallelism for improved performance and efficiency in database
operations.

Algorithms:

 Setup and Configuration: Install and configure HBase, ensuring it's properly
integrated with the Java development environment.
 Connection Establishment: Write Java code to connect to HBase.
 CRUD Operations: Implement Java code to perform INSERT, SELECT, UPDATE,
and DELETE operations.
 Parallel Operations: Use concurrency utilities to perform batch operations and
measure performance.
 Query Optimization: Use HBase-specific techniques and tools to optimize query
performance.

STEP BY STEP PROCESS:

Install and Configure Apache HBase


Installation

1. Download HBase: Download the latest HBase binary from the Apache HBase
website.
2. Extract the Archive:
3. Configure HBase:
o Navigate to the conf directory inside the HBase installation directory.
o Edit the hbase-site.xml file to configure HBase. Add the following properties
for a basic setup:
4. Start HBase:
5. Verify Installation:
o Open HBase Shell:
o Check the status:

2. Write Java Code to Establish a Connection to HBase

3. Execute Basic SQL Queries Against HBase


For HBase, you use the Table API rather than traditional SQL. Below are examples of how
you can perform CRUD operations.
Insert Data
Select Data
Update Data
Delete Data
parallel Data Operations
Parallel Queries Example
You can use a similar approach to perform parallel queries.

5. Optimize Database Queries for Performance

IMPLEMENTATION:

Install and Configure Apache HBase


Installation

1. Download HBase: Download the latest HBase binary from the Apache HBase
website.
2. Extract the Archive:

bash
Copy code
tar xzf hbase-x.y.z-bin.tar.gz

3. Configure HBase:
o Navigate to the conf directory inside the HBase installation directory.
o Edit the hbase-site.xml file to configure HBase. Add the following properties
for a basic setup:

xml
Copy code
<configuration>
<property>
<name>hbase.root.logger</name>
<value>INFO,console</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/tmp/zookeeper</value>
</property>
<property>
<name>hbase.master</name>
<value>localhost:16000</value>
</property>
<property>
<name>hbase.regionserver.port</name>
<value>16020</value>
</property>
</configuration>

4. Start HBase:

bash
Copy code
./bin/start-hbase.sh

5. Verify Installation:
o Open HBase Shell:

bash
Copy code
./bin/hbase shell

o Check the status:

hbase
Copy code
status 'detailed'
2. Write Java Code to Establish a Connection to HBase
You'll need the HBase client library in your Java project. You can include it via Maven or
download it manually.
Here’s an example of how to establish a connection using Java:
java
Copy code
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class HBaseConnectionExample {

public static void main(String[] args) {


Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.master", "localhost:16000");

try (Connection connection = ConnectionFactory.createConnection(config)) {


System.out.println("Connected to HBase!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Execute Basic SQL Queries Against HBase
For HBase, you use the Table API rather than traditional SQL. Below are examples of how
you can perform CRUD operations.
Insert Data
java
Copy code
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseInsertExample {

public static void main(String[] args) {


Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.master", "localhost:16000");

try (Connection connection = ConnectionFactory.createConnection(config);


Table table = connection.getTable(TableName.valueOf("my_table"))) {

Put put = new Put(Bytes.toBytes("row1"));


put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("column1"),
Bytes.toBytes("value1"));
table.put(put);

System.out.println("Data inserted!");

} catch (Exception e) {
e.printStackTrace();
}
}
}
Select Data
java
Copy code
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

public class HBaseSelectExample {

public static void main(String[] args) {


Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.master", "localhost:16000");

try (Connection connection = ConnectionFactory.createConnection(config);


Table table = connection.getTable(TableName.valueOf("my_table"))) {

Get get = new Get(Bytes.toBytes("row1"));


Result result = table.get(get);

byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("column1"));


System.out.println("Value: " + Bytes.toString(value));

} catch (Exception e) {
e.printStackTrace();
}
}
}
Update Data
You use Put for updating data in HBase as well.
Delete Data
java
Copy code
import org.apache.hadoop.hbase.client.Delete;

public class HBaseDeleteExample {

public static void main(String[] args) {


Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.master", "localhost:16000");

try (Connection connection = ConnectionFactory.createConnection(config);


Table table = connection.getTable(TableName.valueOf("my_table"))) {

Delete delete = new Delete(Bytes.toBytes("row1"));


table.delete(delete);

System.out.println("Data deleted!");

} catch (Exception e) {
e.printStackTrace();
}
}
}
4. Perform Parallel Data Operations
For parallel operations, use Java's ExecutorService.
Batch Inserts Example
java
Copy code
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HBaseBatchInsertExample {

private static final int THREAD_COUNT = 10;

public static void main(String[] args) {


ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);

for (int i = 0; i < THREAD_COUNT; i++) {


executor.submit(() -> {
try {
// Your HBase insert code here
} catch (Exception e) {
e.printStackTrace();
}
});
}

executor.shutdown();
}
}
Parallel Queries Example
You can use a similar approach to perform parallel queries.

5. Optimize Database Queries for Performance

 Use Filters: Apply filters to limit the data scanned.


 Batch Operations: Use batch operations to reduce the number of requests.
 Column Families: Group related columns into column families to optimize data
retrieval.

OUTPUT:
RESULT: Thus to design and implement a parallel database system to manage employee
information, including personnel, missions, and equipment has been successfully executed
Ex.No.: 5

ACTIVE DATABASES

Create a Active database with facts and extract data using rules.

PROBLEM STATEMENT:

Problem Statement: Design and implement a database for managing sales information. Your
database should include tables for customers, orders, and products. Set up the schema and
populate the tables with sample data. Implement triggers to automate specific actions in the
sales database. Focus on creating triggers that automatically update certain fields or enforce
business rules when data is inserted, updated, or deleted.

Tasks:

1. Schema Design:
o Create the following tables:
 Customers: CustomerID, CustomerName, ContactEmail
 Products: ProductID, ProductName, Price
 Orders: OrderID, CustomerID, OrderDate, TotalAmount
 OrderItems: OrderItemID, OrderID, ProductID, Quantity, ItemTotal
2. Sample Data Insertion:
o Populate each table with sample data to represent a sales scenario.
3. Basic Query:
o Write a query to retrieve all orders along with customer names and product
details.

Create a trigger that automatically calculates and updates the TotalAmount in the Orders
table whenever a new order is inserted or an OrderItems record is modified.

Create a trigger that automatically updates product inventory levels in the Products table
whenever an OrderItems record is inserted or updated.

Test the triggers by inserting and updating records in the Orders and OrderItems tables.
Verify that the TotalAmount and inventory levels are correctly updated.

Create an assertion that ensures the TotalAmount in the Orders table is equal to the sum of
ItemTotal values in the OrderItems table for that order.

Create an assertion that enforces a minimum quantity rule, such as ensuring that no
OrderItems record has a quantity less than 1.

Test the assertions by attempting to insert or update records that violate the rules. Verify that
the assertions prevent these invalid operations.

INTRODUCTION:
An active database is a type of database that integrates data storage with intelligent
processing capabilities. It actively monitors and reacts to changes or events within the
database by applying predefined rules. This approach extends the traditional database model
by allowing automatic, event-driven responses and decisions based on the current state of the
data.

Key Concepts of Active Databases

1. Facts:
o Definition: Facts are discrete pieces of information stored in the database.
They represent the current state or knowledge about entities in the domain.
o Example: In a customer database, facts might include "Customer A purchased
Product X" or "Product Y is in stock."
2. Rules:
o Definition: Rules are logical expressions that define how to derive new
information or trigger actions based on the existing facts.
o Structure: Typically expressed in an "IF-THEN" format. For instance, "IF a
customer's purchase amount exceeds $1000, THEN apply a 10% discount."
o Purpose: Rules enable the database to make decisions, infer new data, or
automate responses without manual intervention.
3. Event-Condition-Action (ECA) Model:
o Definition: A common paradigm in active databases where actions are
triggered based on specific events and conditions.
o Components:
 Event: A change or occurrence within the database or an external
event that prompts the database to act.
 Condition: A test applied to determine if the event meets certain
criteria.
 Action: The response or activity executed if the condition is satisfied.
4. Inference Engine:
o Definition: The component responsible for applying rules to facts to derive
new information or trigger actions.
o Functionality: Evaluates which rules apply to the current set of facts and
performs the necessary actions.

APPLICATIONS:

1. Real-Time Monitoring and Alerting


2. Business Process Automation
3. Enforcement of Business Rules

4. Auditing and Compliance


5. Dynamic Data Integration
6. Reactive User Interfaces

STEP-BY-STEP EXPLANATION:

1. Schema Design: Define tables for Customers, Products, Orders, and OrderItems.
2. Sample Data Insertion: Insert sample records into each table to simulate a sales
scenario.
3. Basic Query: Write a SQL query to retrieve orders along with customer names and
product details.
4. Triggers:
o Create a trigger to automatically update the TotalAmount in the Orders table
when new records are inserted or updated in the OrderItems table.
o Create a trigger to automatically update the inventory levels in the Products
table whenever an OrderItems record is inserted or updated.
5. Assertions:
o Create an assertion to ensure TotalAmount in the Orders table matches the
sum of ItemTotal in the OrderItems table.
o Create an assertion to enforce a minimum quantity rule for OrderItems.
6. Testing:
o Insert and update records to test triggers.
o Attempt invalid operations to test assertions.

AIM:

The aim is to create a relational database that manages sales information by tracking
customers, products, orders, and order items.

ALGORITHMS:

1. Schema Design: Define tables for Customers, Products, Orders, and OrderItems.
2. Sample Data Insertion: Insert sample records into each table to simulate a sales
scenario.
3. Basic Query: Write a SQL query to retrieve orders along with customer names and
product details.
4. Triggers:
o Create a trigger to automatically update the TotalAmount in the Orders table
when new records are inserted or updated in the OrderItems table.
o Create a trigger to automatically update the inventory levels in the Products
table whenever an OrderItems record is inserted or updated.
5. Assertions:
o Create an assertion to ensure TotalAmount in the Orders table matches the
sum of ItemTotal in the OrderItems table.
o Create an assertion to enforce a minimum quantity rule for OrderItems.
6. Testing:
o Insert and update records to test triggers.
o Attempt invalid operations to test assertions.

IMPLEMENTATIONS:
1. Schema Design
The schema consists of four tables: Customers, Products, Orders, and OrderItems. Each table
will have a set of fields as described.
SQL Statements for Table Creation
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(100),
ContactEmail VARCHAR(100)
);

CREATE TABLE Products (


ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2),
InventoryLevel INT
);

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10, 2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

CREATE TABLE OrderItems (


OrderItemID INT PRIMARY KEY,
OrderID INT,
ProductID INT,
Quantity INT,
ItemTotal DECIMAL(10, 2),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
2. Sample Data Insertion
Populate each table with sample data to represent a sales scenario.
SQL Statements for Data Insertion

-- Insert sample data into Customers


INSERT INTO Customers (CustomerID, CustomerName, ContactEmail)
VALUES
(1, 'John Doe', '[email protected]'),
(2, 'Jane Smith', '[email protected]');

-- Insert sample data into Products


INSERT INTO Products (ProductID, ProductName, Price, InventoryLevel)
VALUES
(1, 'Laptop', 999.99, 10),
(2, 'Smartphone', 499.99, 20),
(3, 'Tablet', 299.99, 15);

-- Insert sample data into Orders


INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
VALUES
(1, 1, '2024-09-01', 0.00),
(2, 2, '2024-09-01', 0.00);

-- Insert sample data into OrderItems


INSERT INTO OrderItems (OrderItemID, OrderID, ProductID, Quantity, ItemTotal)
VALUES
(1, 1, 1, 2, 1999.98),
(2, 1, 2, 1, 499.99),
(3, 2, 3, 3, 899.97);
3. Basic Query
To retrieve all orders along with customer names and product details:
SELECT
o.OrderID,
c.CustomerName,
p.ProductName,
oi.Quantity,
oi.ItemTotal
FROM
Orders o
JOIN
Customers c ON o.CustomerID = c.CustomerID
JOIN
OrderItems oi ON o.OrderID = oi.OrderID
JOIN
Products p ON oi.ProductID = p.ProductID;
4. Implementing Triggers
Trigger to Update TotalAmount in Orders
This trigger will automatically calculate and update the TotalAmount in the Orders table
whenever a new OrderItems record is inserted or an OrderItems record is modified.
CREATE TRIGGER UpdateOrderTotalAmount
AFTER INSERT OR UPDATE ON OrderItems
FOR EACH ROW
BEGIN
DECLARE orderTotal DECIMAL(10, 2);
SELECT SUM(ItemTotal) INTO orderTotal
FROM OrderItems
WHERE OrderID = NEW.OrderID;
UPDATE Orders
SET TotalAmount = orderTotal
WHERE OrderID = NEW.OrderID;
END;

Trigger to Update Product Inventory Levels


This trigger will automatically update product inventory levels in the Products table
whenever an OrderItems record is inserted or updated.

CREATE TRIGGER UpdateProductInventory


AFTER INSERT OR UPDATE ON OrderItems
FOR EACH ROW
BEGIN
DECLARE currentStock INT;
SELECT InventoryLevel INTO currentStock
FROM Products
WHERE ProductID = NEW.ProductID;
UPDATE Products
SET InventoryLevel = currentStock - NEW.Quantity
WHERE ProductID = NEW.ProductID;
END;

5. Testing Triggers
Test the triggers by inserting and updating records in the Orders and OrderItems tables.
Sample Test Data Insertion
-- Insert a new order item
INSERT INTO OrderItems (OrderItemID, OrderID, ProductID, Quantity, ItemTotal)
VALUES (4, 1, 3, 1, 299.99);

-- Update an existing order item


UPDATE OrderItems
SET Quantity = 2, ItemTotal = 599.98
WHERE OrderItemID = 2;

6. Implementing Assertions
Assertions are constraints that enforce rules on the data. Although not all databases support
the ASSERTION keyword directly, the same effect can be achieved using CHECK
constraints or triggers.
Assertion for TotalAmount Validation
CREATE TRIGGER ValidateOrderTotal
AFTER INSERT OR UPDATE ON Orders
FOR EACH ROW
BEGIN
DECLARE calculatedTotal DECIMAL(10, 2);
SELECT SUM(ItemTotal) INTO calculatedTotal
FROM OrderItems
WHERE OrderID = NEW.OrderID;

IF NEW.TotalAmount != calculatedTotal THEN


SIGNAL SQLSTATE '45000'
Assertion for Minimum Quantity Rule

CREATE TRIGGER ValidateMinQuantity


BEFORE INSERT OR UPDATE ON OrderItems
FOR EACH ROW
BEGIN
IF NEW.Quantity < 1 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Quantity must be at least 1.';
END IF;
END;
7. Testing Assertions
Attempt to insert or update records that violate the rules and verify that the assertions prevent
these invalid operations.
Sample Test for Assertions
-- Attempt to insert an invalid order item
INSERT INTO OrderItems (OrderItemID, OrderID, ProductID, Quantity, ItemTotal)
VALUES (5, 2, 1, 0, 0.00);

-- Attempt to update an order with mismatched TotalAmount


UPDATE Orders
SET TotalAmount = 1000.00
WHERE OrderID = 1;

OUTPUT:
Customers Table

Products Table:

Orders Table:

OrderItems Table:

RESULT: Thus to design and implement a production database with tables for products,
production orders, and inventory has been successfully executed
Ex.No.: 6

DEDUCTIVE DATABASE

Create a knowledge database with facts and extract data using rules

PROBLEM STATEMENT:

To use Datalog to create and query a deductive database that manages information
about birds and their characteristics. You will define facts and rules in Datalog to
infer new information and answer queries.
Set Up Environment:
 Use a Datalog implementation or a knowledge base system that supports Datalog
(e.g., SWI-Prolog, LogicBlox).
Define Facts:
 Create a set of facts about birds, including their characteristics such as habitat, diet,
and size.
Define Rules:
 Define rules to infer new information based on the facts
Query the Database:
 Write queries to retrieve information based on the facts and rules defined.
Add New Facts and Rules:
 Extend the database with additional facts and rules.
Test and Validate:
 Test the queries with the extended facts and rules.
 Validate that the queries return the expected results based on the new facts and rules.

INTRODUCTION:

A Knowledge Database (also known as a Deductive Database) extends the


functionality of traditional databases by incorporating reasoning capabilities. It stores
facts and rules and can infer new information from these through logical deduction.
The primary query language used in deductive databases is Datalog, a subset of
Prolog.

APPLICATIONS:

1. Expert Systems

 Medical Diagnosis:
 Legal Reasoning:

2. Data Integration
 Semantic Data Integration:
 Ontology Management:

3. Business Rule Management

 Fraud Detection:
 Compliance and Policy Enforcement:

4. Natural Language Processing (NLP)

 Semantic Parsing:
 Information Extraction:

5. Artificial Intelligence and Machine Learning

 Reasoning in AI:
 Explanation Generation:

6. Semantic Web

 RDF and OWL Reasoning:


 Linked Data Querying:

7. Cognitive Computing.

STEP-BY-STEP PROCESS:

1. Understand the Basics: Facts and Rules

 Facts: Basic assertions about the world, similar to tuples in a relational database.
 Rules: Logical statements that define relationships between facts, allowing the
inference of new facts.

2. Choose a Deductive Database System

Several systems support Datalog or similar languages:

 XSB: A logic programming and deductive database system.


 SWI-Prolog: Prolog environment with support for deductive database features.
 LogicBlox: A platform for building and querying deductive databases.

For this guide, let's assume we're using SWI-Prolog.

3. Install and Set Up the Environment

1. Download and Install SWI-Prolog:


o Visit the SWI-Prolog website and download the installer for your operating
system.
o Follow the installation instructions.
2. Open SWI-Prolog:
o Launch the SWI-Prolog interactive environment.

4. Create the Knowledge Base (KB)

In Prolog, the knowledge base is a file that contains facts and rules. Create a file named
birds.pl:

 Birds: Sparrows, eagles, penguins, and ostriches.


 Characteristics: Which birds can fly, and general rules like all birds have feathers
and lay eggs.

5. Load the Knowledge Base into SWI-Prolog

1. Load the File:


o
2. Check for Errors:
o Ensure there are no syntax errors in your KB.

6. Query the Knowledge Base


Now that the KB is loaded, you can start querying it:

AIM: To create and query a deductive database using Datalog to manage and infer
information about birds and their characteristics, such as habitat, diet, and size.

ALGORITHMS:

 Set Up Environment:

 Choose a Datalog implementation or knowledge base system (e.g., SWI-Prolog,


LogicBlox).
 Install and configure the chosen system.

 Define Facts:

 Represent information about birds using facts. Facts are basic assertions about entities
in the database.
 Example facts could include habitat, diet, and size of various bird species.

 Define Rules:

 Create rules to infer new information based on the existing facts. Rules use logical
inference to derive conclusions from facts.
 For example, a rule might infer that a bird is endangered if it has a specific diet and
habitat.

 Query the Database:


 Write queries to retrieve information based on the facts and rules defined.
 Queries can ask questions like which birds live in a specific habitat or which birds
have a certain diet.

 Add New Facts and Rules:

 Extend the database with additional facts and rules as needed.


 This allows you to include more information and refine the inferences.

 Test and Validate:

 Test the queries with the updated facts and rules.


 Validate that the results match the expected outcomes based on the new facts and
rules.

IMPLEMENTATION:

Define facts:
% Facts about birds
bird(eagle).
bird(pigeon).
bird(sparrow).
bird(parrot).

% Facts about habitats


habitat(eagle, forest).
habitat(pigeon, urban).
habitat(sparrow, urban).
habitat(parrot, tropical).

% Facts about diets


diet(eagle, carnivore).
diet(pigeon, herbivore).
diet(sparrow, omnivore).
diet(parrot, omnivore).

% Facts about sizes


size(eagle, large).
size(pigeon, medium).
size(sparrow, small).
size(parrot, medium).

Define Rules:

% Rule to infer if a bird is an urban bird


urban_bird(Bird) :- habitat(Bird, urban).

% Rule to infer if a bird is a predator


predator(Bird) :- diet(Bird, carnivore).

% Rule to infer if a bird is small


small_bird(Bird) :- size(Bird, small).

% Rule to find birds that are both urban and small


urban_and_small_bird(Bird) :- urban_bird(Bird), small_bird(Bird).

% Rule to find medium-sized birds


medium_bird(Bird) :- size(Bird, medium).

Write Queries:

% Query to find all urban birds


?- urban_bird(Bird).
% Expected output: Bird = pigeon ; Bird = sparrow.

% Query to find all predator birds


?- predator(Bird).
% Expected output: Bird = eagle.

% Query to find all small birds


?- small_bird(Bird).
% Expected output: Bird = sparrow.

% Query to find all birds that are both urban and small
?- urban_and_small_bird(Bird).
% Expected output: Bird = sparrow.

% Query to find all medium-sized birds


?- medium_bird(Bird).
% Expected output: Bird = pigeon ; Bird = parrot.

OUTPUT:

Here’s a sample output for each query when using the facts and rules defined:

1. Query to find all urban birds


?- urban_bird(Bird).

2. Query to find all predator birds


?- predator(Bird).

3. Query to find all small birds

?- small_bird(Bird).

4. Query to find all birds that are both urban and small

?- urban_and_small_bird(Bird).

5. Query to find all medium-sized birds

?- medium_bird(Bird).
RESULT: Thus to create and query a deductive database using Datalog to manage and infer
information about birds and their characteristics, such as habitat, diet, and size has been
successfully executed

Ex.No.: 7

ETL TOOL
To extract data from your transactional system to create a consolidated data
warehouse or data mart for reporting and analysis.

Problem Statement: Design and implement a basic ETL process to integrate production and
sales data from multiple sources into a centralized database. The goal is to extract data from
source systems, transform it to fit the target schema, and load it into the database.

Tasks:

1. Source Data Preparation:


o Source 1: CSV file containing production data.
 Fields: ProductID, ProductName, QuantityProduced, ProductionDate
o Source 2: SQL database containing sales data.
 Fields: SaleID, ProductID, SaleDate, QuantitySold, SaleAmount
2. Design Target Schema:
o Target Table 1: Production
 Fields: ProductID, ProductName, QuantityProduced, ProductionDate
o Target Table 2: Sales
 Fields: SaleID, ProductID, SaleDate, QuantitySold, SaleAmount
3. ETL Tool Setup:
o Use an ETL tool (e.g., Apache NiFi, Talend, or Microsoft SSIS) to perform
the following:
 Extract: Import data from the CSV file and SQL database.
 Transform: Cleanse and format the data. For example, standardize
date formats and remove duplicates.
 Load: Insert the transformed data into the target tables.
4. Validation:
o Verify that the data in the target tables matches the source data in terms of
accuracy and completeness.

INTRODUCTION:

ETL (Extract, Transform, Load) tools are software applications used to manage the process
of extracting data from various sources, transforming it into a suitable format, and loading it
into a target database or data warehouse. ETL processes are fundamental in data integration,
allowing businesses to consolidate data from different sources for analysis, reporting, and
decision-making

Step-by-Step Guide to Enhance the ETL Process with Data Aggregation and
Enrichment

1. Source Data Preparation

Source 1: CSV File Containing Production Data


 Content: Information about products being manufactured, including ProductID,
ProductionDate, QuantityProduced, etc.
 Location: Stored in a file directory.

Source 2: SQL Database Containing Sales Data

 Content: Sales records, including ProductID, SaleDate, QuantitySold, SaleAmount,


etc.
 Location: Stored in a SQL database.

2. Design Target Schema with Aggregation

Target Table 1: Production

 Fields: ProductID, ProductionDate, QuantityProduced, ProductCategory

Target Table 2: Sales

 Fields: ProductID, SaleDate, QuantitySold, SaleAmount

Target Table 3: MonthlySalesSummary

 Fields: ProductID, Month, TotalQuantitySold, TotalSaleAmount


 Description: This table will store aggregated sales data, summarized by month and
product.

3. ETL Tool Setup

Let's assume you're using a common ETL tool like Talend, Apache NiFi, or Pentaho Data
Integration (PDI). The steps will be similar across tools, with differences mainly in the user
interface.

**1. Extract: Import Data from Sources

 Production Data (CSV) Import:


o Create an Input component to read the CSV file.
o Configure the path to the CSV file and define the schema (e.g., ProductID,
ProductionDate, QuantityProduced).
 Sales Data (SQL) Import:
o Create an Input component to connect to the SQL database.
o Write an SQL query to extract the necessary fields (e.g., ProductID, SaleDate,
QuantitySold, SaleAmount).

**2. Transform: Data Aggregation and Enrichment

 Step 1: Aggregate Sales Data by Month and Product


o Use an Aggregation component to group sales data by ProductID and Month
(extracted from SaleDate).
o Calculate TotalQuantitySold and TotalSaleAmount for each product in each
month.
 Step 2: Enrich Production Data
o Define rules to assign a ProductCategory based on the ProductID or other
attributes.
o Use a Lookup/Mapping component to add the ProductCategory field to the
production data.

**3. Load: Insert Data into Target Tables

 Load Production Data:


o Create an Output component to load the transformed production data into the
Production table in your target database.
 Load Sales Data:
o Create an Output component to load the sales data into the Sales table in your
target database.
 Load Monthly Sales Summary:
o Create an Output component to load the aggregated data into the
MonthlySalesSummary table.

4. Validation

 Step 1: Verify Aggregation Results


o Query the MonthlySalesSummary table in the target database.
o Compare the results with the original sales data to ensure the aggregation was
performed correctly.
 Step 2: Check Data Enrichment
o Verify that the ProductCategory has been correctly added to the production
data based on the predefined rules.

APPLICATIONS:

1. Data Warehousing

2. Business Intelligence and Reporting

3. Data Migration

4. Data Integration

5. Big Data Processing

6. Customer Relationship Management (CRM)

AIM: Enhance the ETL (Extract, Transform, Load) process to not only load data from
multiple sources but also perform aggregations and enrich the dataset with additional
information.

ALGORITHMS:
 Source Data Preparation:

 Source 1: CSV file with production data.


 Source 2: SQL database with sales data.

 Design Target Schema:

 Target Table 1: Production


o Fields: ProductID, ProductName, ProductCategory
 Target Table 2: Sales
o Fields: SaleID, ProductID, SaleDate, QuantitySold, SaleAmount
 Target Table 3: MonthlySalesSummary
o Fields: ProductID, Month, TotalQuantitySold, TotalSaleAmount

 ETL Tool Setup:

 Extract:
o Import data from the CSV file and SQL database.
 Transform:
o Aggregation:
 Aggregate sales data from the SQL database by month and product to
calculate TotalQuantitySold and TotalSaleAmount.
o Enrichment:
 Enrich the production data by adding a ProductCategory based on
predefined rules or external data.
 Load:
o Insert the transformed and aggregated data into the Production, Sales, and
MonthlySalesSummary tables.

 Validation:

 Verify that the data in the MonthlySalesSummary table is correct by checking


aggregation results and comparing them against raw sales data.

IMPLEMENTATION:

Extract Data:

 CSV Extraction:

import pandas as pd
# Load CSV data into DataFrame
production_df = pd.read_csv('production_data.csv')
 SQL Extraction

import pandas as pd
import sqlalchemy
# Connect to SQL database
engine = sqlalchemy.create_engine('mysql+pymysql://user:password@host/dbname')
sales_df = pd.read_sql('SELECT * FROM sales_data', engine)

Transform Data:

 Aggregation:

# Convert SaleDate to datetime


sales_df['SaleDate'] = pd.to_datetime(sales_df['SaleDate'])
sales_df['Month'] = sales_df['SaleDate'].dt.to_period('M')

# Aggregate sales data


monthly_summary = sales_df.groupby(['ProductID', 'Month']).agg(
TotalQuantitySold=pd.NamedAgg(column='QuantitySold', aggfunc='sum'),
TotalSaleAmount=pd.NamedAgg(column='SaleAmount', aggfunc='sum')
).reset_index()
 Enrichment:

# Define enrichment rules (example)


category_rules = {
'P001': 'Electronics',
'P002': 'Clothing',
# Add more rules as needed
}
production_df['ProductCategory'] = production_df['ProductID'].map(category_rules)

Load Data:

 Load Data to SQL Database

# Load Production and Sales data


production_df.to_sql('Production', engine, if_exists='replace', index=False)
sales_df.to_sql('Sales', engine, if_exists='replace', index=False)
monthly_summary.to_sql('MonthlySalesSummary', engine, if_exists='replace', index=False)

Validation:

 Check Aggregation Results:

# Query and validate MonthlySalesSummary


summary_check = pd.read_sql('SELECT * FROM MonthlySalesSummary', engine)
print(summary_check.head())

OUTPUT:

Sample Output
1. Production Table:

2. Sales Table:

3. MonthlySalesSummary Table:

RESULT: Thus to Enhance the ETL (Extract, Transform, Load) process to not only load
data from multiple sources but also perform aggregations and enrich the dataset with
additional information has been successfully executed

Ex.No.: 8

ORACLE DATABASE
Store and retrieve voluminous data using SanssouciDB / Oracle DB.

PROBLEM STATEMENT:

A large dataset in an educational institution where student records are maintained in two
different classes (class1 and class2). Due to administrative needs, there is a requirement to
combine these records and retrieve specific information based on various criteria such as age
or grade. The objective is to demonstrate efficient data management and retrieval techniques
in Oracle DB by working with voluminous data.

Storing and Retrieving Voluminous Data Using Oracle DB

 Create two tables named class1 and class2.


 Insert a large amount of data into these tables.
 Perform a union operation to combine the tables.
 Retrieve and display the combined data.

INTRODUCTION:

Oracle Database is a robust and widely used relational database management system
(RDBMS) known for its scalability, performance, and security features. It supports a wide
range of data management tasks, including storing, retrieving, and manipulating large
volumes of data. Oracle's SQL language and advanced features like PL/SQL, partitioning,
and indexing make it a popular choice for enterprise applications.

STEP – BY – STEP PROCESS:

1. Setup Oracle Database Environment

Ensure you have access to an Oracle Database instance. You can use Oracle SQL Developer
or SQL*Plus for executing SQL queries.

2. Create Tables for Employee Information

You will create two tables to simulate storing employee records separately for different
departments.

3. Insert a Large Amount of Data into the Tables

To simulate a large dataset, you'll insert a significant number of records into both tables.

4. Combine Data from Both Tables Using a UNION Operation


The UNION operation will combine the records from both tables. The UNION operator
removes duplicates by default, but you can use UNION ALL if duplicates are not a concern.

5. Retrieve and Display the Combined Data

You can apply various SQL queries to retrieve specific information from the combined
dataset.

APPICATIONS:

1. Enterprise Resource Planning (ERP)

2. Customer Relationship Management (CRM)

3. Data Warehousing

4. E-commerce

5. Banking and Financial Services

6. Healthcare

7. Government and Public Sector

8. Telecommunications

AIM:

To demonstrate how to efficiently manage and retrieve a large dataset of employee


information stored in separate tables for different departments using Oracle DB.

ALGORITHM:

 Create Tables:

 Create two tables, employee_department1 and employee_department2, to store


employee data separately for different departments.

 Insert Data:

 Insert a large amount of sample employee data into these tables to simulate a real-
world scenario with voluminous data.
 Display Sample Data:

 Retrieve and display a subset of the data from both tables to verify that the data has
been correctly inserted.

 Combine Data:

 Use the SQL UNION operation to combine the data from both tables. This operation
eliminates duplicate records and provides a unified view of all employee data.

 Retrieve and Display Combined Data:

 Retrieve and display the combined data, applying filters to demonstrate data retrieval
based on specific criteria such as age or salary.

Output for Specific Criteria:

 Retrieve and display data for employees aged between 30 and 40.
 Retrieve and display data for employees with a salary greater than 60,000.

IMPLEMENTATION:

Step 1: Create Tables

CREATE TABLE employee_department1 (


employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
age NUMBER,
salary NUMBER,
department VARCHAR2(50)
);

CREATE TABLE employee_department2 (


employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
age NUMBER,
salary NUMBER,
department VARCHAR2(50)
);
Step 2: Insert Data
To simulate a large dataset, we will insert a substantial number of records into each table. The
following PL/SQL block inserts 50,000 records into each table:
BEGIN
FOR i IN 1..50000 LOOP
INSERT INTO employee_department1 (employee_id, first_name, last_name, age,
salary, department)
VALUES (i, 'First' || i, 'Last' || i, MOD(i, 60) + 20, MOD(i, 10000) + 30000,
'Department1');

INSERT INTO employee_department2 (employee_id, first_name, last_name, age,


salary, department)
VALUES (i + 50000, 'First' || (i + 50000), 'Last' || (i + 50000), MOD(i + 50000, 60) +
20, MOD(i + 50000, 10000) + 30000, 'Department2');
END LOOP;
COMMIT;
END;
Step 3: Display Sample Data
Retrieve and display a small subset of the data from each table to verify that data insertion
was successful:
SELECT * FROM employee_department1 FETCH FIRST 10 ROWS ONLY;

SELECT * FROM employee_department2 FETCH FIRST 10 ROWS ONLY;


Step 4: Combine Data
Use the UNION operation to combine data from both tables. The UNION operation removes
duplicate rows, ensuring a unique combined dataset:
SELECT * FROM employee_department1
UNION
SELECT * FROM employee_department2;
Step 5: Retrieve and Display Combined Data
Retrieve and display combined data based on specific criteria. For example, to retrieve
employees older than 30 with a salary greater than 50,000:
SELECT * FROM (
SELECT * FROM employee_department1
UNION
SELECT * FROM employee_department2
)
WHERE age > 30 AND salary > 50000;

Retrieve Employees Aged Between 30 and 40:

SELECT * FROM (
SELECT * FROM employee_department1
UNION
SELECT * FROM employee_department2
) WHERE age

Retrieve Employees with Salary Greater than 60,000:

SELECT * FROM (
SELECT * FROM employee_department1
UNION
SELECT * FROM employee_department2
) WHERE salary > 60000;

OUTPUT:

Table Creation Confirmation:


 The tables employee_department1 and employee_department2 are successfully
created.

Sample Data Display:

 A subset of employee data from both employee_department1 and


employee_department2 tables is displayed, confirming correct data insertion.
Example output:

Combined Data Display:

 All unique employee records from both departments are displayed after performing
the UNION operation. Example output:

Filtered Data Display:

 A filtered set of combined data is displayed based on the specified criteria (e.g., age >
30 and salary > 50,000). Example output:

Output for Employees Aged Between 30 and 40:


Output for Employees with Salary Greater than 60,000:

RESULT: Thus to demonstrate how to efficiently manage and retrieve a large dataset of
employee information stored in separate tables for different departments using Oracle DB
has been successfully executed

Ex.No.: 9

NO SQL

Expose supermarket and genre information stored in Oracle NoSQL Database


and access NoSQL data from Oracle Database using SQL queries.
PROBLEM STATEMENT:

You are the database administrator for a manufacturing company that produces various
electronic components. The company wants to adopt a NoSQL database to handle its
dynamic and complex data requirements more efficiently, including inventory management,
employee information, and production tracking. Your task is to design a MongoDB database
that meets these requirements and perform various CRUD operations to demonstrate its
capabilities.

1. Understand the use of MongoDB in managing manufacturing data.


2. Learn to create and manipulate collections and documents in MongoDB.
3. Perform CRUD operations relevant to a manufacturing company scenario.
4. Use aggregation pipelines to analyze manufacturing data.
5. Apply indexing and schema design strategies to optimize queries.

INTRODUCTION:

NoSQL (Not Only SQL) databases are designed to handle large volumes of unstructured,
semi-structured, or structured data with high scalability and flexibility. Unlike traditional
relational databases that use SQL, NoSQL databases support various data models like key-
value, document, column-family, and graph. They are particularly useful in scenarios where
data needs to be distributed across many servers, or when the data model changes frequently,
such as in real-time analytics, content management, and Internet of Things (IoT) applications.

Oracle NoSQL Database is a distributed, highly scalable key-value database designed to


manage large amounts of unstructured and semi-structured data. It offers automatic sharding,
replication, and failover, providing high availability and fault tolerance. Oracle NoSQL
supports ACID transactions, and it can be accessed using APIs for Java, C, Python, and
REST.

STEP – BY – STEP PROCESS:

1. Setup Oracle NoSQL Database Environment

Before you begin, ensure you have access to an Oracle NoSQL Database instance. You can
run Oracle NoSQL on-premises, or you can use Oracle NoSQL Cloud Service.

2. Create and Populate NoSQL Tables

Let’s assume you have two datasets: one for supermarkets and another for genres of products
sold in those supermarkets.

 Supermarkets: Stores information about supermarket chains, including name,


location, and ratings.
 Genres: Stores information about different genres of products, such as “Fruits”,
“Dairy”, “Bakery”, etc.

a. Create Supermarkets Table

b. Create Genres Table


c. Insert Data into the Tables

You can insert records into these tables using Oracle NoSQL’s API. Below is an example in
Python:

3. Expose NoSQL Data to Oracle Database

Oracle Database supports integration with Oracle NoSQL Database, allowing you to expose
NoSQL data as relational tables. This can be achieved through Oracle SQL Access for
NoSQL.

4. Configure Oracle SQL Access for NoSQL


5. Install and configure Oracle SQL Access for NoSQL on your Oracle Database
instance.
6. Create an external table in Oracle Database that maps to the NoSQL table.

b. Create External Tables in Oracle Database

4. Access NoSQL Data Using SQL Queries

Once the external tables are created, you can run SQL queries on the NoSQL data as if it
were in Oracle Database.

APPICATIONS:

1. Big Data Analytics

2. Content Management Systems (CMS)


3. E-commerce

4. Social Media and Social Networks

5. Internet of Things (IoT)

6. Mobile Applications

AIM:

To leverage MongoDB to efficiently manage and analyze data related to inventory, employee
information, and production tracking.

ALGORITHM:

 Database Design:

 Collections: Define collections for Inventory, Employees, and Production.


 Documents: Structure documents in each collection to capture relevant data.

 CRUD Operations:

 Create: Insert new records into each collection.


 Read: Retrieve and query records from the collections.
 Update: Modify existing records.
 Delete: Remove records from the collections.

 Aggregation Pipelines:

 Use aggregation to analyze and summarize data (e.g., total inventory value, employee
performance metrics).

 Indexing and Schema Design:

 Implement indexes to optimize query performance.

Design schemas to handle evolving data requirements efficiently

IMPLEMENTATION:

Schema Design:

 Define collections for Products, Employees, and ProductionRecords.


 Design document structure for each collection based on requirements.

Products Collection:
{
"_id": ObjectId,
"productName": "string",
"category": "string",
"price": "number",
"stockQuantity": "number"
}
Employees Collection:

{
"_id": ObjectId,
"employeeName": "string",
"position": "string",
"hireDate": "ISODate",
"salary": "number"
}
ProductionRecords Collection:

{
"_id": ObjectId,
"productId": ObjectId,
"quantityProduced": "number",
"productionDate": "ISODate"
}

2. CRUD Operations

Create:

// Connect to MongoDB
const db = connect('mongodb://localhost:27017/manufacturing');

// Insert a new product


db.Products.insertOne({
productName: "Smartphone",
category: "Electronics",
price: 699.99,
stockQuantity: 150
});
// Insert a new employee
db.Employees.insertOne({
employeeName: "Alice Smith",
position: "Engineer",
hireDate: new Date("2024-01-15"),
salary: 85000
});

// Insert a new production record


db.ProductionRecords.insertOne({
productId: ObjectId("ProductObjectIdHere"),
quantityProduced: 500,
productionDate: new Date()
});
Read:

javascript
Copy code
// Find all products
db.Products.find({}).toArray();

// Find a specific employee


db.Employees.findOne({ employeeName: "Alice Smith" });

// Find production records for a specific product


db.ProductionRecords.find({ productId: ObjectId("ProductObjectIdHere") }).toArray();
Update:

// Update stock quantity of a product


db.Products.updateOne(
{ productName: "Smartphone" },
{ $set: { stockQuantity: 200 } }
);
// Update employee salary
db.Employees.updateOne(
{ employeeName: "Alice Smith" },
{ $set: { salary: 90000 } }
);
Delete:

// Delete a product
db.Products.deleteOne({ productName: "Smartphone" });

// Delete an employee
db.Employees.deleteOne({ employeeName: "Alice Smith" });

3. Aggregation Pipelines

Example: Total Quantity Produced by Product Category:

db.ProductionRecords.aggregate([
{
$lookup: {
from: "Products",
localField: "productId",
foreignField: "_id",
as: "productDetails"
}
},
{ $unwind: "$productDetails" },
{
$group: {
_id: "$productDetails.category",
totalQuantityProduced: { $sum: "$quantityProduced" }
}
}
]).toArray();
4. Indexing
Create Indexes:

// Create an index on productName for faster queries


db.Products.createIndex({ productName: 1 });

// Create an index on employeeName for faster queries


db.Employees.createIndex({ employeeName: 1 });

// Create an index on productId in ProductionRecords for faster joins


db.ProductionRecords.createIndex({ productId: 1 });

OUTPUT:

Read Operation - Products Collection:

Aggregation Output - Total Quantity Produced by Category:

RESULT: Thus To leverage MongoDB to efficiently manage and analyze data related
to inventory, employee information, and production tracking has been successfully executed.
Ex.No.: 10

INTEGRATING WEB DATABASE

Build Web applications using Java servlet API for storing data in databases that can be
queried using variant of SQL.

Problem Statement

You are tasked with developing a simple web application that manages user information for a
company. The application should provide an interface for users to add, view, update, and
delete user records. The application should be built using PHP for server-side scripting and
MySQL for the database.

1. Understand how to set up a PHP development environment with MySQL.


2. Learn how to connect PHP to a MySQL database and perform CRUD operations.
3. Create a user-friendly web interface using HTML and PHP.
4. Validate and sanitize user input to ensure security.
5. Understand basic web security practices, such as preventing SQL injection and cross-
site scripting (XSS).

INTRODUCTION:

An integrated database refers to a system where data is stored in a manner that allows
seamless integration with different software applications, particularly web applications. It
often involves using relational databases that can be queried using SQL (Structured Query
Language) or its variants. The integration allows developers to create, retrieve, update, and
delete (CRUD) operations on data through web applications, ensuring that the data remains
consistent and accessible.

In the context of web applications, Java Servlet API is commonly used to build dynamic web
applications that interact with databases. Java Servlets handle HTTP requests and responses,
while JDBC (Java Database Connectivity) allows Java applications to interact with relational
databases.

STEP-BY-STEP-PROCESS:

1. Setup Development Environment

Before you start, ensure you have the following tools and software installed:

 JDK (Java Development Kit): To compile and run Java programs.


 Apache Tomcat: A web server that supports Java Servlets.
 MySQL or PostgreSQL: A relational database for storing data.
 IDE (Integrated Development Environment): Such as Eclipse or IntelliJ IDEA.
2. Create a Database

First, create a database that your web application will interact with.

a. Create a Database and Table in MySQL

3. Set Up a Java Web Application

a. Create a Dynamic Web Project in Eclipse

1. Open Eclipse and go to File > New > Dynamic Web Project.
2. Enter the project name (e.g., UserManagementApp), select the target runtime (e.g.,
Apache Tomcat), and click Finish.

b. Add JDBC Driver to the Project

1. Download the JDBC driver for your database (e.g., MySQL Connector/J for MySQL).
2. Right-click on your project, go to Build Path > Configure Build Path, and add the
JDBC driver JAR file to the project’s classpath.

4. Configure Database Connection

Create a utility class to manage database connections.

a. Database Connection Utility

5. Develop Servlets to Handle HTTP Requests

a. Create a Servlet to Handle User Registration

b. Create a Servlet to Handle User Login

6. Create JSP Pages for User Interface

a. Registration Page (register.jsp)

b. Login Page (login.jsp)

7. Deploy and Run the Application

1. Deploy the Web Application: Right-click on the project and select Run As > Run on
Server.
2. Access the Application: Open a web browser and go to
http://localhost:8080/UserManagementApp/register.jsp to register a user, or
http://localhost:8080/UserManagementApp/login.jsp to log in.
APPLICATIONS:

1. E-commerce Platforms

2. Content Management Systems (CMS)

3. Customer Relationship Management (CRM) Systems

4. Healthcare Management Systems

5. Educational Platforms

6. Social Media Platforms

AIM:

This web application is to enable a company to manage employee information effectively.


The application will allow administrative staff to perform CRUD (Create, Read, Update,
Delete) operations on employee records, ensuring a smooth and efficient way to handle
employee data.

ALGORITHM:

 Setup Environment

 Install a local server environment like XAMPP or WAMP that includes PHP and
MySQL.
 Create a MySQL database and table for storing employee information.

 Connect PHP to MySQL

 Establish a connection to the MySQL database using PHP’s mysqli or PDO extension.
 Create functions for CRUD operations.

 Build User Interface

 Design HTML forms for adding, viewing, updating, and deleting employee records.
 Use PHP to handle form submissions and display data.

 Implement Form Validation and Sanitization

 Validate user input to ensure it meets expected formats and constraints.


 Sanitize input to prevent security vulnerabilities.

 Basic Security Measures


 Implement prepared statements to prevent SQL injection.
 Use secure password hashing for user authentication if required.

IMPLEMENTATION:

Set Up PHP and MySQL Development Environment

1. Install XAMPP/WAMP:
o Download and install XAMPP (Windows, Linux) or WAMP (Windows) from
their official sites.
o Start Apache and MySQL services from the control panel.
2. Create Database and Table:

sql
Copy code
CREATE DATABASE company_db;
USE company_db;

CREATE TABLE employees (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
position VARCHAR(100),
department VARCHAR(100),
email VARCHAR(100) UNIQUE NOT NULL
);

2. Connect PHP to MySQL

db_connect.php

php
Copy code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "company_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
3. Build User Interface

index.php

<?php include 'db_connect.php'; ?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Employee Management</title>
</head>
<body>
<h1>Employee Management System</h1>

<h2>Add Employee</h2>
<form action="add_employee.php" method="post">
Name: <input type="text" name="name" required><br>
Position: <input type="text" name="position"><br>
Department: <input type="text" name="department"><br>
Email: <input type="email" name="email" required><br>
<input type="submit" value="Add Employee">
</form>

<h2>View Employees</h2>
<?php
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table
border='1'><tr><th>ID</th><th>Name</th><th>Position</th><th>Department</
th><th>Email</th><th>Actions</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['position']}</
td><td>{$row['department']}</td><td>{$row['email']}</td>";
echo "<td><a href='update_employee.php?id={$row['id']}'>Update</a> | <a
href='delete_employee.php?id={$row['id']}'>Delete</a></td></tr>";
}
echo "</table>";
} else {
echo "No employees found.";
}
?>
</body>
</html>

add_employee.php
<?php
include 'db_connect.php';

$name = $conn->real_escape_string($_POST['name']);
$position = $conn->real_escape_string($_POST['position']);
$department = $conn->real_escape_string($_POST['department']);
$email = $conn->real_escape_string($_POST['email']);

$sql = "INSERT INTO employees (name, position, department, email) VALUES ('$name',
'$position', '$department', '$email')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully. <a href='index.php'>Go back</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

update_employee.php

<?php
include 'db_connect.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$id = (int)$_POST['id'];
$name = $conn->real_escape_string($_POST['name']);
$position = $conn->real_escape_string($_POST['position']);
$department = $conn->real_escape_string($_POST['department']);
$email = $conn->real_escape_string($_POST['email']);

$sql = "UPDATE employees SET name='$name', position='$position',


department='$department', email='$email' WHERE id=$id";

if ($conn->query($sql) === TRUE) {


echo "Record updated successfully. <a href='index.php'>Go back</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
} else {
$id = (int)$_GET['id'];
$sql = "SELECT * FROM employees WHERE id=$id";
$result = $conn->query($sql);
$employee = $result->fetch_assoc();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Employee</title>
</head>
<body>
<h1>Update Employee</h1>
<form action="update_employee.php" method="post">
<input type="hidden" name="id" value="<?php echo $employee['id']; ?>">
Name: <input type="text" name="name" value="<?php echo $employee['name']; ?>"
required><br>
Position: <input type="text" name="position" value="<?php echo $employee['position'];
?>"><br>
Department: <input type="text" name="department" value="<?php echo
$employee['department']; ?>"><br>
Email: <input type="email" name="email" value="<?php echo $employee['email']; ?>"
required><br>
<input type="submit" value="Update Employee">
</form>
</body>
</html>

delete_employee.php

<?php
include 'db_connect.php';

$id = (int)$_GET['id'];

$sql = "DELETE FROM employees WHERE id=$id";

if ($conn->query($sql) === TRUE) {


echo "Record deleted successfully. <a href='index.php'>Go back</a>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
OUTPUT:

Adding an Employee

After filling out the form and submitting, you would see:

New record created successfully. <a href='index.php'>Go back</a>

Viewing Employees

You would see a table with employee details and action links:

Updating an Employee

After updating, you would see:

Record updated successfully. <a href='index.php'>Go back</a>

Deleting an Employee

After deleting, you would see:


Record deleted successfully. <a href='index.php'>Go back</a>

Security Measures

SQL Injection Prevention: Use prepared statements to prevent SQL injection. For
instance, modify add_employee.php as follows:

Using Prepared Statements

RESULT: Thus to enable a company to manage employee information effectively. The


application will allow administrative staff to perform CRUD (Create, Read, Update, Delete)
operations on employee records, ensuring a smooth and efficient way to handle employee
data has been successfully executed.

You might also like