Ex10 DBMS
Ex10 DBMS
Here are
the answers to each part of your question:
a) List the names and addresses of all the customers who have ordered products of
cost more than 500:
sql
Copy code
SELECT c.Name, c.Address
FROM CUSTOMER c
JOIN "ORDER" o ON c.C_ID = o.C_ID
WHERE o.P_COST > 500;
This query selects the Name and Address columns from the CUSTOMER table by joining
it with the "ORDER" table on the C_ID column. It retrieves the names and addresses
of customers who have ordered products with a cost greater than 500.
b) List the names of all the products ordered whose cost is 1,000 or more:
sql
Copy code
SELECT P_Name
FROM "ORDER"
WHERE P_COST >= 1000;
This query selects the P_Name column from the "ORDER" table where the P_COST is
greater than or equal to 1,000. It retrieves the names of products that were
ordered with a cost of 1,000 or more.
c) List the product names which are ordered by customers of "City = Delhi":
sql
Copy code
SELECT o.P_Name
FROM CUSTOMER c
JOIN "ORDER" o ON c.C_ID = o.C_ID
WHERE c.City = 'Delhi';
This query selects the P_Name column from the "ORDER" table by joining it with the
CUSTOMER table on the C_ID column. It retrieves the product names that were ordered
by customers who are located in Delhi.
sql
Copy code
ALTER TABLE CUSTOMER
ADD Email_id VARCHAR(255);
This statement adds a new column named Email_id of type VARCHAR(255) to the
CUSTOMER table.
e) To demonstrate a user-defined function for the above tables, you can create a
function based on your specific requirements. Here's an example of a user-defined
function that calculates the total cost of products ordered by a customer:
sql
Copy code
CREATE OR REPLACE FUNCTION CalculateTotalCost(customer_id INT)
RETURNS INT
AS
$$
DECLARE
total_cost INT := 0;
BEGIN
SELECT SUM(P_COST) INTO total_cost
FROM "ORDER"
WHERE C_ID = customer_id;
RETURN total_cost;
END;
$$
LANGUAGE PL/pgSQL;
This function CalculateTotalCost takes a customer_id as input and returns the total
cost of products ordered by that customer. It calculates the sum of P_COST from the
"ORDER" table for the specified customer. You can customize the function according
to your specific requirements.
Please note that the syntax provided here is a general representation and may need
to be adjusted based on your specific database system.