Advertisement
Radeen10-_

SQL for database of a Store

Oct 3rd, 2021
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.88 KB | None | 0 0
  1. create database shopee_improvisation;
  2. CREATE TABLE shopee_improvisation.Product_Category (
  3.   Product_ID nvarchar(255) not null,
  4.   Product_Price int,  
  5.   Product_Type varchar(255),
  6.   primary key(Product_ID)
  7. );
  8.  
  9. CREATE TABLE shopee_improvisation.Customer (
  10.   Customer_ID nvarchar(255) not null,
  11.   Customer_Name varchar(255),
  12.   Email nvarchar(255),
  13.   Telephone_No int,
  14.   Address nvarchar(255),
  15.   City varchar(255),
  16.   Postal_Code int,
  17.   primary key(Customer_ID)
  18. );
  19.  
  20. CREATE TABLE shopee_improvisation.Shipment_Information (
  21.   Product_ID nvarchar(255) not null,
  22.   Shipment_ID nvarchar(255) not null,
  23.   Shipment_Price int,
  24.   primary key(Shipment_ID)
  25. );
  26.  
  27. CREATE TABLE shopee_improvisation.Payment (
  28.   Transaction_ID nvarchar(255) not null,
  29.   Payment_Type char(255),
  30.   Payment_Date date,
  31.   Total_Price int,
  32.   Shipment_ID nvarchar(255) not null,
  33.   Product_ID nvarchar(255) not null,
  34.   primary key(Transaction_ID)
  35. );
  36.  
  37. CREATE TABLE shopee_improvisation.Staff(
  38.   Staff_ID nvarchar(255) not null,
  39.   Staff_Name varchar(255),
  40.   Position varchar(255),
  41.   Email nvarchar(255),
  42.   primary key(Staff_ID)
  43. );
  44.  
  45. drop table shopee_improvisation.Defective_Product;
  46.  
  47. CREATE TABLE shopee_improvisation.Defective_Product(
  48.   Product_ID nvarchar(255) not null,
  49.   Customer_ID nvarchar(255) not null,
  50.   Recieve_Date date,
  51.   primary key(Product_ID)
  52. );
  53. CREATE TABLE shopee_improvisation.Supplier (
  54.   Supplier_ID nvarchar(255) not null,
  55.   Supplier_Name varchar(255),
  56.   Email nvarchar(255),
  57.   Telephone_No int,
  58.   primary key(Supplier_ID)
  59. );
  60.  
  61. CREATE TABLE shopee_improvisation.Product_Information (
  62.   Product_ID nvarchar(255) not null,
  63.   Product_Name varchar(255),
  64.   Product_Category varchar(255),
  65.   Product_Price int not null,
  66.   Order_Quantity int not null,
  67.   primary key(Product_ID)
  68. );
  69.  
  70. CREATE TABLE shopee_improvisation.Store(
  71.   Product_ID nvarchar(255) not null,
  72.   Product_Status varchar(255),
  73.   Shipment_ID nvarchar(255) not null,
  74.   Payment_Status varchar(255),
  75.   Product_Type varchar(255),
  76.   primary key(Product_ID)
  77. );
  78.  
  79.  
  80. drop table shopee_improvisation.Store;
  81.  
  82.  
  83.  
  84. CREATE TABLE shopee_improvisation.Delivery_and_Supply_System (
  85.   Shipment_ID nvarchar(255) not null,
  86.   Shipment_Type varchar(255),
  87.   Delivery_Company_Name varchar(255),
  88.   Recieve_Date date,
  89.   Delivery_Status varchar(255),
  90.   primary key(Shipment_ID)
  91. );
  92.  
  93. CREATE TABLE shopee_improvisation.Order_Information (
  94.   Customer_ID nvarchar(255) not null,
  95.   Total_Ordered_Product_Price int,
  96.   Required_Date date,
  97.   Order_Date date,
  98.   Order_Id nvarchar(255) not null,
  99.   Product_ID nvarchar(255) not null,
  100.   primary key(order_id)
  101. );
  102.  
  103. alter table shopee_improvisation.Order_Information
  104.     add  Customer_ID nvarchar(20)
  105.         after Trace_ID;
  106. alter table shopee_improvisation.Order_Information
  107.     add Trace_ID nvarchar(255) not null
  108.         after Product_ID;
  109.  
  110.  
  111. #add column Trace id in order_information which is the join of customer_id and order_id
  112.  
  113. create table shopee_improvisation.Order_trace
  114. as( select OI.Customer_ID, Order_ID,Trace_id
  115.     from shopee_improvisation.Order_Information OI
  116.     join shopee_improvisation.customer
  117.     where customer.Customer_ID=OI.Customer_ID);
  118.    
  119. alter table shopee_improvisation.payment
  120.     add TRX_ID nvarchar(20)
  121.     after Shipment_ID;
  122. select p.Product_ID,Customer_ID,TRX_ID
  123. from shopee_improvisation.payment p
  124. join shopee_improvisation.order_information;
  125.  
  126. alter table shopee_improvisation.delivery_and_supply_system
  127.     add verfication_id nvarchar(20)
  128.     after shipment_id ;
  129.  
  130. select oi.Product_ID, Customer_ID,verfication_id
  131. from shopee_improvisation.delivery_and_supply_system ds
  132. inner join shopee_improvisation.shipment_information si
  133.     on ds.Shipment_ID=si.Shipment_id
  134. inner join shopee_improvisation.order_information oi
  135.     on si.Product_ID=oi.Product_ID;
  136.    
  137. #order of a customer for test. Suppose the customer's name is David Beckham
  138.  
  139. ----- sign up(customer)----------
  140. insert into shopee_improvisation.customer
  141. values('C107','David Beckham','david@gmail.com','372937','21 Nasirabad', 'Kualalampur','4377');
  142. #Now the customer identity as tamim shah for not doing lengthy process
  143. #Tamim Shah wants to buy a laptop. The whole process will be coded below.
  144. #Query for products
  145.  
  146. select Product_Type,Product_Name,PC.Product_Price,PC.Product_ID
  147. from shopee_improvisation.product_category PC
  148. join shopee_improvisation.product_information PI
  149. where PC.Product_ID=PI.Product_ID and Product_Type='Electronics' and Product_Name='Laptop';
  150.  
  151. #see the price of electronics product
  152. select product_category.Product_Price,Product_Name,product_information.Product_ID
  153. from shopee_improvisation.product_information
  154. join shopee_improvisation.product_category
  155. on product_information.Product_ID=product_information.Product_ID and Product_Category='Electronics';
  156.  
  157. # Selected the desire product
  158. select*
  159. from shopee_improvisation.order_information
  160. join shopee_improvisation.product_information
  161. where order_information.Product_ID=product_information.Product_ID and product_information.Product_ID='P111';
  162. #Then he will get shipment information
  163. select*
  164. from shopee_improvisation.shipment_information
  165. where Product_ID="P111";
  166.  
  167. #Then he has to make the payment and will keep the TRX_ID
  168.  
  169. select TRX_ID
  170. from shopee_improvisation.payment
  171. where Product_ID='P111' and Shipment_ID='SP101';
  172.  
  173. #Now the staff will check the order database and then check the store
  174.  
  175. select s.Product_ID,Payment_Status,Product_Status,Product_Type
  176. from shopee_improvisation.order_information oi
  177. join shopee_improvisation.store s
  178. where oi.Product_ID=s.Product_ID and s.Product_ID='P111';
  179.  
  180. #So a staff is informing the supplier who deliver the selected product
  181. #for improvising the experience overall we have to specify the seller with selected product
  182.  
  183. alter table shopee_improvisation.supplier
  184.     add Product_ID nvarchar(255)
  185.     after Supplier_ID ;
  186. select*
  187. from shopee_improvisation.supplier
  188. where supplier.Product_ID='P111';
  189.  
  190. #will provide in the store
  191. #One staff have to inform the delivery company
  192. create table shopee_improvisation.Customer_Product_Verification
  193. as(select order_information.Customer_ID,Product_ID
  194.     from shopee_improvisation.order_information
  195.     join shopee_improvisation.customer
  196.     where order_information.Customer_ID=customer.Customer_ID);
  197. alter table shopee_improvisation.customer_product_verification
  198.     add verfication_id nvarchar(255) not null
  199.     after Product_ID;
  200.  
  201. #Delivery company have to find the customer location
  202. select order_information.Customer_ID,customer.City,Customer_Name,customer.Customer_Name,Postal_Code,customer.Telephone_No,Address,Email
  203. from shopee_improvisation.delivery_and_supply_system
  204. inner join shopee_improvisation.customer_product_verification
  205.     on delivery_and_supply_system.verfication_id=customer_product_verification.verfication_id
  206. inner join shopee_improvisation.customer
  207.     on customer_product_verification.Customer_ID=customer.Customer_ID
  208. inner join shopee_improvisation.order_information
  209.     on customer.Customer_ID=order_information.Customer_ID
  210. inner join shopee_improvisation.store
  211.     on order_information.Product_ID=store.Product_ID
  212.     where order_information.Product_ID='P111';
  213.    
  214. #then customer will notify about the product and delivery status
  215. #customer will use his shipment id to locate his product
  216.  
  217. select*
  218. from shopee_improvisation.delivery_and_supply_system
  219. where delivery_and_supply_system.Shipment_ID='SP101';
  220.  
  221. #after receiving the product,if the customer find the product defective, will contact with a staff
  222. #add his product on defective product database
  223. #staff will notify
  224. #staff will check
  225.  
  226. select*
  227. from shopee_improvisation.defective_product
  228. where Product_ID='P111';
  229.  
  230. #Then either he will again check the store and deliver the product or will refund into customer's account
  231. #suppose the staff has to refund
  232.  
  233. select*
  234. from shopee_improvisation.payment
  235. where payment.Product_ID="P111";
  236.  
  237. #now the staff wants to see the amount of order from his store
  238.  
  239. select count(Order_Quantity)
  240. from shopee_improvisation.product_information;
  241.  
  242. #display the number of the order based on customers username
  243.  
  244. select customer.Customer_ID,Customer_Name,Order_Quantity
  245. from shopee_improvisation.product_information
  246. inner join shopee_improvisation.customer_product_verification
  247.     on product_information.Product_ID=customer_product_verification.Product_ID
  248. inner join shopee_improvisation.customer
  249.     on customer_product_verification.Customer_ID=customer.Customer_ID;
  250.  
  251.  
  252. # suppose the tax is included. We want to know the tax price. For accessories there is 10% tax,
  253. #for electronics it is 15%,for grocceries it is 5% and for skin product it is 7%
  254.  
  255. alter table shopee_improvisation.product_information
  256.     add Tax_Price int
  257.     after Product_Price;
  258. alter table shopee_improvisation.product_information
  259.     drop column Tax_Price;
  260.    
  261.  
  262.  
  263. select Product_Price,Product_ID,Product_Name,
  264. (case
  265.     when Product_Category="Accessories" then Product_Price*.10
  266.     when Product_Category='Electronics' then Product_Price*.15
  267.     when Product_Category='Grocceries' then Product_Price*.05
  268.     else Product_Price*0.07
  269. end) as Tax_Price
  270. from shopee_improvisation.product_information;
  271.  
  272. #see the category
  273.  
  274. select Product_Name,Product_Category
  275. from shopee_improvisation.product_category
  276. join shopee_improvisation.product_information
  277. where product_category.Product_ID=product_information.Product_ID;
  278.  
  279. select Delivery_Status,Delivery_Company_Name,Shipment_Type
  280. from shopee_improvisation.delivery_and_supply_system
  281. join shopee_improvisation.payment
  282. where delivery_and_supply_system.Shipment_ID=payment.Shipment_ID and Product_ID="P111";
  283.  
  284.  
  285.  
  286.  
  287. #practice for exam
  288.  
  289. select Customer_Name,Customer_Address,Product_Name,Product_Price,Payment_Type
  290. from customer
  291. inner join payment
  292.     on customer.Customer_ID=payment.Customer_ID
  293. inner join product
  294.     on payment.Product_ID=product.Product_ID
  295. where Product_Category="Stationary"and "Furniture";
  296.  
  297.  
  298.  select sum(Payment_totPrice),Payment_date
  299.  from Paymnet
  300.  where extract(Month from Payment_Date)=01;
  301.  
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement