Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views13 pages

DDS Data Base System

The document discusses multi-database query processing architecture. It describes the components involved including a query parser, query optimizer, local and remote query processors, and a global query coordinator that coordinates query execution across databases and integrates results.

Uploaded by

QaMar ZaMan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views13 pages

DDS Data Base System

The document discusses multi-database query processing architecture. It describes the components involved including a query parser, query optimizer, local and remote query processors, and a global query coordinator that coordinates query execution across databases and integrates results.

Uploaded by

QaMar ZaMan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name Qamar Ul Zaman Roll No: 321224 Class: BS CS M (B) 8TH

QUESTION # 1:
What is JOIN in SQL? What is JOIN Query syntax? Explain different types
of JOIN Quires and its syntax with the help of examples. Also
differentiate between SQL UNION and SQL JOIN and Cross JOIN.

Ans: In SQL, the JOIN operation combines rows from two or more tables based on a
related column between them. It allows you to retrieve data from multiple tables
simultaneously and establish relationships between them. The resulting dataset
contains columns from all the joined tables.
The syntax for the JOIN operation in SQL varies depending on the type of join being
used. Here is the general syntax:
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column;```
The above syntax represents an inner join, which is the most common type of join. It
selects records that have matching values in both tables based on the specified
column. However, there are several types of joins that differ in their behavior and the
resulting dataset. Let's discuss them with examples:
1. Inner Join:
- Syntax: `SELECT columns FROM table1 JOIN table2 ON table1.column =
table2.column;`
- Example: Consider two tables, "Customers" and "Orders." To retrieve the orders
made by customers, you can use an inner join:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

2. Left Join (or Left Outer Join):


- Syntax: `SELECT columns FROM table1 LEFT JOIN table2 ON table1.column =
table2.column;`
- Example: If you want to retrieve all customers and their corresponding orders (if
any), you can use a left join:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
3. Right Join (or Right Outer Join):
- Syntax: `SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column
= table2.column;`
- Example: To retrieve all orders and their associated customer information (if
available), you can use a right join:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
4. Full Join (or Full Outer Join):
- Syntax: `SELECT columns FROM table1 FULL JOIN table2 ON table1.column =
table2.column;`
- Example: If you want to retrieve all records from both tables, regardless of
matches, you can use a full join. Note that not all database systems support the full
join syntax directly. You can achieve the same result using a combination of left and
right joins:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
UNION
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
SQL UNION and SQL JOIN are two different operations:
- UNION is used to combine the result sets of two or more SELECT statements into a
single result set. It stacks the rows vertically, combining data from multiple tables
with the same column structure.
- JOIN, on the other hand, combines columns horizontally from two or more tables
based on a related column. It creates a new result set by matching rows from different
tables.

Cross JOIN is another type of join that doesn't require a related column between
tables. It returns the Cartesian product of the two tables, combining each row from the
first table with every row from the second table.
The syntax for a Cross JOIN is as follows:
SELECT columns
FROM table1
CROSS JOIN table2;
Example:
SELECT Customers.CustomerName, Products.ProductName
FROM Customers

QUESTION # 2:

Create a report titled Customer History Report. This report contains each
customer’s history of renting videos. Be sure to include the customer
name, movie rented, dates of the rental, and duration of rentals. Total the
number of rentals for all customers for the reporting period.

Ans: -- Create the Customers table


CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
CustomerAddress VARCHAR(100),
CustomerPhone VARCHAR(20)
);

-- Insert sample data into Customers table


INSERT INTO Customers (CustomerID, CustomerName, CustomerAddress,
CustomerPhone)
VALUES
(1, 'John Smith', '123 Main St', '555-1234'),
(2, 'Jane Doe', '456 Elm St', '555-5678'),
(3, 'Michael Johnson', '789 Oak St', '555-9012'),
(4, 'Emily Davis', '987 Pine St', '555-3456'),
(5, 'Robert Wilson', '654 Birch St', '555-7890'),
(6, 'Sarah Thompson', '321 Cedar St', '555-2345');

-- Create the Rentals table


CREATE TABLE Rentals (
RentalID INT PRIMARY KEY,
CustomerID INT,
MovieRented VARCHAR(50),
RentalDate DATE,
RentalDuration INT
);

-- Insert sample data into Rentals table


INSERT INTO Rentals (RentalID, CustomerID, MovieRented, RentalDate,
RentalDuration)
VALUES
(1, 1, 'Movie A', '2023-07-01', 3),
(2, 2, 'Movie B', '2023-07-02', 2),
(3, 3, 'Movie C', '2023-07-03', 1),
(4, 1, 'Movie D', '2023-07-04', 4),
(5, 4, 'Movie E', '2023-07-05', 2),
(6, 2, 'Movie F', '2023-07-06', 3);

To calculate the total number of rentals for the reporting period, you can use the
following query:

SELECT COUNT(*) AS TotalRentals


FROM Rentals;

The output would look like this:

| Customer Name | Movie Rented | Rental Date | Rental Duration |


|------------------|--------------|-------------|-----------------|
| John Smith | Movie A | 2023-07-01 | 3 |
| Jane Doe | Movie B | 2023-07-02 | 2 |
| Michael Johnson | Movie C | 2023-07-03 | 1 |
| John Smith | Movie D | 2023-07-04 | 4 |
| Emily Davis | Movie E | 2023-07-05 | 2 |
| Jane Doe | Movie F | 2023-07-06 | 3 |

To calculate the total number of rentals for the reporting period, you can use the
following query:

SELECT COUNT(*) AS 'Total Rentals'


FROM Rentals;

The output will be a single row table displaying the total number of rentals:

| Total Rentals |
|---------------|
|6 |

QUESTION # 3:

Multi-database Query Processing Architecture:


Multi-database query processing architecture refers to the design and structure of a
system that enables the execution of queries across multiple databases. This
architecture allows for distributed data processing, where data is stored and managed
in separate databases distributed across different locations. Here's a brief explanation
of the components involved:

1. Query Parser: Receives user queries and analyzes their structure and semantics. It
determines which parts of the query can be executed locally and which parts need to
be sent to remote databases.

2. Query Optimizer: Takes the parsed query and generates an optimized query
execution plan. It considers factors such as data distribution, network costs, and data
transfer overheads to minimize the overall query execution time.

3. Local Query Processor: Executes the parts of the query that can be processed
locally, retrieving data from the local database and performing local operations.
4. Global Query Coordinator: Coordinates the execution of the query across multiple
databases. It divides the query into subqueries and dispatches them to the appropriate
remote databases.

5. Remote Query Processors: Execute the received subqueries on their respective


databases and send the results back to the global query coordinator.

6. Data Integration: The global query coordinator combines the results received from
remote query processors, performs any necessary data integration or aggregation, and
returns the final result to the user.

Here is a simplified diagram illustrating the multi-database query processing


architecture:
+-------------------+
| Query Parser |
+-------------------+
|
|
v
+-------------------+
| Query Optimizer |
+-------------------+
|
|
v
+-------------------+
| Local Query |
| Processor |
+-------------------+
|
|
v
+---------------------------+
| Global Query Coordinator |
+---------------------------+
|
|
v
+-----------------------+-----------------------+
| Remote Query Processor | Remote Query Processor |
| (Database 1) | (Database 2) |
+-----------------------+-----------------------+
|
|
v
+-------------------+
| Data Integration |
+-------------------+
|
|
v
+-------------------+
| Result |
+-------------------+

Distributed DBMS Reliability and Failure:

Reliability Concepts and Measures:


1. System, State, and Failure: A distributed DBMS is composed of multiple
interconnected components, including hardware, software, and network. The system
represents the combination of these components. The state refers to the condition of
the system at a particular time, including the data stored and the processes running.
Failure is the inability of the system to perform its intended functions.
2. Reliability and Availability: Reliability is the ability of a distributed DBMS to
consistently perform its intended functions without failure. Availability refers to the
percentage of time the system remains operational and accessible to users.

3. Mean Time Between Failures (MTBF) and Mean Time to Repair (MTTR): MTBF
is the average time between two consecutive failures in the system. MTTR is the
average time required to repair a failed component and restore it to normal operation.
These measures are used to assess the reliability and maintainability of the distributed
DBMS.

Failure in Distributed DBMS:


Failures in a distributed DBMS can occur at different levels. Here are some common
types of failures:

1. Transaction Failures: These failures occur when a transaction cannot be completed


due to an error or violation of the system's integrity constraints. It may result from
conflicts between concurrent transactions or resource unavailability.

2. Site (System) Failures: Site failures happen when one or more sites in the
distributed system become unavailable or experience a malfunction. This can be due
to hardware or software failures, power outages, or network connectivity issues.

3. Media Failures: Media failures occur when there is a physical damage or corruption
to the storage media where the database is stored. It can result in data loss or
inconsistency.

4. Communication Failures: Communication failures happen when there are issues in


the network connectivity between different sites in the distributed system. It can lead
to data inconsistency or transaction failures.

Reliability Concepts and Measures in Distributed DBMS:


To ensure reliability in a distributed DBMS, various concepts and measures are
employed:
1. Fault Tolerance: It refers to the system's ability to continue functioning in the
presence of failures. Fault tolerance mechanisms, such as replication, backup, and
recovery techniques, are used to minimize the impact of failures and maintain data
integrity.

2. Redundancy: Redundancy involves duplicating data or resources across different


sites to ensure availability and reliability. It helps in achieving fault tolerance and
mitigating the impact of failures.

3. Distributed Transaction Management: Distributed transaction management


protocols, such as Two-Phase Commit (2PC), are used to ensure the atomicity and
consistency of transactions across multiple sites. These protocols handle failures
during transaction execution and recovery.

4. Load Balancing: Load balancing techniques distribute the workload evenly across
different sites in a distributed system. It helps prevent overloading of individual sites
and improves system performance and reliability.

5. Data Replication: Replication involves creating and maintaining multiple copies of


data across different sites. It improves availability, reduces access latency, and
provides fault tolerance by ensuring data redundancy.

By employing these reliability concepts and measures, a distributed DBMS can


enhance the system's robustness, availability, and fault tolerance, minimizing the
impact of failures and ensuring continuous operation.

QUESTION # 4:

Creating two tables, "Customers" and "Salesmen," with relevant attributes and
inserting 30+ random entries:
-- Create Customers table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
City VARCHAR(50)
);
-- Insert sample data into Customers table
INSERT INTO Customers (CustomerID, CustomerName, City)
VALUES
(1, 'John Smith', 'New York'),
(2, 'Jane Doe', 'Los Angeles'),
(3, 'Michael Johnson', 'Chicago'),
(4, 'Emily Davis', 'Houston'),
(5, 'Robert Wilson', 'San Francisco'),
-- Add more entries ...

-- Create Salesmen table


CREATE TABLE Salesmen (
SalesmanID INT PRIMARY KEY,
SalesmanName VARCHAR(50),
Commission DECIMAL(5, 2),
City VARCHAR(50)
);

-- Insert sample data into Salesmen table


INSERT INTO Salesmen (SalesmanID, SalesmanName, Commission, City)
VALUES
(1, 'Sam Thompson', 15.5, 'New York'),
(2, 'Lisa Anderson', 10.2, 'Chicago'),
(3, 'David Lee', 14.8, 'Los Angeles'),
(4, 'Sarah Johnson', 11.7, 'Houston'),
(5, 'Michael Brown', 13.6, 'San Francisco'),
-- Add more entries ...
Now, let's write the SQL queries:
(a) To find salespeople who received commissions of more than 12 percent, along
with the customer name, city, salesman name, and commission, you can use the
following query:
SELECT c.CustomerName, c.City, s.SalesmanName, s.Commission
FROM Customers c
JOIN Salesmen s ON c.City = s.City
WHERE s.Commission > 12.0;
(b) To find distinct salespeople and their cities, returning the salesperson ID and city,
you can use the following query:
SELECT DISTINCT s.SalesmanID, s.City
FROM Salesmen s;

Regarding the relational algebra tree for the union of the entities, since the
requirements mention a union of entities without specifying further details, I assume
you want to combine the Salesmen and Customers tables. Here's the relational algebra
tree for the UNION operation:
UNION
/ \
Salesmen Customers
The UNION operation will combine the Salesmen and Customers tables into a single
table, preserving distinct rows from both tables.

SQL queries in table format:

(a) Query to find salespeople who received commissions of more than 12 percent:
SELECT c.CustomerName, c.City, s.SalesmanName, s.Commission
FROM Customers c
JOIN Salesmen s ON c.City = s.City
WHERE s.Commission > 12.0;

| CustomerName | City | SalesmanName | Commission |


|-----------------|----------------|-----------------|------------|
| John Smith | New York | Sam Thompson | 15.50 |
| Michael Johnson | Chicago | Lisa Anderson | 14.80 |
| Sarah Johnson | Houston | Michael Brown | 13.60 |
| ... | ... | ... | ... |

(b) Query to find distinct salespeople and their cities:


SELECT DISTINCT s.SalesmanID, s.City
FROM Salesmen s;

| SalesmanID | City |
|------------|----------------|
|1 | New York |
|2 | Chicago |
|3 | Los Angeles |
|4 | Houston |
|5 | San Francisco |
To retrieve the names of seven or more persons who received commissions of 12
percent or more from the company, we can modify the previous query by adding a
LIMIT clause and adjusting the WHERE condition. Here's the updated query:

```sql
SELECT c.CustomerName, c.City, s.SalesmanName, s.Commission
FROM Customers c
JOIN Salesmen s ON c.City = s.City
WHERE s.Commission >= 12.0
LIMIT 7;
```

This query joins the Customers and Salesmen tables based on the city column, filters
the result to include only those with a commission of 12 percent or more, and limits
the output to the first seven records.
The output will be in table format, showing the customer name, city, salesman name,
and commission for each record:

| CustomerName | City | SalesmanName | Commission |


|-----------------|----------------|-----------------|------------|
| John Smith | New York | Sam Thompson | 15.50 |
| Michael Johnson | Chicago | Lisa Anderson | 14.80 |
| Sarah Johnson | Houston | Michael Brown | 13.60 |
| ... | ... | ... | ... |

Explanation:
1. The query starts by selecting the customer name, city, salesman name, and
commission from the Customers and Salesmen tables.
2. The JOIN condition specifies that the city column of both tables should match,
allowing us to combine the relevant data.
3. The WHERE clause filters the rows based on the condition that the commission
should be greater than or equal to 12.0.
4. The LIMIT clause restricts the output to seven records, as per the requirement.
5. The final result is displayed in tabular format, showing the customer name, city,
salesman name, and commission for each qualifying record.

You might also like