SQL CODE BUGS FIXES
Login Auth now implemented Persistance login page AI app.jsx formating
This commit is contained in:
107
SQL_code/Schema.sql
Normal file
107
SQL_code/Schema.sql
Normal file
@@ -0,0 +1,107 @@
|
||||
-- User Entity
|
||||
CREATE TABLE User (
|
||||
UserID INT AUTO_INCREMENT PRIMARY KEY,
|
||||
Name VARCHAR(100) NOT NULL,
|
||||
Email VARCHAR(100) UNIQUE NOT NULL,
|
||||
UCID VARCHAR(20) UNIQUE NOT NULL,
|
||||
Password VARCHAR(255) NOT NULL,
|
||||
Phone VARCHAR(20),
|
||||
Address VARCHAR(255)
|
||||
);
|
||||
|
||||
CREATE TABLE UserRole (
|
||||
UserID INT,
|
||||
Client BOOLEAN DEFAULT True,
|
||||
Admin BOOLEAN DEFAULT FALSE,
|
||||
PRIMARY KEY (UserID),
|
||||
FOREIGN KEY (UserID) REFERENCES Users (UserID) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Product Entity
|
||||
CREATE TABLE Product (
|
||||
ProductID INT PRIMARY KEY,
|
||||
Name VARCHAR(255) NOT NULL,
|
||||
Price DECIMAL(10, 2) NOT NULL,
|
||||
StockQuantity INT,
|
||||
ImageURL VARCHAR(255),
|
||||
UserID INT,
|
||||
Description TEXT,
|
||||
CategoryID INT NOT NULL,
|
||||
FOREIGN KEY (UserID) REFERENCES User (UserID),
|
||||
FOREIGN KEY (CategoryID) REFERENCES Category (CategoryID)
|
||||
);
|
||||
|
||||
-- Category Entity
|
||||
CREATE TABLE Category (CategoryID INT PRIMARY KEY, Name VARCHAR(255));
|
||||
|
||||
-- Review Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Review (
|
||||
ReviewID INT PRIMARY KEY,
|
||||
UserID INT,
|
||||
ProductID INT,
|
||||
Comment TEXT,
|
||||
Rating INT CHECK (
|
||||
Rating >= 1
|
||||
AND Rating <= 5
|
||||
),
|
||||
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (UserID) REFERENCES User (UserID),
|
||||
FOREIGN KEY (ProductID) REFERENCES Product (ProductID)
|
||||
);
|
||||
|
||||
-- Transaction Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Transaction (
|
||||
TransactionID INT PRIMARY KEY,
|
||||
UserID INT,
|
||||
ProductID INT,
|
||||
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
PaymentStatus VARCHAR(50),
|
||||
FOREIGN KEY (UserID) REFERENCES User (UserID),
|
||||
FOREIGN KEY (ProductID) REFERENCES Product (ProductID)
|
||||
);
|
||||
|
||||
-- Recommendation Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Recommendation (
|
||||
RecommendationID_PK INT PRIMARY KEY,
|
||||
UserID INT,
|
||||
RecommendedProductID INT,
|
||||
FOREIGN KEY (UserID) REFERENCES User (UserID),
|
||||
FOREIGN KEY (RecommendedProductID) REFERENCES Product (ProductID)
|
||||
);
|
||||
|
||||
-- History Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE History (
|
||||
HistoryID INT PRIMARY KEY,
|
||||
UserID INT,
|
||||
ProductID INT,
|
||||
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (UserID) REFERENCES User (UserID),
|
||||
FOREIGN KEY (ProductID) REFERENCES Product (ProductID)
|
||||
);
|
||||
|
||||
-- Favorites Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Favorites (
|
||||
FavoriteID INT PRIMARY KEY,
|
||||
UserID INT,
|
||||
ProductID INT,
|
||||
FOREIGN KEY (UserID) REFERENCES User (UserID),
|
||||
FOREIGN KEY (ProductID) REFERENCES Product (ProductID)
|
||||
);
|
||||
|
||||
-- Product-Category Junction Table (Many-to-Many)
|
||||
CREATE TABLE Product_Category (
|
||||
ProductID INT,
|
||||
CategoryID INT,
|
||||
PRIMARY KEY (ProductID, CategoryID),
|
||||
FOREIGN KEY (ProductID) REFERENCES Product (ProductID),
|
||||
FOREIGN KEY (CategoryID) REFERENCES Category (CategoryID)
|
||||
);
|
||||
|
||||
-- Login Authentication table, Store the userID,and a emailed code of user who have not authenticated,
|
||||
CREATE TABLE AuthVerification (
|
||||
UserID INT AUTO_INCREMENT PRIMARY KEY,
|
||||
Email VARCHAR(100) UNIQUE NOT NULL,
|
||||
VerificationCode VARCHAR(6) NOT NULL,
|
||||
Authenticated BOOLEAN DEFAULT FALSE,
|
||||
Date DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
@@ -1,19 +0,0 @@
|
||||
-- Create the main User table with auto-incrementing ID
|
||||
CREATE TABLE Users (
|
||||
ID INT AUTO_INCREMENT PRIMARY KEY,
|
||||
Name VARCHAR(100) NOT NULL,
|
||||
Email VARCHAR(100) UNIQUE NOT NULL,
|
||||
UCID VARCHAR(20) UNIQUE NOT NULL,
|
||||
Password VARCHAR(255) NOT NULL,
|
||||
Phone VARCHAR(20),
|
||||
Address VARCHAR(255)
|
||||
);
|
||||
|
||||
-- Create a separate table for the multi-valued Role attribute
|
||||
CREATE TABLE UserRole (
|
||||
UserID INT,
|
||||
Client BOOLEAN DEFAULT FALSE,
|
||||
Admin BOOLEAN DEFAULT FALSE,
|
||||
PRIMARY KEY (UserID),
|
||||
FOREIGN KEY (UserID) REFERENCES Users (ID) ON DELETE CASCADE
|
||||
);
|
||||
@@ -1,89 +0,0 @@
|
||||
-- User Entity
|
||||
CREATE TABLE User (
|
||||
UserID_PK INT PRIMARY KEY,
|
||||
Password VARCHAR(255),
|
||||
IP VARCHAR(15),
|
||||
Name VARCHAR(100),
|
||||
Email VARCHAR(255),
|
||||
UCID VARCHAR(20),
|
||||
Phone VARCHAR(20),
|
||||
Role VARCHAR(50),
|
||||
Address VARCHAR(255)
|
||||
);
|
||||
|
||||
-- Buyer, Seller, Admin are roles within User (handled by Role attribute)
|
||||
|
||||
-- Product Entity
|
||||
CREATE TABLE Product (
|
||||
ProductID_PK INT PRIMARY KEY,
|
||||
ProductID VARCHAR(50),
|
||||
Price DECIMAL(10, 2),
|
||||
Description TEXT,
|
||||
StockQuantity INT
|
||||
);
|
||||
|
||||
-- Category Entity
|
||||
CREATE TABLE Category (
|
||||
CategoryID_PK INT PRIMARY KEY,
|
||||
Name VARCHAR(100)
|
||||
);
|
||||
|
||||
-- Review Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Review (
|
||||
ReviewID INT PRIMARY KEY,
|
||||
UserID_FK INT,
|
||||
ProductID_FK INT,
|
||||
Comment TEXT,
|
||||
Rating INT CHECK (Rating >= 1 AND Rating <= 5),
|
||||
Date DATE,
|
||||
FOREIGN KEY (UserID_FK) REFERENCES User(UserID_PK),
|
||||
FOREIGN KEY (ProductID_FK) REFERENCES Product(ProductID_PK)
|
||||
);
|
||||
|
||||
-- Transaction Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Transaction (
|
||||
TransactionID_PK INT PRIMARY KEY,
|
||||
UserID_FK INT,
|
||||
ProductID_FK INT,
|
||||
Date DATE,
|
||||
PaymentStatus VARCHAR(50),
|
||||
FOREIGN KEY (UserID_FK) REFERENCES User(UserID_PK),
|
||||
FOREIGN KEY (ProductID_FK) REFERENCES Product(ProductID_PK)
|
||||
);
|
||||
|
||||
-- Recommendation Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Recommendation (
|
||||
RecommendationID_PK INT PRIMARY KEY,
|
||||
UserID_FK INT,
|
||||
RecommendedProductID_FK INT,
|
||||
FOREIGN KEY (UserID_FK) REFERENCES User(UserID_PK),
|
||||
FOREIGN KEY (RecommendedProductID_FK) REFERENCES Product(ProductID_PK)
|
||||
);
|
||||
|
||||
-- History Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE History (
|
||||
HistoryID INT PRIMARY KEY,
|
||||
UserID_FK INT,
|
||||
ProductID_FK INT,
|
||||
TimeStamp DATETIME,
|
||||
FOREIGN KEY (UserID_FK) REFERENCES User(UserID_PK),
|
||||
FOREIGN KEY (ProductID_FK) REFERENCES Product(ProductID_PK)
|
||||
);
|
||||
|
||||
-- Favorites Entity (Many-to-One with User, Many-to-One with Product)
|
||||
CREATE TABLE Favorites (
|
||||
FavoriteID INT PRIMARY KEY,
|
||||
UserID_FK INT,
|
||||
ProductID_FK INT,
|
||||
FOREIGN KEY (UserID_FK) REFERENCES User(UserID_PK),
|
||||
FOREIGN KEY (ProductID_FK) REFERENCES Product(ProductID_PK)
|
||||
);
|
||||
|
||||
-- Product-Category Junction Table (Many-to-Many)
|
||||
CREATE TABLE Product_Category (
|
||||
ProductID_FK INT,
|
||||
CategoryID_FK INT,
|
||||
PRIMARY KEY (ProductID_FK, CategoryID_FK),
|
||||
FOREIGN KEY (ProductID_FK) REFERENCES Product(ProductID_PK),
|
||||
FOREIGN KEY (CategoryID_FK) REFERENCES Category(CategoryID_PK)
|
||||
);
|
||||
Reference in New Issue
Block a user