ERD for ShoppingCart - sql

I'm a bit confused about the entity relationship diagram for my application.
So far I have made tables for Product,Customer,Category
Should I make a table for UserAccounts that holds the id and password for the Customer or directly place the the id and password in the Customer table?
Secondly, each customer will have his own cart. So I have made a CartItem table
CartItemId,
ProductId,
CategoryId,
Description,
UnitPrice,
TotalPrice
But this table is not associating a customer with the cartItem in the shoppingcart. So Should i add the CustomerId here also?
Is there a need for Description and unit price, because those are already defined in the Product Table?
and for Cart Table,
CartId,
CartItemId,
CustomerId
I need to clear up a few things, before I find any mistakes in my application.
DDL:
CREATE DATABASE ShoppingCart
Create Table Customer(
CustomerId int PRIMARY KEY,
Firstname varchar(50),
Lastname varchar(50),
Address varchar(50),
City varchar(50),
State varchar(50),
Country varchar(50),
Mobile varchar(50),
Phone varchar(50),
Email varchar(50)
)
Create Table UserAccount(
UserName varchar(50),
PasswordHash varchar(50)
)
Create Table Category(
CategoryId int PRIMARY KEY,
CategoryName varchar(50),
CategoryDescription varchar(50),
CategoryImage varchar(50)
)
Create Table Products(
ProductId int PRIMARY KEY,
ProductName varchar(50),
Description varchar(50),
CategoryId int,
UnitPrice money,
DateAdded datetime,
thumn varchar(50),
CONSTRAINT fk_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId)
)
Create Table CartItem(
CartItemId int,
ProductId int,
CategoryId int,
Description varchar(50),
UnitPrice money,
TotalPrice money,
PRIMARY KEY (CartItemId),
CONSTRAINT fk_ProductId FOREIGN KEY (ProductId)
REFERENCES Products(ProductId),
CONSTRAINT fk_CartItem_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId)
)
Create Table Cart(
CartId int,
CartItemId int,
ProductId int,
CategoryId int,
Description varchar(50),
UnitPrice money,
TotalPrice money,
PRIMARY KEY (CartId),
CONSTRAINT fk_CartItemId FOREIGN KEY (CartItemId)
REFERENCES CartItem(CartItemId),
CONSTRAINT fk_CartItem_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId),
PRIMARY KEY (ProductId),
CONSTRAINT fk_CartItemId FOREIGN KEY (ProductId)
REFERENCES CProducts(productId)
)

#1 Do not store the password - store a salted hash.
Do not make the customer Id in the cart item table if it's already obtained through going through Cart table.
Right now, I think you are confused with having a Cart and CartItem table which are nearly identical.
UserAccount appears to be orphaned. It needs to be either in the Customer table or somehow linked.
I would drop CategoryId, Description, UnitPrice and TotalPrice from CartItem (since they are in Product) unless products are customized into the shopping cart for each customer. You also run the risk of letting a user check out with a product at an old price (unless you treat carts as quotes, which is a whole other business domain).
A CartItem needs a way to get to its Cart or Customer.
Here's what I would do:
Category (CategoryID (PK), etc.)
Customer (CustomerID (PK), etc.)
Product (ProductID (PK), CategoryID (FK), etc)
Cart (CartID (PK), CustomerID (FK), ProductID(FK), Quantity)
When a cart becomes an order, typically an order detail or invoice detail table will lock in the price, and perhaps the name of the product, and the cart will be emptied for a custoemr. A lot of this depends on further business needs.

Related

How to get details for a customer that is both a seller and a buyer

