Information Communication Technology ICT – 18 Databases | e-Consult
18 Databases (1 questions)
Login to see all questions.
Click on a question to view the answer
The SQL code to retrieve the ProductName and Quantity of all products ordered is:
SELECT p.ProductName, o.Quantity
FROM Products p
JOIN Orders o ON p.ProductID = o.ProductID;
Purpose of the Foreign Key: The foreign key ProductID in the Orders table establishes a relationship between the Orders and Products tables. It ensures that every ProductID in the Orders table corresponds to a valid ProductID in the Products table. This maintains data integrity by preventing orders from being placed for products that do not exist. Without the foreign key, the database would be vulnerable to inconsistencies and orphaned records.