COMM051 Database Systems Assignment 2023/24 1 COMM051 Database Systems Assignment – Sem 1, 2023/24 The purpose of this assignment is to give you practical experience of designing database systems and business intelligence (BI) solutions in the context of a realistic case study where you work with real data. You will work in groups of 6. The Assignment carries 100% of the mark for the module.
As a group you will create and deliver:
this is my assignment,
first table is customercity it contains id, gender,firstname,lastname,dateregistered,city,county,region,country second table is product it contains productgroup, productcode, variantcode,name,cup,size,leglength,colour,price,features,description third table is orderitem which contains ordernumber,ordercreatedate,orderitemnumber,orderstatuscode,customercityid,billinrrency,productgroup,productcode,variantcode,quantity,unitprice,lineitemtotal
this are the tables provided in the assignment.
Normalize the table to 3nf with proper keys and also provide the code to create the normalized tables and the code to insert the data from original 3 tables to normalized tables
To normalize the tables to 3NF, we'll need to remove any trans dependencies and ensure that each non-key attribute is only dependent on the primary key. We will decompose the tables as follows:
CustomerCity Table will hold unique information and city-related details. The CustomerCityID
serves as the primary key since it's unique for each customer’s location.
Product Table will hold product details. Since multiple products can belong to a group and have multiple variants, ProductCode
and VariantCode
together form a composite primary key.
OrderItem Table will be split into two tables— OrderGroup and OrderItem. The key OrderNumber
and OrderItemNumber
ensures line item uniqueness in the OrderItem table.
Here’s how the normalized tables and their attributes will look:
Here is SQL code to create these normalized tables:
CREATE TABLE CustomerCity ( CustomerCityID INT PRIMARY KEY, Gender CHAR(), FirstName VARCHAR(50), LastName VARCHAR(50), DateRegistered DATE, City VARCHAR(50), County VARCHAR(50), Region VARCHAR(50), Country(50) ); CREATE TABLE Product ( ProductGroup VARCHAR(128), ProductCode VARCHAR(255), VariantCode VARCHAR(255), Name VARCHAR(255), Cup(255), Size VARCHAR(255), LegLength VARCHAR(255), Colour VARCHAR(50), Price MONEY, Features VARCHAR(512), Description TEXT, PRIMARY KEY (ProductCode, VariantCode) ); CREATE TABLEGroup ( OrderNumber NVARCHAR(32) PRIMARY KEY, OrderStatusCode CHAR(1 OrderCreateDate DATETIME, BillingCurrency NVARCHAR(3), CustomerCityID INT, FOREIGN KEY (CustomerCityID) REFERENCES CustomerCity(CustomerCityID) ); CREATE TABLE OrderItem ( OrderItemNumber NVARCHAR(32), Quantity INT, UnitPrice MONEY, LineItemTotal MONEY, OrderNumber NVARCHAR(32), Code VARCHAR(255), VariantCode VARCHAR(255), PRIMARY KEY (OrderItemNumber, OrderNumber), FOREIGN KEY (OrderNumber) REFERENCES OrderGroup(OrderNumber), FOREIGN KEY (ProductCode, VariantCode) REFERENCES Product(ProductCode, VariantCode) );
Next, we create insertion to transfer the data from the original tables to the normalized tables. However, because I can't execute or store data, I will give a generic outline of the insert statements that would be used:
-- Insert into CustomerCity (Select distinct customer-related information from OrderItem) INSERT INTO CustomerCity (CityID, Gender, FirstName, LastName, DateRegistered, City, County, Region, Country) SELECT DISTINCT CustomerCityID, ... FROM OriginalTable GOES HERE; -- Insert into Product (Select product-related information from OrderItem) INSERT INTO Product (ProductGroup, ProductCode, VariantCode, Name, Cup, Size LegLength, Colour, Price, Features, Description) SELECT DISTINCT ProductGroup, ProductCode, VariantCode, ... FROM OriginalTable GOES HERE; -- Insert into OrderGroup (Select distinct order-related information fromItem excluding line items) INSERT INTO OrderGroup (OrderNumber, OrderStatusCode, OrderCreateDate, BillingCurrency, CustomerCityID) SELECT DISTINCT OrderNumber, OrderStatus, OrderCreateDate, BillingCurrency, CustomerCityID FROM OriginalTable GOES HERE; -- Insert into OrderItem (Select order item information from OrderItem) INSERT INTO OrderItem (OrderItemNumber, Quantity, UnitPrice, LineItem, OrderNumber, ProductCode, VariantCode) SELECT OrderItemNumber, Quantity, UnitPrice, LineItemTotal, OrderNumber, ProductCode VariantCode FROM OriginalTable GOES HERE;
Note You should change the OriginalTable GOES HERE
parts to the actual tables or joined tables if necessary, based on the data source. These statements should also consider different cases like the absence of some values and how to fill
normalize it more so that i get the customer details and the city details in different table. also normalize all the tables such that i get atleast 6 to 8 tables after normalization. provide the code to create the normalized tables and to transfer the data from original table to normalized tables
To further normalize the tables, we can separate customer information from city details and ensure that all non-key attributes exclusively depend on the primary key. Here is a possible approach to normalizing the schema into 6-8 tables:
Contains personal information about the customer.
CREATE TABLE Customer ( CustomerID INT PRIMARY KEY, Gender CHAR(1), FirstName VARCHAR(50), LastName VARCHAR(50), DateRegistered DATE );
Contains city-related information.
CREATE TABLE City ( CityID INT PRIMARY KEY, CityName VARCHAR(50), County VARCHAR(50), Region VARCHAR(50), Country VARCHAR(50) );
Links customers to their cities.
CREATE TABLE CustomerCity ( CustomerCityID INT PRIMARY KEY, CustomerID INT, CityID INT, FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID), FOREIGN KEY (CityID) REFERENCES City(CityID) );
Contains information about each product's attributes.
CREATE TABLE Product ( ProductCode VARCHAR(255) PRIMARY KEY, ProductGroup VARCHAR(128), Name VARCHAR(255), Description TEXT );
Contains information about different variants for the products.
CREATE TABLE Variant ( VariantCode VARCHAR(255) PRIMARY KEY, ProductCode VARCHAR(255), CupSize VARCHAR(255), Size VARCHAR(255), LegLength VARCHAR(255), Colour VARCHAR(50), Price MONEY, Features VARCHAR(512), FOREIGN KEY (ProductCode) REFERENCES Product(ProductCode) );
Contains details of each order.
CREATE TABLE Order ( OrderNumber NVARCHAR(32) PRIMARY KEY, OrderStatusCode CHAR(1), OrderCreateDate DATETIME, BillingCurrency NVARCHAR(3) );
Links orders to customers.
CREATE TABLE OrderCustomer ( OrderNumber NVARCHAR(32), CustomerCityID INT, FOREIGN KEY (OrderNumber) REFERENCES Order(OrderNumber), FOREIGN KEY (CustomerCityID) REFERENCES CustomerCity(CustomerCityID), PRIMARY KEY(OrderNumber, CustomerCityID) );
Contains details of each item within an order.
CREATE TABLE OrderItem ( OrderItemNumber NVARCHAR(32), OrderNumber NVARCHAR(32), VariantCode VARCHAR(255), Quantity INT, UnitPrice MONEY, LineItemTotal MONEY, FOREIGN KEY (OrderNumber) REFERENCES Order(OrderNumber), FOREIGN KEY (VariantCode) REFERENCES Variant(VariantCode), PRIMARY KEY(OrderItemNumber, OrderNumber) );
After creating the tables, you would need to populate them with data from the original table. Below are the outlines for the SQL INSERT statements for this task:
-- Populate Customer Table INSERT INTO Customer (CustomerID, Gender, FirstName LastName, DateRegistered) SELECT DISTINCT CustomerID, Gender, FirstName, LastName, DateRegistered FROM OriginalTable; -- Populate City Table INSERT INTO City (CityID, CityName, County, Region, Country) SELECT DISTINCT CityID, CityName, County, Region, Country FROM OriginalTable; -- Populate CustomerCity Table INSERT INTO CustomerCity (CustomerCityID, CustomerID, CityID) SELECT DISTINCT CustomerCityID, CustomerID, CityID FROM OriginalTable; -- Populate Product Table INSERT INTO Product (ProductCode, ProductGroup, Name, Description) SELECT DISTINCT ProductCode, ProductGroup, Name, Description FROM OriginalTable; -- Populate Variant Table INSERT INTO Variant (VariantCode, ProductCode, CupSize, Size, LegLength, Colour, Price, Features) SELECT DISTINCT VariantCode, ProductCode, CupSize, Size, LegLength, Colour, Price, Features FROM OriginalTable; -- Populate Order Table INSERT INTO Order (OrderNumber, OrderStatusCode, OrderCreateDate, BillingCurrency) SELECT DISTINCT OrderNumber, OrderStatusCode, OrderCreateDate, BillingCurrency FROM OriginalTable; -- Populate OrderCustomer Table INSERT INTO OrderCustomerOrderNumber,CityID) SELECT DISTINCT OrderNumber, CustomerCityID FROM OriginalTable; -- Populate OrderItem Table INSERT INTO OrderItem (OrderItemNumber, OrderNumber, VariantCode, Quantity, UnitPrice, LineItemTotal) SELECT OrderItemNumber, OrderNumber, VariantCode, Quantity, UnitPrice, LineItemTotal FROM OriginalTable;
Ensure when you run these queries, you replace OriginalTable
with the name of your original data source. Additional WHERE conditions or JOINs may be required depending on the structure of the data in the original table and its normalization state prior to these operations. Also, make sure to handle any data conversion or integrity issues such as foreign key constraints carefully during
create a er diagram for above mentioned code
I'm currently unable to visualize images or diagrams. However, I can provide you with a textual representation of the ER diagram. Here's a simplified text-based ER diagram representing the relationships between the tables mentioned:
CustomerID [PK] Gender FirstName LastName Registered
CityID [PK] CityName County Region Country
CustomerCityID [PK] CustomerID [FKCityID [FK]
ProductCode [PK] ProductGroup Name Description
OrderNumber [PK] OrderStatusCode OrderCreateDate BillingCurrency
OrderNumber [PK, FK] CustomerCityID [PK, FK]
OrderItemNumber [PK] OrderNumber [PK, FK] VariantCode [] Quantity UnitPrice LineItemTotal
In this representation: -PK] denotes the Primary Key - [FK] denotes the Foreign Key representation shows the entity names, their attributes, and the relationships between the entities, including the primary and foreign keys. Each relationship is indicated by the Foreign Key (FK) connecting to the Primary Key (PK) of another entity. You can use this as a guide to create a graphical ER diagram using a software tool like Lucidchart, draw.io, or any other ER diagram modeling tool.