-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMySqlCheatSheet.sql
1627 lines (1242 loc) · 43.7 KB
/
MySqlCheatSheet.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
-- single line comment
/* multiple line comment */
# documented comment
-- Checking no. of databases
show databases;
-- creating our database
create database AmanDb;
-- deleting db
drop database amandb;
-- using database
use amandb;
/*
common data types
tinyint
smallint
mediumint
int
bigint
float
double
decimal
char
varchar
date
datetime
timestamp
text
*/
-- ddl - create, alter , drop , truncate
-- dml - insert, update delete
-- dql - select
-- dcl - grant, revoke
-- tcl - commit rollback , savepoint
-- creating table
create table TableName(col0 bit,col1 tinyint,col2 smallint,col3 mediumint,col4 int,
col5 integer,col6 bigint,col7 boolean,col8 bool);
create table TableName2(col1 float,col2 double,col3 decimal(5,2));
create table TableName3(col1 date,col2 time,col3 datetime,col4 timestamp,col5 year);
create table TableName4(col1 char(10),col2 varchar(255),col3 tinytext,col4 mediumtext,col5 text,col6 longtext);
-- create table TableName5(col1 binary,col2 varbinary,col3 tinyblob,col4 blob,col5 mediumblob,col6 longblob,col7 enum,col8 set);
create database Amandb3;
use amandb3;
create table stu(rollno int);
insert into stu values(101),
(102),
(103);
select * from stu;
alter table stu add column address varchar(50);
alter table stu drop column name;
alter table stu add column phone varchar(50) after rollno;
alter table stu modify column phone int;
desc stu;
describe stu;
alter table stu rename column phone to MobilePhone;
alter table stu rename to Students_Details;
select * from Students_Details;
drop table students_details;
create table Employee23(id int,name varchar(50),dob date);
insert into Employee23 values(21,"Aman",'1999-04-22');
select * from employee23;
insert into employee23(name) values("abcd");
insert into Employee23 values
(11,"xyz",'1888-04-22'),
(15,"xyz1",'1898-04-22'),
(18,"xyz2",'1828-04-22'),
(30,"xyz9",'1288-04-22');
select * from classicmodels.customers;
select customerNumber,customerName from classicmodels.customers;
insert into employee23(id,name) select customerNumber
,customerName from classicmodels.customers;
select * from employee23;
set sql_safe_updates = 0;
-- operators
create table Mycustomers(customerNumber int
,customerName varchar(255),city varchar(255),creditlimit double);
insert into mycustomers select customerNumber
,customerName,city,creditlimit from classicmodels.customers;
select * from mycustomers;
select * from mycustomers where customername='herkku gifts';
select * from mycustomers where creditlimit > 10000;
select * from mycustomers where creditlimit < 10000;
select * from mycustomers where creditlimit >= 11000;
select * from mycustomers where creditlimit <= 11000;
select * from mycustomers where creditlimit != 11000;
select * from mycustomers where creditlimit <> 11000; -- not equal to
select * from mycustomers where customerName like 'R%';
select * from mycustomers where customerName like '%Inc';
select * from mycustomers where customerName like '%Online%';
-- constraints (Rules)
/*
create table students(roll int unique not null,customer_id varchar(255));
drop table students;
insert into students values(2,"xyz");
insert into students(customer_id) values("xyz");
select * from students;
*/
-- default
/*
create table students(roll int unique not null,
customer_id varchar(255),
city varchar(255) default 'Delhi'
);
drop table students;
insert into students values(2,"xyz","mumbai");
insert into students(customer_id) values("xyz");
insert into students(roll,customer_id) values(34,"xyz 2.0");
select * from students;
desc students;
create table students(roll int primary key,
customer_id varchar(255),
city varchar(255)
);
drop table students;
insert into students values(2,"xyz","mumbai");
insert into students(customer_id) values("xyz");
create table students(roll int,
customer_id varchar(255),
city varchar(255),
primary key(roll)
);
drop table students;
insert into students values(2,"xyz","mumbai");
insert into students(customer_id) values("xyz");
*/
/*
create table students(roll int,
customer_id varchar(255),
city varchar(255),
constraint myrule primary key(roll)
);
drop table students;
insert into students values(2,"xyz","mumbai");
insert into students(customer_id) values("xyz");
desc students;
*/
/*
-- super key
use amandb3;
create table students(
stu_name varchar(255),father_name varchar(255),
city varchar(255),
constraint myrule primary key(stu_name,father_name) -- composite key
);
insert into students values
("ram","dasrath","ayodhya"),
("ram","ghanshyam","ayodhya"),
("shyam","dasrath","ayodhya"),
("shyam","ghanshyam","ayodhya");
select * from students;
insert into students values("ram","dasrath"," delhi");
*/
-- foreign key
/*
create table categories
(cat_id int primary key,
cat_name varchar(255),
description varchar(255)
);
create table products
(category_id int,
pro_id int primary key,
p_model varchar(255),
description2 varchar(255),
-- foreign key(category_id) references categories(cat_id)
constraint myforeignkey foreign key(category_id) references categories(cat_id)
);
insert into categories values
(101,"shirts","denim"),
(102,"jeans","levis"),
(103,"laptop","laptops from india"),
(104,"phone","realme");
select * from categories;
insert into products values
(103,201,"lenevo thikpad","hbhhjm"),
(103,202,"lenevo ideapad 100","hbhhjm"),
(null,203,"lenevo ideapad 101","hbhhjm");
select * from products;
insert into products values
(105,204,"lenevo thikpad","hbhhjm"); -- error
use amandb3;
drop table categories; -- error
drop table products;
drop table categories;
update categories set cat_id=107 where cat_id=103; -- restrict
update categories set cat_id=107 where cat_id=104;
create table categories
(cat_id int primary key,
cat_name varchar(255),
description varchar(255)
);
create table products
(category_id int,
pro_id int primary key,
p_model varchar(255),
description2 varchar(255),
constraint myforeignkey foreign key(category_id)
references categories(cat_id)
on delete cascade
on update cascade );
insert into categories values
(101,"shirts","denim"),
(102,"jeans","levis"),
(103,"laptop","laptops from india"),
(104,"phone","realme");
select * from categories;
insert into products values
(103,201,"lenevo thikpad","hbhhjm"),
(103,202,"lenevo ideapad 100","hbhhjm"),
(null,203,"lenevo ideapad 101","hbhhjm");
select * from products;
insert into products values
(105,204,"lenevo thikpad","hbhhjm"); -- error
update categories set cat_id=107 where cat_id=103; -- restrict
update categories set cat_id=107 where cat_id=104;
delete from categories where cat_id=107;
select * from categories;
select * from products;
*/
create table categories
(cat_id int primary key,
cat_name varchar(255),
description varchar(255)
);
create table products2
(category_id int,
pro_id int primary key,
p_model varchar(255),
description2 varchar(255),
constraint myforeignkey foreign key(category_id)
references categories(cat_id)
-- on delete set null
-- on update set null
on update restrict -- by default
on delete restrict -- by default
);
insert into categories values
(101,"shirts","denim"),
(102,"jeans","levis"),
(103,"laptop","laptops from india"),
(104,"phone","realme");
select * from categories;
insert into products values
(103,201,"lenevo thikpad","hbhhjm"),
(103,202,"lenevo ideapad 100","hbhhjm"),
(null,203,"lenevo ideapad 101","hbhhjm");
select * from products;
insert into products values
(105,204,"lenevo thikpad","hbhhjm"); -- error
update categories set cat_id=107 where cat_id=103; -- restrict
update categories set cat_id=107 where cat_id=104;
insert into products values
(107,205,"lenevo thikpad","hbhhjm");
delete from categories where cat_id=107;
select * from categories;
select * from products;
drop table products;
drop table categories;
-- keys finished
-- joins started
use amandb3;
create table categories
(cat_id int primary key,
cat_name varchar(255),
description varchar(255)
);
create table products
(category_id int,
pro_id int primary key,
p_model varchar(255),
description2 varchar(255),
constraint myforeignkey foreign key(category_id)
references categories(cat_id)
on update restrict -- by default
on delete restrict -- by default
);
insert into categories values
(101,"shirts","denim"),
(102,"jeans","levis"),
(103,"laptop","laptops from india"),
(104,"phone","realme");
insert into products values
(103,201,"lenevo thikpad","hbhhjm"),
(103,202,"lenevo ideapad 100","hbhhjm"),
(null,203,"lenevo ideapad 101","hbhhjm"),
(101,205,'superdry','hhhhb');
select 10+50 as sumcol; -- as for alias
select * from categories,products; -- cross join
select * from categories;
select * from products;
-- inner joins
select * from categories
inner join products
on categories.cat_id = products.category_id;
select * from categories as c
inner join products as p
on c.cat_id = p.category_id;
-- left join
select * from categories as c
left join products as p
on c.cat_id = p.category_id;
-- right join
select * from categories as c
right join products as p
on c.cat_id = p.category_id;
-- only left
select * from categories as c
left join products as p
on c.cat_id = p.category_id
where p.category_id is null;
-- only right
select * from categories as c
right join products as p
on c.cat_id = p.category_id
where c.cat_id is null;
-- emulating full outer join
-- union all
select * from categories as c
left join products as p
on c.cat_id = p.category_id
union all
select * from categories as c
right join products as p
on c.cat_id = p.category_id;
-- union
select * from categories as c
left join products as p
on c.cat_id = p.category_id
union
select * from categories as c
right join products as p
on c.cat_id = p.category_id
where c.cat_id is null or
p.category_id is null;
-- remove common
select * from categories as c
left join products as p
on c.cat_id = p.category_id
where p.category_id is null
union
select * from categories as c
right join products as p
on c.cat_id = p.category_id
where c.cat_id is null;
-- remove common part from full join simulation
use amandb3;
select * from categories as c
left join products as p
on c.cat_id=p.category_id
where p.category_id is null -- only left (uncommon data)
union
select * from categories as c
right join products as p
on c.cat_id=p.category_id
where c.cat_id is null; -- only right (uncommon data)
--
select * from classicmodels.orders;
use classicmodels;
select * from orders;
select count(*) as 'all orders' from orders;
select count(*) 'all orders' from orders;
-- aggregate functions
-- count
-- max
-- min
-- avg
-- sum
-- group by
select status,count(*) 'order count' from orders
group by status;
select * from customers;
select country ,count(*) from customers
group by country;
select country ,sum(creditlimit) from customers
group by country
order by country;
select country ,avg(creditlimit) from customers
group by country
order by country;
select country ,min(creditlimit),max(creditlimit)
from customers
group by country
order by country;
select * from customers
order by country,state;
-- group by according to multiple column
select country,state,count(*) from customers
group by country,state
order by country,state;
-- inconsistency in data using group by
select city,country,state,count(*) from customers
group by country,state
order by country,state;
select city,country,state,count(*) from customers
group by country,state,city
order by country,state;
-- filtering
select country,state,count(*) from customers
group by country,state
order by country,state;
-- employee
-- country
-- state
-- counting employees that are living in same country and state
-- counting employees that
-- are living in same country and state where the no. of
-- employees greater than 2
select country,state,count(*) from customers
group by country,state
-- error -- where count(*) > 2
having count(*) > 2
order by country,state;
-- subquery (nested query)
use classicmodels;
select * from customers order by contactfirstname ;
select * from customers
where creditlimit > ( select creditlimit
from customers
where contactfirstname='peter' and contactlastname='Ferguson');
select * from customers
where creditlimit > -- >117300
(
select creditlimit -- 117300
from customers
where contactlastname='Ferguson'
and contactfirstname=( -- peter
select contactfirstname
from customers where customernumber = 114
)
);
-- inner query executes first
select * from customers
where city in ('Allentown','Amsterdam');
select * from customers
where city in (select city from customers
where city like 'A%');
select * from customers
where city =any(select city from customers
where city like 'A%');
-- =any is just like in
use amandb;
select * from categories;
select * from products;
select * from products as p
where exists
(select * from categories as c
where p.category_id=c.cat_id);
select * from products as p
where not exists
(select * from categories as c
where p.category_id=c.cat_id);
use classicmodels;
select buyprice from products order by buyprice; -- start=15.91 end=103.42
select priceeach from orderdetails order by priceeach;-- start=26.55 end=70.08
select productname,buyprice -- start=15.91 end=103.42
from products
where buyPrice >Any(
select priceeach from orderdetails -- start=26.55 end=70.08
)
order by buyprice;
-- final result start = 26.72 end=103.42
/*
-- error in result
select productname,buyprice from products
where buyPrice <Any (select priceeach from orderdetails)
order by buyprice; -- start= 15.91 end=103.42
*/
/*
> 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 productname,msrp -- start=15.91 end=103.42
from products
where msrp >All(
select priceeach from orderdetails -- start=26.55 end=70.08
);
select * from employees;
select * from products;
-- dcl means data control language
use classicmodels;
create user 'junior' identified by '8861';
grant select on employees to junior;
grant select,insert,update,delete on employees to junior;
grant All on employees to junior;
revoke delete on employees from junior;
revoke All on employees from junior;
drop user junior;
select * from mysql.user;
-- prepared statement
prepare showCustomerData from
'select * from customers where customernumber=?';
set @custId=112;
execute showCustomerdata using @custid;
set @custId=103;
execute showCustomerdata using @custid;
deallocate prepare showCustomerdata;
/*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.*/
-- stored procedure in mysql
delimiter //
create procedure selectAllProduct()
begin
select * from Users;
end //
delimiter ;
call selectAllProduct();
drop procedure selectAllProduct;
delimiter //
create procedure selectAllProduct()
begin
select * from Users;
end //
delimiter ;
call selectAllProduct();
drop procedure selectAllProduct;
call selectAllUsers();
-- IN example
delimiter //
create procedure selectUserById(IN uid int)
begin
select * from Users where id=uid;
end //
delimiter ;
call selectUserById(1);
-- out example
delimiter //
create procedure getUserEmailById(in uid int,out uemail varchar(50))
begin
select email into uemail from Users where id=uid;
end //
delimiter ;
call getUserEmailById(1,@EMAIL);
SELECT concat('THE EMAIL IS :' , @EMAIL) as 'result';
-- inout
delimiter //
create procedure updateProductPrice(in pid int,inout price2 double)
begin
update products set price=price2+price2*15/100 where id=pid;
select price into price2 from products where id=pid; -- new price
end //
delimiter ;
set @temp=0.0;
select price into @temp from products where id=1; -- old price
call updateProductPrice(1,@temp);
select @temp as 'updated price';
select * from products;
-- -------------------------------------
CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
SET @x = 0;
REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END;
call dorepeat(5);
/*
If you use the mysql client program to define a
stored program containing semicolon characters, a problem arises.
By default, mysql itself recognizes the semicolon as a statement delimiter,
so you must redefine the delimiter temporarily to cause mysql to pass the
entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command.
*/
delimiter $$
CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
SET @x = 0;
REPEAT SET @x = @x + 1;
UNTIL @x > p1 END REPEAT;
select @x as 'value of x';
END $$;
delimiter ;
call dorepeat(10);
call dorepeat(80);
call dorepeat(100);
drop procedure dorepeat;
/*
delimiter is changed to $$ to enable the entire definition to be passed to the
server as a single statement, and then restored to ; before invoking the procedure.
This enables the ; delimiter used in the procedure body to be passed through
to the server rather than being interpreted by mysql itself.
*/
delimiter $$
create procedure getCustomers()
begin
select * from customers;
end $$
delimiter ;
call getcustomers();
-- loops within stored procedures
create table MathsTable(col1 int);
delimiter $$
create procedure tableprint(num int)
begin
set @i=1;
repeat
insert into MathsTable values(num*@i);
set @i=@i+1;
until @i>10
end repeat;
end $$;
delimiter ;
call tableprint(7);
drop procedure tableprint;
select * from mathstable;
drop table mathstable;
-- triggers
create table newtable( column1 int);
create trigger newentries
before insert on mathstable
for each row
insert into newtable values(new.col1);
insert into mathstable values(79);
select * from newtable;
select * from mathstable;
show triggers;
delimiter $$
create trigger remoldentries
after insert on mathstable
for each row
begin
insert into newtable values(new.col1);
end $$
delimiter ;
-- similarly we can implement
-- before update after update
-- befor delete after delete
/*You can't delete the rows from the same table on which you implement the trigger.
Because on insert mysql locks the table and can't delete the rows because delete
needs locking so its a deadlock sitution, that's why mysql would not allow this.*/
drop trigger remoldentries;
delimiter $$
create trigger updateEntry
after update on mathstable
for each row
begin
if new.col1 >50 then
insert into newtable values(new.col1);
end if;
end $$
delimiter ;
update mathstable set col1=80 where col1=79;
select * from newtable;
update mathstable set col1=23 where col1=79;
-- tcl transaction control language
-- commit,rollback,savepoint
use classicmodels;
select * from categories;
desc categories;
create table products2
(category_id varchar(20),
pro_id int primary key,
p_model varchar(255),
description2 varchar(255),
constraint myforeignkey2 foreign key(category_id)
references categories(categoryid));
insert into products2 values
('103',201,"lenevo thikpad","hbhhjm"),
('103',202,"lenevo ideapad 100","hbhhjm"),
(null,203,"lenevo ideapad 101","hbhhjm"),
('101',205,'superdry','hhhhb');
set autocommit=0;
set autocommit=off;
-- transactions
set autocommit=1;
set autocommit=on;
start transaction;
insert into products2 values ('103',213,"lenevo thikpad","hbhhjm");
commit;
select * from products2;
create table User1(id1 int,name1 varchar(255),amount1 float);
create table User2(id2 int,name2 varchar(255),amount2 float);
insert into User1 values(100,'aman',7000);
insert into User2 values(100,'Rohan',8000);
start transaction;
update user1 set amount1=amount1-500;
update user2 set amount2=amount2+500;
commit;
select * from user1;-- 7500
select * from user2;-- 8500
start transaction;
update user1 set amount1=amount1-500;
update user2 set amount2=amount2+500;
commit;
select * from user1;-- 7000
select * from user2;-- 9000
start transaction;
update user1 set amount1=amount1-500;
update user2 set amount2=amount2+500;
rollback;
delete from user1 where id1=100;
rollback;
insert into user1 values(101,'ankit',9000);
rollback;
start transaction;
insert into user1 values(101,'ankit',9000);
savepoint s1;
insert into user1 values(102,'ankit 2',19000);
rollback to s1;
select * from user1;-- 7000
desc employees;
create table AllData(cat_id int,category_name varchar(255)
,product_id varchar(255)
,product_name varchar(255));
insert into Alldata select category_id,category_name,
pro_id,p_model from categories c
inner join products2 p
on p.category_id=c.categoryid;
select * from AllData;
drop view udata;
create view udata as
select cat_id,category_name
from AllData;
select * from udata;
insert into udata values(108,'Goods');
insert into udata values(109,'Goodies');
select * from udata;
select * from AllData;
delete from udata where cat_id=101;
select * from udata;
select * from AllData;
update udata set category_name='Hp ke laptop' where cat_id=108;
select * from udata;
select * from AllData;
drop table alldata;
use stugre;
show tables;
describe student;
-- subqueries,CTE and window function
# Alter Query
-- 1) Used to make changes in the structure of the table.
-- 2) There are 4 commands with the alter query
-- a) change - Used to rename a column + add/change its data types +
-- add/change/remove its constraints.
-- b) modify - Used to change the data types of a colum +
-- add/change/remove its constraints
-- c) add - used to add a new column with its data type + add constraint
-- d) drop - used to drop an exitsing column
-- e) rename - used to rename a column.
alter table students change City Location varchar(40) not null;
alter table students change Location Location varchar(40);
# rename Course to subject with datatype varchar(30);
alter table students change Course Subject varchar(30);
-- string and date functions
-- String functions - LCase, UCase, Length, Mid, concat
select lcase('NASA');
select lower('HELLO');
select ucase('india') as Country;
select upper('india') as Country;
select Length('HEllo') as Length; -- length of the string
-- add1,add2,city
select lcase('hello');
select concat('Hello',' ','World');
select concat(ucase('hello'),' ',ucase('world'));
-- trim - removes leading or trailing whitespace from string
select length(' Welcome to SQL '); -- 16