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

Data modelling

data modelling

Uploaded by

swagatasas
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)
2 views

Data modelling

data modelling

Uploaded by

swagatasas
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/ 11

Designing a Relational Database with Three Tables

Let's consider a simple e-commerce scenario involving customers, orders, and


products.
1. Customers Table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);
2. Products Table
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100),
Price DECIMAL(10, 2)
);
3. Orders Table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
ProductID INT,
Quantity INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);
In this schema:
 Each customer can place multiple orders.
 Each order is associated with a single product.
 The Orders table establishes relationships between customers and
products.

SQL Operations
1. INSERT: Adding Data
-- Insert into Customers
INSERT INTO Customers (CustomerID, Name, Email)
VALUES (1, 'Alice Smith', '[email protected]');

-- Insert into Products


INSERT INTO Products (ProductID, ProductName, Price)
VALUES (101, 'Laptop', 750.00);

-- Insert into Orders


INSERT INTO Orders (OrderID, CustomerID, ProductID, Quantity, OrderDate)
VALUES (1001, 1, 101, 2, '2025-05-20');
2. SELECT: Retrieving Data
-- Retrieve all customers
SELECT * FROM Customers;

-- Retrieve orders with customer and product details


SELECT o.OrderID, c.Name, p.ProductName, o.Quantity, o.OrderDate
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
INNER JOIN Products p ON o.ProductID = p.ProductID;
3. UPDATE: Modifying Data
-- Update product price
UPDATE Products
SET Price = 800.00
WHERE ProductID = 101;
4. DELETE: Removing Data
-- Delete an order
DELETE FROM Orders
WHERE OrderID = 1001;
5. ORDER BY: Sorting Results
sql
CopyEdit
-- Retrieve products sorted by price descending
SELECT * FROM Products
ORDER BY Price DESC;

🔗 INNER JOIN: Combining Data from Multiple Tables


An INNER JOIN retrieves records that have matching values in both
tables.Wikipedia+2W3Schools+2GeeksforGeeks+2
sql
CopyEdit
SELECT o.OrderID, c.Name, p.ProductName, o.Quantity
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
INNER JOIN Products p ON o.ProductID = p.ProductID;
This query lists all orders along with the corresponding customer names and
product details.Stack Overflow
📊 Aggregate Functions: Summarizing Data
Aggregate functions perform calculations on multiple rows of a table's column
and return a single value.
1. COUNT: Number of Orders per Customer
sql
CopyEdit
SELECT c.Name, COUNT(o.OrderID) AS TotalOrders
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
GROUP BY c.Name;
2. SUM: Total Sales per Product
sql
CopyEdit
SELECT p.ProductName, SUM(o.Quantity * p.Price) AS TotalSales
FROM Products p
INNER JOIN Orders o ON p.ProductID = o.ProductID
GROUP BY p.ProductName;
3. AVG: Average Order Quantity
sql
CopyEdit
SELECT AVG(Quantity) AS AverageQuantity
FROM Orders;
4. MAX and MIN: Highest and Lowest Product Prices
sql
CopyEdit
SELECT MAX(Price) AS HighestPrice, MIN(Price) AS LowestPrice
FROM Products;
🧠 Summary
By creating related tables and utilizing SQL commands, you can efficiently
manage and analyze data within a relational database. Understanding how to
perform SELECT, INSERT, UPDATE, DELETE, use ORDER BY, implement INNER
JOINs, and apply aggregate functions like COUNT, SUM, AVG, MAX, and MIN is
fundamental for database operations.
If you need further assistance or examples on more advanced topics like
subqueries, indexes, or stored procedures, feel free to ask!

Sources
I prefer this response
ChatGPT
Response 2
Creating and manipulating relational databases using SQL is fundamental for
managing structured data effectively. Below is a comprehensive guide that
covers:
 Creating a relational database with at least three tables.
 Using SQL commands: SELECT, INSERT, DELETE, UPDATE, ORDER BY.
 Performing INNER JOIN operations across two tables.
 Utilizing aggregate functions: COUNT, SUM, AVG, MAX,
