0% found this document useful (0 votes)
116 views3 pages

Order Processing Database Schema and Queries

The document outlines the structure of an order processing database with tables for Customer, Order, OrderItem, Item, Shipment, and Warehouse, including their primary and foreign keys. It provides SQL commands to create these tables, insert sample data, and perform queries to retrieve customer order statistics, order numbers based on shipment city, and details of all orders. Additionally, it demonstrates how to delete an item from the Item table and set the corresponding field to null in the OrderItem table.

Uploaded by

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

Order Processing Database Schema and Queries

The document outlines the structure of an order processing database with tables for Customer, Order, OrderItem, Item, Shipment, and Warehouse, including their primary and foreign keys. It provides SQL commands to create these tables, insert sample data, and perform queries to retrieve customer order statistics, order numbers based on shipment city, and details of all orders. Additionally, it demonstrates how to delete an item from the Item table and set the corresponding field to null in the OrderItem table.

Uploaded by

sumanp3232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Consider the following relations for an order processing database application in a

company.

Customer (custNo: int, cname: string, city: string)


Order (orderNo: int, odate: date, cust No: int, ord-amt: int)
Order-item (rderNo: int, item No: int, qty: int)
Item (itemNo: int, unit price: int)
Shipment (rderNo: int, warehouse No: int, ship-date: date)
Warehouse (warehouseNo: int, city: string)

Create the above tables by properly specifying the primary keys and the foreign
keys.

Enter at least five tuples for each relation.

Produce a listing custname, no of orders, avg order amt, where the middle column is
the total
number of orders by the customer and the last column is the average order amount
for that customer.

List the orderNo for the orders that were shipped from all the warehouses that the
company has in
a specific city.

Demonstrate how you delete itemNo 10 from the item table and make that field null
in the order-
item table.

-- Customer Table
CREATE TABLE Customer (
custNo INT PRIMARY KEY,
cname VARCHAR(50),
city VARCHAR(50)
);

-- Item Table
CREATE TABLE Item (
itemNo INT PRIMARY KEY,
unit_price INT
);

-- Order Table
CREATE TABLE OrderTable (
orderNo INT PRIMARY KEY,
odate DATE,
custNo INT,
ord_amt INT,
FOREIGN KEY (custNo) REFERENCES Customer(custNo)
);

-- OrderItem Table
CREATE TABLE OrderItem (
orderNo INT,
itemNo INT,
qty INT,
PRIMARY KEY (orderNo, itemNo),
FOREIGN KEY (orderNo) REFERENCES OrderTable(orderNo),
FOREIGN KEY (itemNo) REFERENCES Item(itemNo)
);

-- Warehouse Table
CREATE TABLE Warehouse (
warehouseNo INT PRIMARY KEY,
city VARCHAR(50)
);

-- Shipment Table
CREATE TABLE Shipment (
orderNo INT,
warehouseNo INT,
ship_date DATE,
PRIMARY KEY (orderNo, warehouseNo),
FOREIGN KEY (orderNo) REFERENCES OrderTable(orderNo),
FOREIGN KEY (warehouseNo) REFERENCES Warehouse(warehouseNo)
);

-- Customer
INSERT INTO Customer VALUES (1, 'Amit', 'Delhi');
INSERT INTO Customer VALUES (2, 'Neha', 'Mumbai');
INSERT INTO Customer VALUES (3, 'Ravi', 'Kolkata');
INSERT INTO Customer VALUES (4, 'Suman', 'Delhi');
INSERT INTO Customer VALUES (5, 'Pooja', 'Chennai');

-- Item
INSERT INTO Item VALUES (10, 100);
INSERT INTO Item VALUES (11, 150);
INSERT INTO Item VALUES (12, 200);
INSERT INTO Item VALUES (13, 250);
INSERT INTO Item VALUES (14, 300);

