-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSDMysql.sql
1317 lines (967 loc) · 34.5 KB
/
FSDMysql.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Day 1 22/7/2023
show databases;
create database MysqlFSD;
use mysqlfsd;
show tables;
create table Employee(id int,name char(20),
address varchar(255),salary float,
phone bigint);
describe employee;
select * from employee;
insert into employee values(100,"Aman Tiwari","noida 62",50000.45,9891062743);
insert into employee values
(100,"Aman Tiwari","noida 62",50000.45,9891062743),
(101,"Naman","noida 63",500.45,98916565743),
(34,"RAman Tiwari","noida 63",5000.45,98916767743),
(60,"Shri RAman Tiwari","noida 62",80000.45,8766868687),
(180,"BAman ","noida 65",500.5645,687877);
truncate employee;
drop table employee;
select name from employee;
select name,address,salary from employee;
select * from employee where name="aman tiwari";
insert into employee values
(100,"Aman Tiwari","noida 62",50000.45,9891062743);
select distinct name from employee;
insert into employee(name) values("Ankit Pandey");
select * from employee;
insert into employee(name,id) values("abcd",108);
delete from employee where id=108;
update employee set name="Aditya" where name="Ankit Pandey";
-- Hello this is single line comment
/* Multiple
Line
comment
*/
# hello
-- 23/7/2023
use mysqlfsd;
drop table employee;
create table Employee(id int,name char(20),
address varchar(255),salary float,
phone bigint,dob date,joiningdate datetime,
Bankbalance double,comment text,
intime timestamp,ismarried boolean);
insert into employee values(101,"Aman","Imarticus 62",
60000.56,9891062743,"1999/04/22","1999-06-24",66687.32873487,
"he is a best teacher","1999-06-24 13:04:5",false);
select * from employee;
insert into employee values(101,"Ankit","Imarticus 62",
60000.56,9891062743,"1999/04/22",curdate(),66687.32873487,
"he is a best teacher",current_timestamp(),true);
-- 24/07/2023
use mysqlfsd;
create table Students(id int,name varchar(255),
phone bigint,address varchar(255),fees float);
describe students;
insert into students values(100,"Aman Tiwari",9891062743,"noida 62",5000.56);
insert into students(name,phone) values("Ankit Tiwari",758778);
select * from students;
update students set name="Krishna" where id=100; /* update anamolis*/
-- keys (constraints)
drop table students;
-- unique
-- not null
-- default
-- check
-- primary key
-- foreign key
create table Students(id int not null unique,name varchar(255),
phone bigint not null,address varchar(255),fees float);
insert into students values(100,"Aman Tiwari",9891062743,"noida 62",5000.56);
insert into students values(100,"Raman Tiwari",656776,"noida 62",5000.56);
insert into students values(101,"Raman Tiwari",656776,"noida 62",5000.56);
select * from students;
insert into students values
(102,"Aman 1",6776434,"hello",7676),
(103,"Aman 2",6776434,"hello",7676),
(104,"Aman 3",6776434,"hello",7676),
(105,"Aman 4",6776434,"hello",7676),
(106,"Aman 5",6776434,"hello",7676),
(107,"Aman 6",6776434,"hello",7676);
insert into students(id,name,address,fees) values
(108,"Aman 1","hello",7676);
-- check default
drop table students;
create table Students(id int not null unique,name varchar(255),
phone bigint not null,address varchar(255),
fees float,city varchar(255) not null default("delhi"));
insert into students values(100,"Aman Tiwari",9891062743,"noida 62",5000.56,"uttar Pradesh");
insert into students(id,name,phone,address,fees)
values(101,"Krishna Tiwari",5667,"noida 62",5000.56);
-- check
drop table students;
create table Students(id int not null unique,name varchar(255),
age int check(age>=18) ,phone bigint not null,address varchar(255),
fees float,city varchar(255) not null default("delhi"));
insert into students values(100,"Aman Tiwari",24,
9891062743,"noida 62",5000.56,"uttar Pradesh");
insert into students(id,name,phone,address,fees,city) values(101,"Ravi Tiwari",
9891062743,"noida 62",5000.56,"uttar Pradesh");
insert into students values(102,"Ankit Tiwari",12,
9891062743,"noida 62",5000.56,"uttar Pradesh");
-- a table must have a single primary key
drop table students;
create table Students(stuid int primary key,name varchar(255),
age int not null check(age>=18),phone bigint not null, email varchar(255) unique ,
address varchar(255),fees float,city varchar(255) default("delhi"));
describe students;
insert into students values(100,"Aman Tiwari",24,
9891062743,"amantiwari8861@gmail.com","noida 62",5000.56,
"Noida");
insert into students(name,age,phone,email,address,fees,city) values("Aman Tiwari",24,
9891062743,"amantiwari8861@gmail.com","noida 62",5000.56,
"Noida");
select * from students;
-- auto_increment
drop table students;
create table Students(stuid int primary key auto_increment,name varchar(255),
age int not null check(age>=18),phone bigint not null, email varchar(255) unique ,
address varchar(255),fees float,city varchar(255) default("delhi"));
insert into students(name,age,phone,email,address,fees,city) values("Aman Tiwari",24,
9891062743,"amantiwari8861@gmail.com3","noida 62",5000.56,
"Noida");
-- h.w BatchMates
-- 26/7/2023
-- primary key
/* What is Database Normalization?
Normalization is a database design technique that reduces data redundancy and eliminates
undesirable characteristics like Insertion, Update and Deletion Anomalies.
Normalization rules divides larger tables into smaller tables and links them
using relationships. The purpose of Normalisation in SQL is to eliminate
redundant (repetitive) data and ensure data is stored logically.*/
create table Students(name varchar(255),
fathername varchar(255),address varchar(255),
primary key(name,fathername) );
insert into students values("Krishna","Xyz","noida");
insert into students values("Krishna","Abcd","noida");
insert into students values("Ravi","Xyz","noida");
insert into students values("Ankit","Abcd","noida");
insert into students values("Krishna","Xyz","noida 62bhvfbh");
insert into students values("Ravi","Xyz","noida");
use mysqlfsd;
create table categories(category_id int primary key,category_name varchar(255),category_desc varchar(255));
create table products(
cat_id int,product_id int primary key,
product_name varchar(255),product_desc varchar(255),
brand varchar(255),
foreign key(cat_id) references categories(category_id));
select * from categories;
select * from products;
insert into categories values(201,'electronic devices','best electronic items');
insert into products values(201,501,'dell vestro','small lappy','dell');
select * from categories;
select * from products;
insert into products values(586,502,'dell inspron','good lap','dell');
delete from categories where category_id = 201;
update categories set category_id=202 where category_id=201;
drop table categories; -- error parent table can't be deleted first
drop table products;
drop table categories;
-- 3 tables
create table sellers(vendor_id int primary key,vendor_name varchar(255),address varchar(255));
create table categories(category_id int primary key,category_name varchar(255),category_desc varchar(255));
create table products(
cat_id int,product_id int primary key,
product_name varchar(255),product_desc varchar(255),
brand varchar(255),
seller_id int,
foreign key(cat_id) references categories(category_id),
foreign key(seller_id) references sellers(vendor_id)
);
insert into categories values(201,'electronic devices','best electronic items');
insert into sellers values(301,'flipkart','online');
insert into sellers values(302,'amazon','online');
insert into products values(201,501,'dell vestro','small lappy','dell',301);
select * from categories;
select * from products;
select * from sellers;
-- h.w create a table students and teaches then apply foreign key in students
-- students taught by teacher
-- 28/7/2023
use mysqlfsd;
show tables;
drop table products;
drop table categories;
drop table sellers;
create table sellers(vendor_id int ,vendor_name varchar(255),address varchar(255),primary key(vendor_id));
create table categories(category_id int,category_name varchar(255),category_desc varchar(255),primary key(category_id));
create table products(
cat_id int,product_id int,
product_name varchar(255),product_desc varchar(255),
brand varchar(255),
seller_id int,
primary key(product_id),
constraint myrule foreign key(cat_id) references categories(category_id) on update cascade on delete cascade,
constraint myrule2 foreign key(seller_id) references sellers(vendor_id)
);
desc products;
insert into products(cat_id,product_id) values(104,345);
(no updation and deletion is permitted if child having rows linked with foreign key in parent)
on update restrict on delete restrict -- by default
on update cascade on delete cascade
on update set null on delete set null
insert into categories values(201,'electronic devices','best electronic items');
insert into sellers values(301,'flipkart','online');
insert into sellers values(302,'amazon','online');
insert into products values(201,502,'dell vestro','small lappy','dell',301);
select * from categories;
update categories set category_id=903;
delete from categories where category_id=903;
select * from products;
drop table products;
create table products(
cat_id int,product_id int,
product_name varchar(255),product_desc varchar(255),
brand varchar(255),
seller_id int,
primary key(product_id),
constraint myrule foreign key(cat_id) references categories(category_id) on update set null on delete set null,
constraint myrule2 foreign key(seller_id) references sellers(vendor_id)
);
insert into products values(201,501,'dell vestro','small lappy','dell',301);
insert into categories values(201,'electronic devices','best electronic items');
update categories set category_id=701 where category_id=201;
select * from products;
delete from categories where category_id=201;
drop table products;
create table products(
cat_id int,product_id int,
product_name varchar(255),product_desc varchar(255),
brand varchar(255),
seller_id int,
primary key(product_id),
constraint myrule foreign key(cat_id) references categories(category_id)
on update cascade on delete set null,
constraint myrule2 foreign key(seller_id) references sellers(vendor_id)
);
select * from categories;
select * from products;
insert into categories values(201,'electronic devices','best electronic items');
update categories set category_id=701 where category_id=201;
delete from categories where category_id=701;
insert into products values(201,501,'dell vestro','small lappy','dell',301);
create database mysqlfsd;
use mysqlfsd;
create table students(roll_no int,name varchar(255),mobile varchar(255),fees double);
insert into students values
(101,"Aman",9891062743,500.56),
(102,"RAman",9891062743,1500.56),
(103,"BAman",545456,500.56),
(104,"Aman Tiwari",9891062743,58900.56),
(105,"Ankit",6565654,50000.56),
(106,"Ravi",9891062743,50.56),
(107,"Naman",68878,50099.56);
select * from students;
select * from students where name="Ravi";
select * from students where fees<5000;
select * from students where fees>5000;
select * from students where name like '%n';
select * from students where name like '%i';
select * from students where name like 'R%';
select * from students where name like '_Am_n';
select * from students where name like '%r%';
select * from students where name like '%a%' and fees<5000;
select * from students where name like 'Aman' or fees>5000;
select * from students where not fees>5000;
select * from students where name <> 'aman';
select * from students where name <> 'aman' order by name;
insert into students(name,fees) values('ravi',4000);
insert into students(name,fees) values('ravi',400);
insert into students(name,fees) values('ravi',40000);
select * from students order by name,fees;
select * from students order by name,fees desc;
-- 1/8/2023
drop database mysqlfsd;
create database mysqlfsd;
use classicmodels;
show tables;
select customerName,city,country,creditlimit from customers where country="usa";
select customerName,city,country,creditlimit from customers
where creditlimit>90000 and country="singapore" ;
select customerName,city,country,creditlimit from customers
where customername like "%co." and country="germany" and creditlimit>30000;
select customerName,city,country,creditlimit from customers
where customername like "%ltd%";
select customerName,city,country,creditlimit from customers
where customername like "Dr%Ltd.";
select customerName,city,country,creditlimit from customers
where country="australia";
select customerName,city,country,creditlimit from customers
where country="australia" limit 3;
select customerName,city,country,creditlimit from customers
where country="australia" limit 3 offset 2;
select customerName,city,country,creditlimit from customers
where country="australia" order by creditlimit asc limit 3 offset 2;
select customerName,city,country,creditlimit from customers
where country="australia" order by creditlimit desc limit 3;
select customerName,city,country,creditlimit from customers
where country="australia" or creditlimit>100000 order by country,creditLimit;
select customerName,city,country,creditlimit from customers
where country="australia" and creditlimit>90000 order by creditlimit,country;
select customerName,city,country,creditlimit from customers
where not country="australia" and creditlimit>90000 order by creditlimit,country;
select customerName,city,country,creditlimit from customers
where country in ("australia","USA") order by creditlimit,country;
select * from customers where country <> 'usa'; -- not equal to
-- insert into select
-- alter
-- 02/08/2023
use classicmodels;
use mysqlfsd;
show tables;
create table Customers(custId int,name varchar(255),companyname varchar(100));
select * from classicmodels.customers;
select customernumber,contactfirstname,customername from classicmodels.customers;
insert into customers(custId,name) select customernumber,contactfirstname
from classicmodels.customers;
truncate customers;
insert into customers select customernumber,contactfirstname,customername
from classicmodels.customers;
select * from customers;
-- alter
alter table customers add column address varchar(200);
alter table customers add column address varchar(200) after custId;
alter table customers drop column address;
alter table customers drop column name;
alter table customers modify column custId varchar(20);
desc customers;
alter table customers modify column address int;
alter table customers modify column custId int not null unique;
alter table customers modify column custId int primary key auto_increment;
alter table customers drop primary key;
alter table customers modify column custid int;
desc customers;
-- constraint myrule primary key(abcd)
commit;
SHOW INDEX FROM customers;
alter table customers drop index `PRIMARY`;
SHOW CREATE TABLE products;
alter table products drop foreign key `products_ibfk_2`;
desc products;
insert into products(product_id,seller_id) values(55566,56);
insert into products(product_id,seller_id) values(45,454544256);
select * from products;
alter table customers rename to employees;
desc employees;
alter table employees rename column custid to customerId;
-- 07/08/2023
use classicmodels;
select * from customers;
-- select * from customers where country='singapore';
select country,count(*)
as 'total customers',sum(creditLimit) 'Total',avg(creditLimit),
min(creditLimit),max(creditLimit) from customers group by country order by country;
select country,city,count(*)
as 'total customers',
sum(creditLimit) 'Total',avg(creditLimit),
min(creditLimit),max(creditLimit)
from customers group by country,city
order by country,city;
select country,city,count(*)
as 'total customers',
sum(creditLimit) 'Total',avg(creditLimit),
min(creditLimit),max(creditLimit)
from customers group by country,city
where sum(creditlimit)>50000
order by country,city;
select country,city,count(*)
as 'total customers',
sum(creditLimit) 'Total',avg(creditLimit),
min(creditLimit),max(creditLimit)
from customers group by country,city
having sum(creditLimit)>65000
order by country,city;
-- 9/8/2023
-- sub query : query within query
use classicmodels;
select * from employees;
select firstname
from employees
where jobtitle='vp sales';
select * from employees where firstname="Mary";
-- or
select *
from employees
where firstname=(
select firstname
from employees
where jobtitle='vp sales');
-- subquery use with different table
select *
from employees;
select *
from offices;
select * from offices
where officeCode=(
select officeCode
from employees
where firstName="George"
);
-- operators
-- select * from offices
-- where officeCode=(
-- select officeCode
-- from employees
-- where jobTitle="Sales Rep"
-- );
select * from offices
where officeCode =any(
select officeCode
from employees
where jobTitle="Sales Rep"
);
select * from offices
where officeCode =any(
select officeCode
from employees
where jobTitle="Sales Rep" and reportsTo=1143
);
/*
> ALL means greater than every value. In other words, it means greater than
the maximum value. For example, > ALL (1, 2, 3) means greater than 3.
> ANY means greater than at least one value, that is, greater than the minimum.
So > ANY (1, 2, 3) means greater than 1.
*/
select * from offices
where officeCode >All(
select officeCode
from employees
where jobTitle="Sales Rep" and reportsTo=1143
);
select * from offices
where officeCode <All(
select officeCode
from employees
where jobTitle="Sales Rep" and reportsTo=1621
);
select * from offices
where officeCode =All(
select officeCode
from employees
where jobTitle="Sales Rep" and reportsTo=1143
);
-- 10/8/2023 joins
use classicmodels;
create table categories(category_id int,
category_name varchar(255)
,category_desc varchar(255),
primary key(category_id));
create table product_details(
cat_id int,product_id int,
product_name varchar(255),
brand varchar(255),
product_desc varchar(255),
primary key(product_id),
constraint myrule foreign key(cat_id) references categories(category_id)
);
insert into categories values
(101,"phones","best phones"),
(102,"laptop","best laptop"),
(103,"fashion","best fashion"),
(104,"food","best food"),
(105,"guns","best guns");
insert into product_details values
(101,201,"Realme XT","Realme","best phone of realme"),
(101,202,"Redmi note 9","Redmi","best phone of redmi"),
(102,203,"HP Pavallion","HP","best laptop of HP"),
(102,204,"Dell inspiron","Dell","best laptop of dell"),
(103,205,"Denim jeans","denim","best jeans of denim"),
(null,206,"others","xyz","pata nahi"),
(null,207,"coffee","nescafe","best coffee");
select * from categories;
select * from product_details;
-- cross join (comma join)
select *
from categories,product_details
order by categories.category_id,
product_details.product_id;
select *
from product_details,categories
order by categories.category_id,
product_details.product_id;
-- inner join
select * from
categories
inner join product_details
on categories.category_id=product_details.cat_id;
select * from
categories as c
inner join product_details as p
on c.category_id=p.cat_id;
-- left join
select * from
categories as c
left join product_details as p
on c.category_id=p.cat_id;
-- only left
select * from
categories as c
left join product_details as p
on c.category_id=p.cat_id
where p.cat_id is null;
-- right join
select * from
categories as c
right join product_details as p
on c.category_id=p.cat_id;
-- only right
select * from
categories as c
right join product_details as p
on c.category_id=p.cat_id
where c.category_id is null;
-- full outer join
select * from
categories as c
left join product_details as p
on c.category_id=p.cat_id
union all
select * from
categories as c
right join product_details as p
on c.category_id=p.cat_id;
-- full outer join without common twice
select * from
categories as c
left join product_details as p
on c.category_id=p.cat_id
union
select * from
categories as c
right join product_details as p
on c.category_id=p.cat_id;
-- full outer join without common part
select * from
categories as c
right join product_details as p
on c.category_id=p.cat_id
where c.category_id is null
union
select * from
categories as c
left join product_details as p
on c.category_id=p.cat_id
where p.cat_id is null;
-- 17/8/2023
-- using clause
use classicmodels;
select * from categories; -- in this category_id is primary key
select * from product_details; -- in this cat_id is foreign key
-- if u want to use using clause the u have to set the column defination same(datatype and name).
alter table product_details rename column cat_id to category_id;
select * from
categories
inner join product_details
on categories.category_id=product_details.category_id;
-- here the category_id column is duplicate in result set
select * from
categories
inner join product_details
using (category_id);
-- DQL(Data Query Language) eg.select
-- DML(data manipulation language) eg. insert,update,delete
-- DDL(data defination language) eg. create,alter,drop etc
-- Test
-- DCL(data control language) eg. grant,revoke
-- TCL(Transaction control language) eg.commit,savepoint,rollback
-- trigger,stored procedures,prepared statements,views
use classicmodels;
create user 'krishna'@'localhost' identified by '123456';
grant select,insert,update,delete
on classicmodels.products
to 'krishna'@'localhost';
select * from mysql.user;
-- 22/8/2023
drop user 'krishna'@'localhost';
create user 'backenddev'@'localhost' identified by '2818';
grant select on *.* to 'backenddev'@'localhost';
grant all on *.* to 'backenddev'@'localhost';
revoke all PRIVILEGES,GRANT OPTION from 'backenddev'@'localhost';
grant grant option on *.* to 'backenddev'@'localhost';
-- backenddev.sql ---
create database abcd;
show databases;
use amazon;
show tables;
select * from product;
use amandb;
drop database amandb;
create user 'krishna'@'localhost' identified by '12345';
grant Select on *.* to 'krishna'@'localhost';
-- krishna.sql ----------
show databases;
use example;
show tables;
drop database example;
-- 22/08/2023
/*
TCL - Transaction Control Language:
Think of a transaction like making a pizza. You need to prepare the dough, add toppings, bake it, and serve it. Now, imagine you're doing this with a friend. If something goes wrong (like you run out of cheese), you want to make sure everything gets fixed, or you both agree to scrap the pizza-making attempt. In databases, transactions work similarly.
COMMIT: Imagine you're making the pizza with your friend, and everything goes smoothly. You both high-five each other and decide to serve the pizza. This is like saving all the changes you made in the database.
ROLLBACK: Now, imagine the cheese falls on the floor while you're making the pizza. You and your friend quickly decide to start over. In databases, if something goes wrong during a transaction, you can undo all the changes you made, just like starting over with the pizza.
ACID - Atomicity, Consistency, Isolation, Durability:
Think of ACID like the guarantees you get when you buy something online:
Atomicity: Imagine buying a set of building blocks online. Either you get the whole set delivered, or you get nothing. You don't want to receive just a few blocks. Similarly, in databases, a transaction is like getting the complete set of changes or none at all.
Consistency: Let's say you order a blue toy car online, but you receive a red one. That's a mistake, and you'd expect the company to fix it. In databases, consistency means that the changes you make are accurate and follow the rules, just like getting the right product.
Isolation: Imagine you're sharing a toy with your friend, and you each have your own copy to play with. What your friend does with their toy shouldn't affect what you're doing with yours. Similarly, in databases, even if multiple people are making changes at the same time, their changes won't mess up each other's work.
Durability: When you put your toys in a toy box, you expect them to stay there, even if you leave the room. In databases, once you've made changes and they're saved, they're like toys in the box – even if the power goes out, your changes won't disappear.
So, TCL helps you manage the steps of making changes (transactions) in a database, and ACID ensures that these changes are reliable, correct, and safe, just like ordering products online and expecting them to be right, separate from others, and not disappear after you've ordered them.
*/
-- 25/8/2023
use mysqlfsd;
create table Employees(id int primary key auto_increment,
name varchar(255),amount double);
insert into Employees(name,amount) values("Aman",10000),
("Ravi",5000),
("Krishna",1000),
("Aditya",500);
select * from employees;
desc employees;
set autocommit=0;
begin;
insert into employees values(8,"lalit",9000);
rollback;
begin;
delete from employees where id=8;
commit;
begin;
insert into employees values(8,"lalit",9000);
savepoint save1;
insert into employees values(9,"lalit 2",9000);
rollback to save1;
-- triggers
use classicmodels;
show tables;
select * from product_details;
select * from categories;
insert into categories values
(101,"phones","best phones"),
(102,"laptop","best laptop"),
(103,"fashion","best fashion"),
(104,"food","best food"),
(105,"guns","best guns");
insert into product_details values
(101,201,"Realme XT","Realme","best phone of realme"),
(101,202,"Redmi note 9","Redmi","best phone of redmi"),
(102,203,"HP Pavallion","HP","best laptop of HP"),
(102,204,"Dell inspiron","Dell","best laptop of dell"),
(103,205,"Denim jeans","denim","best jeans of denim"),
(null,206,"others","xyz","pata nahi"),
(null,207,"coffee","nescafe","best coffee");
create table soft_delete (cat_id int,product_id int,pname varchar(255)
,brand varchar(255),description varchar(255),delete_timestamp timestamp);
/*In MySQL, a delimiter is a special character used to signal the end of a SQL statement.
The most commonly used delimiter in MySQL is the semicolon (;),
which is used to separate statements from one another.*/
delimiter $$
create trigger delete_product
after delete on product_details
for each row
begin
INSERT INTO SOFT_delete values(old.category_id,old.product_id,
old.product_name,old.brand,old.product_desc,current_timestamp());
end $$
delimiter ;
delete from product_details where product_id=202;
drop trigger delete_product;
select * from product_details;
select * from soft_delete;
-- 28/8/2023
-- stored procedures
use amazon;
desc product;
select * from product;
-- Create Users table
CREATE TABLE Users (
ID SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
shipping_address TEXT,
payment_information TEXT
);
-- Create Role table
CREATE TABLE Role (
ID SERIAL PRIMARY KEY,
role_name VARCHAR(50) NOT NULL,
description TEXT
);
-- Create UserRoles mapping table
CREATE TABLE UserRoles (
user_id INT REFERENCES Users(ID),
role_id INT REFERENCES Role(ID),
PRIMARY KEY (user_id, role_id)
);
-- Create Sellers table
CREATE TABLE Sellers (
ID SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
contact_information TEXT,
description TEXT
);
-- Create Products table
CREATE TABLE Products (
ID SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
quantity_in_stock INT NOT NULL,
image_url TEXT
);
-- Create SellerProduct mapping table
CREATE TABLE SellerProduct (
seller_id INT REFERENCES Sellers(ID),