MIN.GeeksforGeeks

Database Schema: Online Store Example


Let's design a simple online store database with three interconnected tables:
1. Customers: Stores customer information.
2. Orders: Records customer orders.
3. OrderItems: Details items within each order.BCcampus Open
Publishing+15en.wikipedia.org+15GeeksforGeeks+15W3Schools+14en.w
ikipedia.org+14Medium+14DataCamp
1. Creating Tables
sql
CopyEdit
-- Customers table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
);

-- Orders table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

-- OrderItems table
CREATE TABLE OrderItems (
OrderItemID INT PRIMARY KEY,
OrderID INT,
ProductName VARCHAR(100),
Quantity INT,
Price DECIMAL(10, 2),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);
2. Inserting Data
sql
CopyEdit
-- Insert into Customers
INSERT INTO Customers (CustomerID, Name, Email)
VALUES
(1, 'Alice Smith', '[email protected]'),
(2, 'Bob Johnson', '[email protected]');

-- Insert into Orders


INSERT INTO Orders (OrderID, OrderDate, CustomerID)
VALUES
(101, '2025-05-01', 1),
(102, '2025-05-02', 2);

-- Insert into OrderItems


INSERT INTO OrderItems (OrderItemID, OrderID, ProductName, Quantity, Price)
VALUES
(1001, 101, 'Laptop', 1, 1200.00),
(1002, 101, 'Mouse', 2, 25.00),
(1003, 102, 'Keyboard', 1, 45.00);

🔍 Data Retrieval with SELECT


a. Retrieve all customers
sql
CopyEdit
SELECT * FROM Customers;
b. Retrieve orders with customer names
sql
CopyEdit
SELECT Orders.OrderID, Orders.OrderDate, Customers.Name
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
c. Retrieve order items for a specific order
sql
CopyEdit
SELECT ProductName, Quantity, Price
FROM OrderItems
WHERE OrderID = 101;

✏️Updating Records with UPDATE


Update a customer's email
sql
CopyEdit
UPDATE Customers
SET Email = '[email protected]'
WHERE CustomerID = 1;
Update the price of a product in an order
sql
CopyEdit
UPDATE OrderItems
SET Price = 1300.00
WHERE OrderItemID = 1001;

Deleting Records with DELETE


Delete an order item
sql
CopyEdit
DELETE FROM OrderItems
WHERE OrderItemID = 1002;
Delete a customer (ensure related orders and items are handled
appropriately)
sql
CopyEdit
DELETE FROM Customers
WHERE CustomerID = 2;
Note: Ensure referential integrity is maintained when deleting records with
foreign key dependencies.

📊 Sorting Results with ORDER BY


Retrieve all orders sorted by date descending
sql
CopyEdit
SELECT * FROM Orders
ORDER BY OrderDate DESC;
🔗 Joining Tables with INNER JOIN
Retrieve order details with customer and item information
sql
CopyEdit
SELECT Orders.OrderID, Customers.Name, OrderItems.ProductName,
OrderItems.Quantity, OrderItems.Price
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
INNER JOIN OrderItems ON Orders.OrderID = OrderItems.OrderID;

📈 Aggregate Functions
a. Count total number of customers
sql
CopyEdit
SELECT COUNT(*) AS TotalCustomers FROM Customers;
b. Sum total sales amount
sql
CopyEdit
SELECT SUM(Quantity * Price) AS TotalSales FROM OrderItems;
c. Average order value
sql
CopyEdit
SELECT AVG(OrderTotal) AS AverageOrderValue
FROM (
SELECT OrderID, SUM(Quantity * Price) AS OrderTotal
FROM OrderItems
GROUP BY OrderID
) AS OrderSums;
d. Maximum and minimum product price
sql
CopyEdit
SELECT MAX(Price) AS MaxPrice, MIN(Price) AS MinPrice FROM OrderItems;

You might also like