-- OrderTable
INSERT INTO OrderTable VALUES (101, TO_DATE('2024-01-01', 'YYYY-MM-DD'), 1, 300);
INSERT INTO OrderTable VALUES (102, TO_DATE('2024-01-02', 'YYYY-MM-DD'), 2, 400);
INSERT INTO OrderTable VALUES (103, TO_DATE('2024-01-03', 'YYYY-MM-DD'), 1, 200);
INSERT INTO OrderTable VALUES (104, TO_DATE('2024-01-04', 'YYYY-MM-DD'), 3, 500);
INSERT INTO OrderTable VALUES (105, TO_DATE('2024-01-05', 'YYYY-MM-DD'), 4, 100);

-- OrderItem
INSERT INTO OrderItem VALUES (101, 10, 2);
INSERT INTO OrderItem VALUES (102, 11, 1);
INSERT INTO OrderItem VALUES (103, 10, 1);
INSERT INTO OrderItem VALUES (104, 12, 2);
INSERT INTO OrderItem VALUES (105, 13, 1);

-- Warehouse
INSERT INTO Warehouse VALUES (1, 'Delhi');
INSERT INTO Warehouse VALUES (2, 'Kolkata');
INSERT INTO Warehouse VALUES (3, 'Mumbai');
INSERT INTO Warehouse VALUES (4, 'Chennai');
INSERT INTO Warehouse VALUES (5, 'Bangalore');

-- Shipment
INSERT INTO Shipment VALUES (101, 1, TO_DATE('2024-01-06', 'YYYY-MM-DD'));
INSERT INTO Shipment VALUES (102, 2, TO_DATE('2024-01-07', 'YYYY-MM-DD'));
INSERT INTO Shipment VALUES (103, 3, TO_DATE('2024-01-08', 'YYYY-MM-DD'));
INSERT INTO Shipment VALUES (104, 4, TO_DATE('2024-01-09', 'YYYY-MM-DD'));
INSERT INTO Shipment VALUES (105, 1, TO_DATE('2024-01-10', 'YYYY-MM-DD'));

Q.1 Produce a listing: custname, no_of orders, avg _order amt, where the middle
column is
the total number of orders by the customer and the last column is the average order
amount for that _customer.
SELECT
[Link] AS custname,
COUNT([Link]) AS no_of_orders,
AVG(o.ord_amt) AS avg_order_amt
FROM
Customer c
JOIN
OrderTable o ON [Link] = [Link]
GROUP BY
[Link];

Q.2 List the orderNo for the orders that were shipped from all the warehouses that
the company has in a specific city.
SELECT [Link]
FROM Shipment s
JOIN Warehouse w ON [Link] = [Link]
WHERE [Link] = 'Delhi'
GROUP BY [Link]
HAVING COUNT(DISTINCT [Link]) = (
SELECT COUNT(*)
FROM Warehouse
WHERE city = 'Delhi'
);

Q.3 We would like to know the order numbers of all orders placed by customers who
belong to a particular city.
SELECT [Link]
FROM "Order" o
JOIN Customer c ON [Link] = [Link]
WHERE [Link] = 'Kolkata';

Q.4 We would like to know the details of all orders that the company has received.
SELECT * FROM OrderTable;

Common questions

Powered by AI

The database tables for the order processing system can be designed by creating separate tables for Customer, Order, OrderItem, Item, Warehouse, and Shipment. Each table is defined with appropriate primary keys to uniquely identify its records. For instance, the Customer table uses custNo as the primary key, and the Order table uses orderNo. To maintain referential integrity, foreign keys are used to establish relationships between tables. For example, OrderTable references custNo from Customer, and OrderItem references orderNo from OrderTable and itemNo from Item. This setup ensures that all related data is consistently connected, and constraints are enforced to prevent orphan records and ensure data integrity .

Defining proper table structures and relationships is vital because it ensures efficient data organization, consistent data integrity, and optimized query performance. Proper structuring supports comprehensive data analysis and prevents redundancy. Inadequately structured databases can lead to data anomalies, inconsistencies, and difficulty in managing and retrieving related data, which may compromise data analysis and reporting capabilities. Poor design also increases the risk of data loss or corruption during updates or deletions .