I have a database which contains customers that are both buyers and sellers.
the customers can buy and sell houses.
I need to get details for the customers separately and still have a connection between them so i could know which customer sold to which customer.
In case a customer buys a house from a different customer how can i get the details for each customer separately?
The table I made so far for the customers is :
CREATE TABLE Customers (
SellerID int,
BuyerID int,
HouseID int,
SaleID int,
FirstName varchar (50),
LastName varchar (50),
Adress varchar (50),
BirthDate Date,
City varchar (50),
HomePhone varchar (50),
PRIMARY KEY (SellerID, BuyerID),
FOREIGN KEY (HouseID) REFERENCES House(HouseID),
FOREIGN KEY (SaleID) REFERENCES Sale(SaleID),
);
I have a sale table
CREATE TABLE Sale (
SaleID int,
SalesManID int,
SaleDate Date,
SalePrice int,
PRIMARY KEY (SaleID),
FOREIGN KEY (SalesManID) REFERENCES SalesMan(SalesManID),
);
I have a table that contains both sale and customers
CREATE TABLE SaleToCustomers (
SaleID int,
CustomersID int,
PRIMARY KEY (SaleID, CustomersID)
);
Your entities are confused. A customer should be a person, with information about the persion. Then Sales should refer to it twice, once for buyers and o once for sellers:
CREATE TABLE Customers (
CustomerId int PRIMARY KEY,
FirstName varchar(50),
LastName varchar(50),
Adress varchar(50),
BirthDate Date,
City varchar(50),
HomePhone varchar(50)
);
CREATE TABLE Sales (
SaleID int PRIMARY KEY,
SellerId int,
BuyerId int,
HouseId int,
SaleDate Date,
SalePrice int,
FOREIGN KEY (SellerId) REFERENCES Customers(CustomerId),
FOREIGN KEY (BuyerId) REFERENCES Customers(CustomerId),
FOREIGN KEY (HouseID) REFERENCES House(HouseID)
);
In other words, "buyer" and "seller" are attributes of a sale, not of a person.

SQL Can't create Foreign key

I'm sure my question would be kinda silly, but I can't solve it. Here is my issue:
create table Product
(
ProductID int PRIMARY KEY,
ProductName varchar(16),
RetrailPrice int,
WholesalePrice int,
MonthDelivery int,
Waste int,
StorageName varchar(16)
)
create table Storage
(
StorageID int PRIMARY KEY,
StorageName varchar(16),
City varchar(16),
Employees int,
Area int,
ProductID int
)
And of course I want to link them
alter table Product
add FOREIGN KEY (StorageName) references Storage(StorageName)
But I can't do it, I'm wrong somewhere :/
You have to reference the PK from the Storage table. StorageName is a poor choice for a FK. What if there are Storages with the same name in different cities?
Replace the StorageName in the Product table with StorageID
create table Product(
ProductID int PRIMARY KEY,
ProductName varchar(16),
RetrailPrice int,
WholesalePrice int,
MonthDelivery int,
Waste int,
StorageID int) --this has chnaged, it will hold the id/PK of the Storage
create table Storage(
StorageID int PRIMARY KEY,
StorageName varchar(16),
City varchar(16),
Employees int,
Area int,
ProductID int)
And then:
alter table Product
add FOREIGN KEY (StorageID) references Storage(StorageID)

Database design with creating a product that has two prices

I'm creating a database design for our water refilling system and I'm just new to databases. I am stuck with creating table that provides two different prices for a product. To further explain my problem, here's an example, a product's ('5 GALLON') price changes when it is delivered or bought on point by a customer. For example a delivered ('5 GALLON') is 45 pesos while a bought on point gallon is only 40 pesos. Can someone help me please?
Here's my codes so far
create table Product (
product_id int primary key,
prodtype_id int,
product_name varchar(55),
product_quantity int
)
-----NOT SURE IF THESE TWO TABLES ARE CORRECT
create table DeliveryPrice (
prod_id int,
product_price money,
foreign key (prod_id) references Product
)
create table OnPointPrice(
prod_id int,
product_price money,
foreign key (prod_id) references Product
)
You're likely better off just having the two prices in the Product table. They are attributes of the product, so that's where they belong.
Also, you should specify which columns are NOT NULL in your database (which should be most of them).
So is this correct?
create table Product(
product_id int primary key,
product_name varchar(55) not null,
product_quantity int not null,
pickup_price money not null,
delivery_price money not null
)
create table Customer(
customer_id int primary key,
customer_name varchar (255) not null,
customer_address varchar(200),
customer_phone int
)
create table INVOICE(
inv_number int primary key,
customer_id varchar(5),
foreign key (customer_id) references Customer,
inv_date date not null,
bought_mode char(10) not null
)
create table LINE(
INV_NUMBER int,
foreign key (INV_NUMBER) references INVOICE,
LINE_NUMBER INT not null,
PRIMARY KEY (INV_NUMBER, LINE_NUMBER),
line_quantity int not null,
line_price money not null
)

