Shop database, SQL for order table - sql

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

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.

Cannot add foreign key SQL fiddle

Im new to sql, I'm having issues with the foreign key in my table. If any can point out the error, please do, I have no idea what it is.enter image description here
CREATE TABLE donut(
donutId int(5) NOT NULL,
name VARCHAR(20),
descriptioin VARCHAR(60),
price DECIMAL(8,3),
PRIMARY KEY (donutId)
);
CREATE TABLE customer(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),
zip VARCHAR(8),
homephone VARCHAR(15),
cellphone VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid) );
CREATE TABLE customer_order_donut(
orderid int(10) NOT NULL,
customerid int(10),
donutId int(10),
orderdate datetime,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid)
customerid int FOREIGN KEY REFERENCES customer(customerid),
donutId int FOREIGN KEY REFERENCES donut(donutId));
Your FOREIGNKEY syntax is wrong. This should work:
CREATE TABLE donut
(
donutid INT(5) NOT NULL,
name VARCHAR(20),
descriptioin VARCHAR(60),
price DECIMAL(8,3),
PRIMARY KEY (donutid)
);
CREATE TABLE customer
(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),
zip VARCHAR(8),
homephone VARCHAR(15),
cellphone VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid)
);
CREATE TABLE customer_order_donut
(
orderid INT(10) NOT NULL,
customerid INT(10),
donutid INT(10),
orderdate DATETIME,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutid)
);
Check the syntax of the FOREIGN KEY constraints (and add a comma after PRIMARY KEY):
CREATE TABLE customer_order_donut(
orderid int(10) NOT NULL,
customerid int(10),
donutId int(10),
orderdate datetime,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutId)
);

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
)

ORA-00904: "BRANCH_ID": invalid identifier

Why is the create tables not allowing me to add the staff tables, the CONSTRAINT seem logical to me.
CREATE TABLE branch
(
Branch_ID VARCHAR(2),
Branch_Name VARCHAR(20),
Branch_Address VARCHAR(40),
Branch_Postcode VARCHAR(15),
Branch_Telephone NUMBER(15),
Branch_email VARCHAR(40),
Branch_Fax NUMBER(15),
PRIMARY KEY ( Branch_ID )
);
CREATE TABLE staff
(
Staff_ID INT NOT NULL PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
CONSTRAINT BRANCH_fk FOREIGN KEY(Branch_ID ) REFERENCES branch(Branch_ID )
);
ORA-00904: "BRANCH_ID": invalid identifier
I think you forgot to add the Branch_ID field.
You are referencing this one to be your foreign key at the Staff table, but you didn't define it in your staff table yet.
Change staff table definition to:
CREATE TABLE staff
(
Staff_ID INT NOT NULL PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
Branch_ID VARCHAR2(2),
CONSTRAINT BRANCH_fk FOREIGN KEY(Branch_ID) REFERENCES branch(Branch_ID)
);
About Gordon Linoff's comment, check the link below. I modified my answer to match the 'best-practice'.
Difference VARCHAR and VARCHAR2

Need help in the query (oracle 11 g)

