-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGrocery store_Part1.sql
185 lines (142 loc) · 7.81 KB
/
Grocery store_Part1.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
-- create database
create database if not exists grocerystore;
-- use database to start performing any operations on the database
use grocerystore;
-- Creating tables Location, designation, employees
create table location( Location_Id varchar(10) not null, Zipcode int not null, City varchar(20) not null,
Street varchar(30) not null, primary key (Location_Id));
insert into location values("JTP01", 413245, "New York", "Jericho"), ("NHP02", 413287, "New York", "Hyde Park"),
("WGP03", 417654, "Pennsylvania", "Willow Grove");
create table designation(Dsgn_Id varchar(20) not null, Designation varchar(20) not null,
Salary int, primary key(Dsgn_Id));
insert into designation values("AM01", "Asst.Manager", 80000), ("LVL1", "Level 1", 70000), ("M01", "Manager", 100000);
create table employees(Location_Id varchar(20) not null,
Email varchar(40) not null, Mobile varchar(15) not null,
First_Name varchar(40) not null, Last_Name varchar(30) not null,E_Id int not null,
Dsgn_Id varchar(20) not null, primary key(E_Id), foreign key(Location_Id) references location(Location_Id),
foreign key(Dsgn_Id) references designation(Dsgn_Id) );
-- Inserting values into the tables location, employees, designation
insert into employees values ("JTP01", "[email protected]", "9875678987", "Katherine", "Johnson", 313100, "M01"),
("NHP02", "[email protected]", "9865467435", "Mark", "Jason", 315178, "M01"),
("WGP03", "[email protected]", "9456789876", "Rick", "Stark", 314768, "M01");
insert into location values("MCA04", 414876, "New York", "Marcus Avenue"),
("HPG05", 415665, "New York", "Hauppage"),
("STF06", 416876, "New York", "Statford"),
("GCP07", 416766, "New York", "Garden City Park"),
("MRA08", 416321, "New York", "Merillon Avenue"),
("ATA09", 418765, "New York", "Atlantic Avenue"),
("MNL10", 415397, "New York", "Mineola"),
("WLP11", 418657, "New York", "Williston Park"),
("RDS12", 537878, "New Haven", "Rodney Street"),
("STA13", 538766, "New Haven", "Stevens Avenue"),
("LSS14", 527687, "New Haven", "Lester Street"),
("RCP15", 526856, "New Haven", "Richards PI"),
("MLR16", 546765, "New Haven", "Meloy Rd"),
("HRS17", 613245, "Pennsylvania", "Horsham"),
("HTB18", 614387, "Pennsylvania", "Hatboro"),
("JRT19", 616655, "Pennsylvania", "Jarrettown"),
("STN20", 618984, "Pennsylvania", "Stenton");
insert into designation values("SM01", "Senior Manager", 140000),
("CSH01", "cashier", 50000),
("AST01", "Aisle Staff", 50000),
("LVL2", "Level 2", 70000),
("LVL3", "Level 3", 70000),
("LVL4", "Level 4", 70000),
("LVL5", "Level 5", 70000),
("DPM1", "Deputy Manager", 80000),
("STF1", "Staff", 50000),
("RPN1", "Receptionist", 50000),
("DTR1", "Director", 200000),
("LVL6", "Level 6", 70000),
("LVL7", "Level 7", 70000),
("LVL8", "Level 8", 70000),
("LVL9", "Level 9", 70000),
("STF2", "Staff 2", 50000),
("STF3", "Staff 3", 50000);
insert into employees values("ATA09", "[email protected]", "9432187654", "Rachel", "Zeen", 316532, "M01"),
("GCP07", "[email protected]","9422177654", "Mike", "Gurrero", 312432, "M01"),
("HPG05", "[email protected]","9413217698", "Harvey", "Spectar",307654, "M01"),
("HRS17", "[email protected]","9433127498", "Luke", "Ross",310765, "M01"),
("HTB18", "[email protected]", "9453217654", "Jessica", "Aniston",305654, "M01"),
("JRT19", "[email protected]","9428769932", "Monica", "Grecher",343234, "M01"),
("MLR16", "[email protected]", "9446579127", "Jennifer", "Cox",324876, "M01"),
("LSS14", "[email protected]", "9446623438", "Matt", "Kudrow",365432, "M01"),
("MCA04", "[email protected]", "9454537657", "James", "Tyler",329873, "M01"),
("MNL10", "[email protected]", "9658794309", "Bruce", "Wills",319808, "M01"),
("MRA08", "[email protected]", "6543287623", "Lisa", "Wheller",312435, "M01"),
("RCP15", "[email protected]", "6754391239", "Tom", "Sibbett",376543, "M01"),
("RDS12", "[email protected]","8439796397", "Jack", "Geller", 376541, "M01"),
("STA13", "[email protected]","8712309784", "Jon", "Taylor", 412345, "M01"),
("STF06", "[email protected]","7650982115", "Eddie", "Cahill", 333421, "M01"),
("STN20", "[email protected]", "5198760954","Elle", "Hecht", 327654, "M01"),
("WLP11", "[email protected]","5981297287", "Kathy", "Paget", 374867, "M01");
-- Creating tables customers, manager, dbusers, commodities, supplier , categories
create table customers(Cust_ID int not null primary key, First_name varchar(20) not null,
Last_name varchar(20) not null, Phone varchar(20) not null, Email varchar(30) not null,
Location_Id varchar(20) not null, foreign key(Location_Id) references location(Location_Id));
insert into customers values(1,'Shane','Lane',5679345679,"[email protected]","JTP01"),
(2,'Jack','Jill',9874567845,"[email protected]","JTP01"),
(3,'Liam','Hen',5670983456,"[email protected]","NHP02"),
(4,'Chris','Morris',5647895876,"[email protected]","NHP02"),
(5,'Billy','Ben',3459876908,"[email protected]","NHP02");
describe customers;
create table Manager(E_Id int not null primary key,
Mobile varchar(15) not null, Email varchar(30) not null,
foreign key(E_Id) references employees(E_Id));
insert into Manager(E_Id, Mobile, Email) select E_Id, Mobile, Email from employees where Dsgn_Id = "M01";
create table DBUsers(E_Id int not null primary key, Username varchar(20),
Password varchar(50), foreign key(E_Id) references Manager(E_Id));
-- alter table DBUsers modify Password varchar(50);
insert into DBUsers(E_Id, Username, Password) select E_Id, substring(Email, 1, 6), substring(E_Id, 1, 6) from Manager;
create table Commodities( Product_No int not null primary key,
Product_Name varchar(30) not null, Prod_Quantity int not null, Prod_Price int not null);
create table Suppliers(Supplier_No int NOT NULL Primary Key AUTO_INCREMENT,
Supplier_Name varchar(20) NOT NULL,
Mobile varchar(20) NOT NULL,
Location_Id varchar(20) NOT NULL,
Email varchar(30) not null,
Product_No int NOT NULL, FOREIGN KEY(Product_No) REFERENCES Commodities(Product_No),
foreign key(Location_Id) references location(Location_Id),
Prd_Actual_Price decimal(10,2) NOT NULL);
-- To check the structure of the table
describe Commodities;
describe Suppliers;
INSERT INTO Commodities(Product_No,Product_Name,Prod_Quantity,Prod_Price)
VALUES(1,'Noodles',10,12),
(2,'Pasta',16,14),
(3,'Meat',28,10), (4,'Bread',17,2.5),
(5,'Avocado',23,1.2), (6,'Eggs',50, 7);
Insert into Suppliers values(1,'Panera',7654532879,"JTP01","[email protected]",4,2),
(2 ,'Nestle',9599457967,"JTP01" ,"[email protected]" ,1, 11 ),
(3 ,'Jane Meat Suppliers',2591093897,"NHP02", "[email protected]" ,3 ,9.5),
(4 ,'High Land',3287390523,"JTP01", "[email protected]" ,6,6 ),
( 5,'Fresh Foods',7689278041 ,"NHP02", "[email protected]", 5,0.8 ),
( 6,'Linc',7687454092 ,"JTP01", "[email protected]" ,2 ,12 );
create table Categories(Category_Id varchar(20) not null,
Category_Name varchar(30), Product_No int not null, foreign key(Product_No) references Commodities(Product_No));
-- alter table Categories drop primary key;
describe Categories;
insert into Categories values("C1", "Pasta and more", 1),
("C1", "Pasta and more", 2),
("C2", "Meat products", 3),
("C3", "Bread", 4),
("C4", "Fruits and Salads", 5),
("C2", "Meat products", 6);
-- Display all data in the tables employees, designation and location and also get the count of the records in each of the tables
select * from employees;
select count(*) from employees;
select * from designation;
select count(*) from designation;
select * from location;
select count(*) from location;
select * from Commodities;
select * from Suppliers;
select * from Categories;
select * from DBUsers;
select * from Manager;
select * from customers;
-- Creating a view to display managers and their location
create view Branch_Manager as select ee.First_Name, ee.Last_Name, loc.Street, loc.City, loc.Zipcode from employees
as ee inner join location as loc on ee.Dsgn_Id = "M01" and ee.Location_ID = loc.Location_Id;
-- Display the view
select * from Branch_Manager;