The SQL `GROUP BY` clause can be combined with aggregation functions like `SUM`, `AVG`, `COUNT`, `MIN`, and `MAX` to deliver data summaries that synthesize information across records. For example, `SELECT c.cname, COUNT(o.orderNo) AS order_count, AVG(o.ord_amt) AS avg_order_amt FROM Customer c JOIN OrderTable o ON c.custNo = o.custNo GROUP BY c.cname;` generates a summary of order counts and average order amounts per customer, using `COUNT` to tally orders and `AVG` to compute the mean order value for each customer group .

The SQL query is: `SELECT c.cname AS custname, COUNT(o.orderNo) AS no_of_orders, AVG(o.ord_amt) AS avg_order_amt FROM Customer c JOIN OrderTable o ON c.custNo = o.custNo GROUP BY c.cname;`. This query illustrates how SQL can be used to perform grouping and aggregation. The `GROUP BY` clause is employed to group the records by customer name (cname), while the aggregation functions `COUNT` and `AVG` are used to calculate the total number of orders and the average order amount per customer respectively .

To delete an item from the Item table and set related entries in the OrderItem table to null, you must first alter the foreign key constraint on OrderItem to allow nulls and enable cascading updates or changes. After that, execute a deletion on the Item table and update affected order item fields to null accordingly. This process demonstrates the complexity involved in maintaining referential integrity and handling cascading actions when modifying data connected by foreign key constraints .

To obtain comprehensive details of all orders received by a company, you can use the SQL query: `SELECT * FROM OrderTable;`. This query selects all columns and records in the OrderTable, which includes information about order numbers, dates, customer numbers, and order amounts. It provides a full overview of the company's order data, demonstrating how SQL can be employed to retrieve complete datasets from a table .

A `JOIN` operation is crucial when integrating related data from different tables. For instance, using the database schema, to produce a list of customer names along with their total number of orders and average order amount, a query like `SELECT c.cname, COUNT(o.orderNo) AS no_of_orders, AVG(o.ord_amt) AS avg_order_amt FROM Customer c JOIN OrderTable o ON c.custNo = o.custNo GROUP BY c.cname;` is essential. This `JOIN` between the Customer and OrderTable consolidates information from both tables, enabling analysis that a single table cannot provide by itself .

The SQL query to list orders shipped from all warehouses in a specific city can be written as: `SELECT s.orderNo FROM Shipment s JOIN Warehouse w ON s.warehouseNo = w.warehouseNo WHERE w.city = 'Delhi' GROUP BY s.orderNo HAVING COUNT(DISTINCT s.warehouseNo) = ( SELECT COUNT(*) FROM Warehouse WHERE city = 'Delhi' );`. This query uses a `JOIN` between the Shipment and Warehouse tables filtered by city. It groups by orderNo and uses the `HAVING` clause to ensure that orders are shipped from all warehouses within the city by comparing the count of distinct warehouse numbers against the total number of warehouses in that city .

Primary keys play a crucial role by uniquely identifying each record in a table, which is essential for ensuring that each entry is distinct and can be precisely referenced. Foreign keys are equally important for maintaining data consistency across multiple tables, as they establish relationships between different tables by referencing primary keys in related tables. This setup ensures that data changes in one table are propagated correctly throughout the database, enforcing referential integrity and preventing data inconsistencies such as orphaned records or conflicting entries .

Challenges in ensuring data integrity in a database with multiple related tables include maintaining consistency and preventing orphan records when records are updated or deleted. These can be addressed by correctly implementing foreign key constraints to ensure data consistency across tables. Additional strategies like cascading actions, such as `ON DELETE CASCADE` or `ON UPDATE CASCADE`, can automate updates and deletions in related tables. Triggers can also be employed to enforce complex business rules that aren't covered by standard constraints .

You might also like