First of all, these are my create_table statements.
/*--- Base tables ---*/
CREATE TABLE CUSTOMER (
CUST_ID VARCHAR(10) NOT NULL,
CUST_FNAME VARCHAR(15),
CUST_LNAME VARCHAR(15),
CUST_HP NUMBER(10),
CUST_EMAIL VARCHAR(40),
primary key (CUST_ID)
);
CREATE TABLE STAFF(
STAFF_ID NUMBER(12) NOT NULL,
STAFF_FNAME VARCHAR(15),
STAFF_LNAME VARCHAR(15),
STAFF_DOB Date,
STAFF_AGE NUMBER(2),
STAFF_ADDRESS VARCHAR(70),
STAFF_HP NUMBER(10),
STAFF_SALARY NUMBER(6,2),
primary key (STAFF_ID)
);
CREATE TABLE SUPPLIER(
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_NAME VARCHAR(30),
SUPP_TEL NUMBER(10),
SUPP_EMAIL VARCHAR(25),
SUPP_ADDRESS VARCHAR(80),
SUPP_CITY VARCHAR(20),
SUPP_STATE VARCHAR(10),
SUPP_ZIP NUMBER(5),
primary key (SUPP_ID)
);
CREATE TABLE PRODUCT(
PROD_ID VARCHAR(10) NOT NULL,
PROD_NAME VARCHAR(30),
PROD_DESC VARCHAR(70),
R_UNIT_PRICE NUMBER(3,2) DEFAULT 4.50,
L_UNIT_PRICE NUMBER(3,2) DEFAULT 5.00,
primary key (PROD_ID)
);
CREATE TABLE INGREDIENT(
ING_ID VARCHAR(10) NOT NULL,
ING_NAME VARCHAR(20),
ING_DESC VARCHAR(60),
primary key (ING_ID)
);
CREATE TABLE TOPPING(
TOP_ID VARCHAR(10) NOT NULL,
TOP_NAME VARCHAR(20),
TOP_DESC VARCHAR(40),
TOP_PRICE NUMBER(2,2),
primary key (TOP_ID)
);
/*--- Child tables ---*/
CREATE TABLE ORDERS(
ORDERS_ID VARCHAR(10) NOT NULL,
CUST_ID VARCHAR(10) NOT NULL,
STAFF_ID NUMBER(12) NOT NULL,
TOTAL_PRICE NUMBER(5,2),
ORDERS_DATE DATE,
primary key (ORDERS_ID),
foreign key (CUST_ID) references CUSTOMER(CUST_ID),
foreign key (STAFF_ID) references STAFF(STAFF_ID)
);
CREATE TABLE ORD_DETAIL(
SK1_ID NUMBER(4) NOT NULL,
ORDERS_ID VARCHAR(10) NOT NULL,
PROD_ID VARCHAR(10) NOT NULL,
TOP_ID VARCHAR(10) NOT NULL,
TEATYPE VARCHAR(8),
CUPSIZE CHAR(1),
QTY NUMBER(2),
CONSTRAINT CHK_CUPSIZE CHECK (CUPSIZE = 'R' OR CUPSIZE = 'L'),
CONSTRAINT CHK_TEATYPE CHECK (TEATYPE = 'Oolong' OR TEATYPE = 'Jasmine'),
primary key (SK1_ID),
foreign key (ORDERS_ID) references ORDERS(ORDERS_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (TOP_ID) references TOPPING(TOP_ID)
);
CREATE TABLE PROD_ING(
PROD_ID VARCHAR(10) NOT NULL,
ING_ID VARCHAR(10) NOT NULL,
primary key (PROD_ID , ING_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID)
);
CREATE TABLE ING_SUPP(
ING_ID VARCHAR(10) NOT NULL,
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_PRICE NUMBER(6,2),
primary key (ING_ID, SUPP_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID),
foreign key (SUPP_ID) references SUPPLIER(SUPP_ID)
);
And my query is this:Which employee served the most number of customers?
But when I executed these statements:
SELECT MAX (COUNT (O.CUST_ID)) AS "Served customer"
FROM STAFF S, ORDERS O
WHERE (S.STAFF_ID = O.STAFF_ID)
GROUP BY O.STAFF_ID
HAVING COUNT (O.CUST_ID) > 0;
What I got was there was a column named "Served customer" with the maximum number only.
So, now my question is how to display the employee's ID, first name and last name along with
"Served customer" column.
The question is: "Which employee served the most number of customers? " Your query does not even return information about the employee, only about the number of customers.
The following aggregation returns for each staff member, the number of customers who were served:
SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID;
To get the maximum number we just need to order this by the count and take the first row. In Oracle, this uses a subquery:
SELECT so.*
FROM (SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID
ORDER BY COUNT(O.CUST_ID) DESC
) so
WHERE rownum = 1;