How to find the total amount across table with same foreign key?

So I am doing an assignment on managing a shop. The problem I'm having is to display the total money of the item the customer bought in a table. Here are my code
create database assingment
use database assignment
create table Product
(
ProductID varchar(10),
PRoductName nvarchar(50),
Price money,
constraint PK_Product primary key (ProductID)
)
go
create table Export
(
ExportID varchar(10),
ImportCompany nvarchar(50),
Address nvarchar(50),
constraint PK_Export primary key (ExportID)
)
go
create table ExportDetail
(
ExportID varchar(10),
ProductID varchar(10),
Quantity int,
constraint FK_ED_E foreign key (ExportID) references Export(ExportID),
constraint FK_ED_P foreign key (ProductID) references Product(ProductID),
)
go
So I want to display the sum of the total (quantity*price) on ExportDetail as the amount on Export. For example:
ExportDetail
ExportID ProductID Quantity Total
0234 001 10 10000
0234 002 23 4600
the result on Export would be
Export
Total 14600
Is there a way to display it on the table without using select?

Shop database, SQL for order table

I have a University project and I have to create a DB for a plant shop.
I have a problem with the order table. At the moment it only allows a customer to buy one product at a time but in real life a customer buys many products at a time.
For example,
We have a customer John Doe, and he bus two products that are in the product table. How do I pull those two (or more) products and add them to one order table?
Below is the SQL code I wrote:
CREATE TABLE customer(
customer_id INT(3),
customer_fname VARCHAR(20),
customer_lname VARCHAR(20),
customer_gender CHAR(1),
customer_tel VARCHAR(20),
customer_email VARCHAR(30),
customer_dateJoined DATE,
address_id INT(3),
PRIMARY KEY(customer_id),
INDEX(customer_id),
FOREIGN KEY(customer_id) REFERENCES address);
CREATE TABLE address(
adress_id INT(3),
customer_street VARCHAR(30),
customer_town VARCHAR(30),
customer_postcode CHAR(7),
PRIMARY KEY(address_id),
INDEX(address_id),
FOREIGN KEY(address_id) REFERENCES customer(address_id),
FOREIGN KEY(address_id) REFERENCES employee(address_id));
CREATE TABLE product(
product_id INT(5),
product_name VARCHAR(20),
product_season VARCHAR(15),
product_price NUMERIC(4,2),
product_origin VARCHAR(15),
product_type VARCHAR(15),
product_inStock BOOLEAN,
PRIMARY KEY(product_id),
INDEX(product_id));
CREATE TABLE orders(
order_id INT(3),
customer_id INT(3),
employee_id INT(3),
product_name VARCHAR(20),
quantity INT(4),
order_date TIMESTAMP,
PRIMARY KEY(order_id),
INDEX(order_id));
CREATE TABLE employee(
employee_id INT(3),
employee_fname VARCHAR(20),
employee_lname VARCHAR(20),
address_id INT (3),
employee_pay NUMERIC(2,2),
employee_daysOff INT(2),
employee_hoursWorked INT(3),
PRIMARY KEY(staff_id),
INDEX(staff_id));
You have to create Kettle Table customer_orders, in this table you store customer_id and order_id and connect them with foreign keys to the customer and orders tables.
Like in the following query:
CREATE TABLE customer_orders(
customer_id INT(3),
order_id INT(3),
PRIMARY KEY(customer_id, order_id),
FOREIGN KEY(customer_id) REFERENCES customer(customer_id),
FOREIGN KEY(order_id) REFERENCES orders(order_id)
);
CREATE TABLE sales.stores (
store_id INT IDENTITY (1, 1) PRIMARY KEY,
store_name VARCHAR (255) NOT NULL,
phone VARCHAR (25),
email VARCHAR (255),
street VARCHAR (255),
city VARCHAR (255),
state VARCHAR (10),
zip_code VARCHAR (5)
);
One way to design this is to have two tables for Orders.
OrderHeader :- This contains order id with customer details.
OrderItem :- Contains Line items per order.
OrderHeader fields could be
order_id //primary key
customer_id
employee_id
order_date
OrderItem :- Fields could be
order_id //composite key
line_Item_id //composite key
product_id,
product_quant