-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory Project SQL Query.sql
More file actions
285 lines (213 loc) · 8.42 KB
/
Inventory Project SQL Query.sql
File metadata and controls
285 lines (213 loc) · 8.42 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
-- Inventory Project
create database Inventory_Project;
use inventory_project;
-- Create supplier table in inventory project database
create table Supplier (SID VARCHAR(10) primary key ,SNAME VARCHAR(50) not null ,SADD VARCHAR(100) NOT NULL ,
SCITY VARCHAR(30) DEFAULT( 'DELHI') , SPHONE VARCHAR(15) UNIQUE , SEMAIL VARCHAR (50) );
-- Describe the supplier table
exec sp_help 'supplier';
-- Insert data into supplier table
insert into Supplier (SID ,SNAME, SADD, SPHONE , SEMAIL)
values('S0001', 'GlobalTech Supplies', 'A-12, Connaught Place', '9876543210', 'globaltech@supplies.com'),
('S0002', 'NextGen Distributors', 'B-45, Nehru Place', '9812345678', 'nextgen@distributors.com'),
('S0003', 'Prime Wholesale', 'C-67, Karol Bagh', '9898989898', 'prime@wholesale.com'),
('S0004', 'Metro Logistics', 'D-89, Lajpat Nagar', '9123456780', 'metro@logistics.com'),
('S0005', 'Skyline Traders', 'E-10, Hauz Khas', '9988776655', 'skyline@traders.com');
-- To view the supplier table
select * from Supplier;
-- Create a product table
create table Product( PID varchar(10) primary key ,PDESC varchar (100) not null,PRICE decimal(10,2) check (price >0),
CATEGORY varchar(50) check (category in ( 'IT','HA','HC')),SID varchar(10) foreign key (sid) references supplier(sid));
-- Describe the product table
exec sp_help 'product';
-- insert data into product table
insert into [Product] (PID, PDESC, PRICE, CATEGORY, SID) values
('P0001', 'Mouse Product', 250.00, 'IT', 'S0001'),
('P0002', 'Keyboard Product', 450.00, 'IT', 'S0002'),
('P0003', 'Monitor Product', 950.00, 'IT', 'S0002'),
('P0004', 'Fridge Product', 800.00, 'HA', 'S0004'),
('P0005', 'Iron Product', 300.00, 'HA', 'S0005'),
('P0006', 'Fan Product', 400.00, 'HA', 'S0005'),
('P0007', 'Shampoo Product', 150.00, 'HC', 'S0001'),
('P0008', 'Soap Product', 80.00, 'HC', 'S0001'),
('P0009', 'Toothpaste Product', 90.00, 'HC', 'S0005'),
('P0010', 'Trimmer Product', 550.00, 'HC', 'S0003');
-- To view the product table
select * from Product;
--create a cust table
create table cust (CID varchar(10) primary key ,CNAME varchar (40) not null ,ADDRESS VARCHAR(100) NOT NULL ,
CITY VARCHAR(50) NOT NULL , PHONE VARCHAR(10) NOT NULL ,EMAIL VARCHAR(50) NOT NULL ,DOB DATE CHECK(DOB < '2000-01-01'))
-- Describe the cust table
exec sp_help 'Cust';
-- insert data into cust table
INSERT INTO CUST (CID, CNAME, ADDRESS, CITY, PHONE, EMAIL, DOB) VALUES
('C0001', 'Ravi Sharma', '45 A, Nehru Nagar', 'Delhi', '9876543210', 'rs987@rcg.com', '1990-04-12'),
('C0002', 'Priya Mehta', '21 B, Model Town', 'Delhi', '9823456781', 'pm781@rcg.com', '1985-11-03'),
('C0003', 'Amit Verma', '78, Rajouri Garden', 'Delhi', '9812345678', 'av678@rcg.com', '1992-07-25'),
('C0004', 'Nisha Kapoor', '12, Green Park', 'Delhi', '9898765432', 'nk432@rcg.com', '1993-09-14'),
('C0005', 'Sandeep Joshi', '9, Lajpat Nagar', 'Delhi', '9845632190', 'sj190@rcg.com', '1988-01-30');
-- To view the cust table
select * from cust;
--create a orders table
create table orders( OID varchar(20) not null ,ODATE date ,PID varchar(10) foreign key(pid) references product(PID),
CID varchar(10) foreign key (cid) references cust(cid),
OQTY int check (oqty >= 1));
-- Describe the orders table
exec sp_help 'orders';
--insert data into orders table
INSERT INTO ORDERS (OID, ODATE, PID, CID, OQTY) VALUES
('O0001', '2024-04-10', 'P0001', 'C0001', 3),
('O0002', '2024-04-15', 'P0002', 'C0002', 1),
('O0003', '2024-04-18', 'P0003', 'C0003', 2),
('O0004', '2024-04-21', 'P0004', 'C0004', 4),
('O0005', '2024-04-25', 'P0005', 'C0005', 5);
-- To view the orders table
select * from orders;
--create a stock table
create table stock (PID varchar(10) foreign key references product(pid) ,SQTY int check (sqty >0),
ROL int check (rol >= 0),MOQ int check (moq >=5));
-- Describe the stock table
exec sp_help 'stock';
--insert data into stock table
INSERT INTO STOCK (PID, SQTY, ROL, MOQ) VALUES
('P0001', 120, 20, 10),
('P0002', 80, 15, 5),
('P0003', 50, 10, 7),
('P0004', 200, 25, 15),
('P0005', 60, 12, 6),
('P0006', 140, 30, 10),
('P0007', 75, 10, 8),
('P0008', 90, 18, 12),
('P0009', 110, 22, 9),
('P0010', 130, 20, 10);
-- To view the stock table
select * from stock;
-- view all the tables of inventory project database
select * from INFORMATION_SCHEMA.tables
-- auto generate alphanumeric ids for supp,cust,prod,ord tables
-- create squences for all tables
create sequence seq_supplier as int
start with 6
increment by 1 ;
create sequence seq_product as int
start with 11
increment by 1 ;
create sequence seq_cust as int
start with 6
increment by 1;
create sequence seq_orders as int
start with 6
increment by 1;
-- create procedure for auto generate alphanumeric ids and also insert data into supplier table through procedure
create procedure ins_supplier
@sname varchar(50) ,
@sadd varchar(100) ,
@sphone varchar(15) ,
@semail varchar (50)
as
begin
declare @numb int = next value for seq_supplier
declare @id varchar(5)
--generate sid
set @id = CONCAT( 'S' ,left('000' + CAST(@numb as varchar ) ,4) )
--insert data into supplier table
insert into Supplier(SID,SNAME , sadd, SPHONE,SEMAIL)
values(@id ,@sname,@sadd,@sphone ,@semail )
--print message after inserted a row by user
print 'inserted sid is ' + @id
end ;
--excecute procedure with inserting data into supplier table
execute dbo.ins_supplier @sname = 'NeVep EXP',
@sadd = 'Secter-11 Dwarka',
@sphone ='8545145896',
@semail ='nevep@exp.com';
-- view the inserted data of supplier table
select * from Supplier;
-- create procedure for auto generate alphanumeric ids and also insert data into product through procedure
create procedure ins_product
@Pdesc varchar(100) ,
@price decimal(10,2) ,
@category varchar(50) ,
@sid varchar(10)
as
begin
declare @numb int = next value for seq_product
declare @id varchar(5)
set @id =CONCAT('P' , right('000' + cast(@numb as varchar),4))
insert into Product(pid,Pdesc,price ,category ,sid)
values(@id ,@Pdesc , @price,@category,@sid)
print 'inserted pid is '+ @id
end;
--excecute procedure with inserting data into product table
exec dbo.ins_product
@pdesc ='Bulb Product',
@price ='100' ,
@category = 'HA',
@sid ='S0006';
--view the inserted data of product table
select * from product;
-- create procedure for auto generate alphanumeric ids and also insert data into cust through procedure
create procedure ins_cust
@cname varchar(40) ,
@address varchar(100) ,
@city varchar(50) ,
@phone varchar(10) ,
@email varchar(50) ,
@dob date
as
begin
declare @numb int = next value for seq_cust
declare @id varchar(5)
set @id= CONCAT('C' , RIGHT('000' + cast(@numb as varchar ) ,4))
insert into cust (CID ,CNAME,ADDRESS,CITY ,PHONE ,EMAIL,DOB )
values (@id ,@cname ,@address,@city ,@phone ,@email,@dob )
print 'inserted cid is ' +@id
end;
--excecute procedure with inserting data into cust table
exec dbo.ins_cust
@cname ='Rahul Kumar',
@address = '34 C,shaheed Nagar',
@city = 'Delhi' ,
@phone= '7845865263',
@email = 'rk856@rcg.com',
@dob = '1994-04-22';
--view the inserted data of cust table
select * from cust;
-- create procedure for auto generate alphanumeric ids and also insert data into orders through procedure
create procedure ins_orders
@odate date ,
@pid varchar(5),
@cid varchar(5) ,
@oqty int
as
begin
declare @numb int= next value for seq_orders
declare @id varchar(5)
set @id = CONCAT('O' , right('000' + cast(@numb as varchar ) ,4))
insert into orders (OID ,ODATE,PID,CID,OQTY)
values(@id ,@odate,@pid,@cid ,@oqty)
print 'inserted oid is '+@id
end;
--excecute procedure with inserting data into orders table
exec dbo.ins_orders
@odate = '2024-04-29',
@pid = 'P0011',
@cid = 'C0006',
@oqty = 3;
--view the inserted data of orders table
select * from orders;
-- insert new data into stock table
insert into stock (PID, SQTY,ROL,MOQ)
values('P0011' ,50,15,10),('P0012', 100,20,25);
-- create a bill view
create view bill
as
select o.OID as Order_id, o.ODATE as Order_date ,c.CNAME as Customer_name,c.ADDRESS as Address, c.PHONE as Phone,
p.PDESC as Product_desc , p.PRICE as Price ,o.OQTY as Quantity ,p.price*o.oqty as Total_amount from orders o
inner join Product p
on p.PID = o.pid
inner join cust c
on c.CID = o.CID;
-- check bill for a specific customer
select * from bill
where Customer_name ='Ravi sharma';