-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.txt
4579 lines (2932 loc) · 95.6 KB
/
mysql.txt
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
What is a Database?
A Data Base is an Organized Collection of Data which can be easily
accessed, managed and updated.
In today’s World, Data plays a vital role in every business. In our day to day life, we see or
interact with many applications and Software’s, every application that we see or work with will
have two parts:
1. GUI (Graphical User Interface / Front end)
2. Database
To keep it simple GUI is the part where user interacts with (like Facebook applications – look
and feel) and the Data that we see in the application (like Facebook profile, messages, images
and videos) are pulled from Database.
End User who interacts with the application may not know how the data is being fetched and
where so much of information is stored. Internally all the dynamic content that we see in the
application is fetched from Database.
Database and all its components should be designed and configured at the time of application
development. Once the application is developed we will not be able to make changes to the
database structure as every change will hugely affect the business application GUI code.
It is very important to make sure that data is securely maintained and accurately stored.
So to maintain security and accuracy in database a set of rules / software system is defined and that
we call it as DBMS (Data Base Management System – which performs all the operations on the
database)
What is DBMS?
DBMS (Database Management System) is a software tool that is used to store and manage data
in the Database.
A database management system contains a set of programs that control the creation,
maintenance, and use of a database. Like:
Adding new data to the table.
Modifying existing data.
Deleting unwanted data.
DBMS allows different user application programs to concurrently access the same database.
Before Database and DBMS were introduced, traditional approach was to store data in flat files
(physical files in system) but there were some disadvantages with it.
What is RDBMS?
A relational database management system (RDBMS) is a Database Management System
(DBMS) that is based on the relational model introduced by E. F. Codd and most popular
databases currently in use are based on the relational database model. To put in different
words RDBMS is built on top of in which data is stored in tables and the relationships among
the data are maintained. The data stored in a table is organized into rows and columns. Each
row in a table represents an individual record and each column represents a field. A record is
an individual entry in the database.
Difference between DBMS and RDBMS
----------------------------------
For example, consider the database which stores employee information.
In DBMS, all details like empno, ename, job, salary, deptno, dname, location, street, city, state, phone will
be stored in a single table.
But in RDBMS, the data model will be designed in such a way that
like the empno, ename, job, salary and deptno will be stored in emp table and deptno, dname, location will be stored in dept table and location, street, city, state, phone will be stored under locations table.
Any information to be obtained is done by properly relating the ‘emp’, ‘dept’
and ‘locations’ tables.
SQL:
====
SQL stands for Structured Query Language.
It is used for storing and managing data in relational database management system (RDMS).
It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.
All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as their
standard database language.
SQL allows users to query the database in a number of ways, using English-like statements.
Rules:
SQL follows the following rules:
Structure query language is not case sensitive.
Generally, keywords of SQL are written in uppercase.
Statements of SQL are dependent on text lines.
We can use a single SQL statement on one or multiple text line.
Using the SQL statements, you can perform most of the actions in a database.
sql stands for structure query language designed for
accessing and managing the data in the RDBMS
Sql act as interface between user and database.
why sql ?
=========
Sql can retrieve records from the database.
sql can insert record into the database
sql can update record in to a database
sql can provides privileges to pprocedures,functions,
views, sequences----database objects
Sql is divided into five categories:
====================================
DDl --DATA DEFINITION LANGUAGE
-------------------------------
CMDS:CREATE,ALTER,TRUNCATE,DROP,RENAME
which deals with database schemas and descriptions, of how the data should reside in the database.
DML -- DATA MANIPULATION LANGUAGE
-----------------------------------
CMDS:INSERT,UPDATE,DELETE
which deals with data manipulation and includes most common SQL statements such
INSERT, UPDATE, DELETE, etc., and it is used to store, modify, retrieve, delete and update data in a database.
DCL -- DATA CONTROL LANGUAGE
-----------------------------
GRANT,REVOKE
TCL -- TRANSACTION CONTROL LANGUAGE
-------------------------------------
COMMIT,ROLLBACK,SAVEPOINT
DRL -- DATA RETRIVEAL LANGUAGE
------------------------------
SELECT
Oracle Datatypes:
=================
Number[(p,s)]:- this data is used to store numeric data
maxisize we can store up to 38 digits
p--precision
s--scale
ex:
====
empno number
empno number(5)
sal number(10,3); -- 10 is precision and 3 is scale.
Example:
=======
The precision is the number of digits in a number. It ranges from 1 to 38.
The scale is the number of digits to the right of the decimal point in a number.
It ranges from -84 to 127.
For example, the number 1234.56 has a precision of 6 and a scale of 2.
So to store this number, you need NUMBER(6,2).
char[(size)] :- this is used to store fixed characters
maximum size is 2000 bytes.
nchar[(size)] :- this data type is used to store
national characters ,maxmimum size is 2000 bytes.
varchar(size) :- this data type is used to string type of data.
maximum size is 2000 bytes.
varchar2(size) :-- this datda type is used to store string type of data
maximum size is 4000 bytes.
Difference varchar(size) and varchar2(size):
============================================
ex:
===
ename varchar(10); ename varchar2(10);
suppose a user
enter smith suppose user
interanlly enter smith
internally the sql egine
it will allocate will allocate the
memory based on memory based on user
enter value.
variable datatype size
------
---------- smith
smithnnnnn
remaining character will
be fillup with nullvalues
at right side
size if fixed size is variable value length
Date:--
========
this datatype is used to store valid date
by default the oracle use the date format
DD-MON-YYYY.
Date range :- from 1 jan 4712 BC to 31 dec 9999 A.D
TImestamp:
===========
thid datatype is used to store valid date(year,date,month) and time
(HH:MM:SS:MS)
AnsI(american national standards institution)
===========================================
Int
Float
Decimal
varchar
....etc
LOB (large binary objects)dataypes:
====================================
Bfile :- it will stores the locations of file.
BLOB --to store unstructured data -- maximum size is 4gb.
ClOB -- to store character of binary single character or
multiple characters of data ---maximum size is 4gb.
ROWID:
======
Every record in database contains physical address or rowid
1001 smith 3000
1001 smith 3000
1003 raju 4000
1005 rakesh 5000
1001 smith 3000
XML:
====
to store xml data --semi structure data --Introduced in oracle11g
Eg:
===
<empi>
<empno>1001</empno>
<ename>smith</empno>
<sal>3000</3000>
</empid>
Note:
=====
insert the values for varchar,varchar2,date datatypes
in single code('');
DDL:
=====
DDL stands for Data definition language.
DDL is used to define the definitions or schema or structure and
modify the definitions or schema or structure in the database.
DDl commands are auto commit commands.
DDL commands are
-----------------
create,alter,rename,truncate,drop
create:
=======
create command is used to define the database objects.
database objects are table,view,sequenc,procedure...etc
To create a table syntax:
========================
syntax:
=======
create table <tablename>(
columnam1 datetype(size) [constraints],
columname2 datatype(size)[constraints],
---------
----------------------
columnamen datatype(size)[constraints]
);
tablename conventions:
======================
tablename should be 30 character
tablename should contain numbers and underscore
tablename cannot start with number.
tablename is not case sensitive.
tablename cannot contain space.
ex1
===
create table emp11 it contains the columns
columns datatype length
empno number 5
ename varchar2 10
sal number 10,2
solu:
=====
create table emp11(empno number(5), ename varchar2(10),sal number(10,2));
or
mysql
------
create table emp11(empno int(5),ename varchar(10),sal decimal(10,2));
sol:
====
create table emp11(
empno number(5),
ename varchar2(10),
sal number(10,2),
hiredate date);
Ex2:
----
create table named as product_11
fields or columns -- pid,pname,price
insert five records
sol:
====
create table product_11(pid number(5),pname varchar2(10),price number(5,2)):
Ex:3
=====
create table named as course
fields or columnname dataype size
cid number 4
cname varchar2 20
joindate date
course_end_date date
to list all the table from a user:
===================================
cmd: select * from tab;
to clear screen:
=================
cmd: cl scr
to show the current user:
==========================
cmd: show user;
to describe the table structure:
================================
syntax:
========
desc[ribe] tablename;
Insert some records into emp11 table:
=====================================
syntax:
=========
insert into tablename values(column-value,----columnn-values);
sol:
====
insert into emp11 values(1001,'smith',20000);
insert into emp11 values(1002,'martin',30000);
insert into emp11 values(1003,'chaitnaya',20000);
insert into emp11 values(1004,'raju',10000);
insert into emp11 values(1005,'harsha',40000);
commit; --to store the records permanently in table.
to list all the records from the table:
=======================================
syntax:
-======
select * from tablename;
ex:
===
select * from emp11;
copy a table with schema with out data from existing table
==========================================================
syntax:
========
create table tablename as select * or[specific columns]
from tablename where anyfalse condition;
ex2:
====
create a table emp_11(empno,ename,sal) from emp11 table without data.
solu:
======
create table emp_11 as select empno,ename,sal from emp11 where 1=2;
copy a table with schema with data from existing table
==========================================================
syntax:
=======
create table tablename as select * or[specific columns]
from tablename;
ex:
===
create a table emp_with_date (empno,ename,sal) from
existing table emp11 with data.
sol:
====
create table emp_with_data as select empno,ename from emp11;
to clear the screen:
====================
oracle -- cl scr
mysql -- clear or ctrl+L
IBMDB2 !cls
Alter:
=======
alter command is used to modify the definition(structure) of a table.
1)to add a new column to an existing table:
============================================
syntax:
=======
alter table tablename add columname datatype ;
or
alter table tablename add (column datatype,columname datatype);
note:
=====
all columns added to table at last.
2)modify the column datatype size or change column datatype
============================================================
note:
=====
to decrease the size of a column first we need to make
column as empty.
to change the column datatype then first we need to
make the column as empty.
syntax:
========
alter table tablename modify columnname datatype(size)
or
alter table tablename modify (columnname datatype(size),columname datatype(size));
3) to drop a column from the table:
====================================
syntax:
=======
alter table tablename drop column columname;
4) to rename a column:
========================
syntax:
=========
alter table tablename rename column colunname to newcolumname;
5)to add a constraint for column:
==================================
syntax
------
alter table tablename add constraint constraintname;
6) to drop a constraint for a column:
======================================
syntax:
========
alter table tablename drop constraint constraintname;
example:
=========
Already we have emp11 table:
alter operations on emp11:
==========================
a) add column mobileno
sol:
=====
alter table emp11 add mobileno number(10);
b) add columns address,job
============================
sol:
---------
alter table emp11 add (address varchar2(20),job varchar2(20));
c)current size of ename is 10 then
increase the size of ename column to 20;
sol:
=====
alter table emp11 modify ename varchar2(20);
d) alter the datatype of mobileNo from number to varchar
sol:
----
alter table emp11 modify mobileno varchar2(10);
mysql
-----
alter table emp11 modify mobileno varchar(10);
Ex:
---
increase the size of ename column 10 to 20
--
alter table emp11 modify ename varchar(20);
d)drop a column address;
sol:
====
alter table emp11 drop column address;
e)rename a column job to designation
==========================================
sol:
----
alter table emp11 rename column job to designation;
7) adding constraint at alter level:
--------------------------------------
add primary key for existing table emp11 for empno.
sol:
----
alter table emp11 add primary key(empno);
RENAME(for table name):
==================
Rename is used to rename the table.
syntax:
=======
oracle:
-------
rename oldtablename to newtablename;
mysql:
------
rename table oldtablename to newtablename;
ex:
===
write a query to rename a product_11 to product_22 ?
Ex:
---
Mysql:
------
rename table product_11 to product_22;
truncate:
=========
this command is used to delete all the records or data
from the table but not schema. we cannot rollback(truncate is
auto commit cmd).
syntax:
=========
truncate table tablename;
ex:
===
write a query to trunate a table emp11 ?
sol:
===
truncate table emp11;
Drop :
======
this command is used to drop the database objects.
it will drop data from the table as well as schema.
database objects:- table,view,sequence,procedure..etc
to drop a table:
=================
syntax:
-------
drop table tablename;
or
to drop a table which contains constraint:
==========================================
syntax:
=======
drop table tablename cascade constraints;
DML
====
DML stands Data Manipulation language.
Dml commands are not auto commit commands,
we need to explicitly commit the DML statements.
DMl commands are:
==================
insert,update,delete
insert:
======
insert command is used to add record into a table.
syntax:
=======
insert into tablename values(column value1,col-value2...,colvalue-N);
EX:
---
create table emp11(empno int(5),ename varchar(10),sal decimal(10,2));
insert into emp11 values(1001,'smith',20000);
insert into emp11 values(1002,'martin',30000);
insert into emp11 values(1003,'chaitnaya',20000);
insert into emp11 values(1004,'raju',10000);
insert into emp11 values(1005,'harsha',40000);
insert with specific columns:
================================
it is used to insert the data for specific columns
syntax:
=======
insert into tablename (columnname1,coluname2,---columname3)
values(columnvalue1,columnvalue2,---columnname3);
ex:
====
write a query to insert record for specific columns(empno,ename) into
table emp11;
sol:
====
insert into emp11 (empno,ename) values(1005,'dsds');
or
insert into emp11 (empno,ename) values(&empno,'&ename');
insert into select
=================
syntax:
========
insert into trg_tablename select columnslist from src_tablename;
note:
=====
the trg_tablename columnames and src_tablename columns
should have the same schema(column data type).
Ex:
---
create table emp_dup(empno int(5),ename varchar(10));
insert into emp_dup select empno,ename from emp11;
update:
=======
update is used to modify the column values.
syntax:
=========
update tablename set columname=value,columnname=value where condition
note:
=====
if where condition is not specified total records will be
updated in the table.
ex:
===
write a query to update a column ename raju to another name
where empno 1004?
solu:
=====
update emp11 set ename='rajuhadoop' where empno=1004;
ex:
---
update the sal column of empno =1006.
sol:
----
update emp11 set sal=4000 where empno=1006
write a query to update a column ename and sal where
empno 1005?
sol:
=====
update emp_11 set ename='sdsd',sal=4000 where empno=1005;
Assignment:
===============
update as select;
Difference between truncate and delete?
delete:
========
this command is used to delete a specific record
or all the records.
syntax:
=======
delete from tablename where condition; --specific record
or
delete from tablename -----all record will be deleted.
or
delete tablename where condition.
ex:
---
delete the record of empno 1006
sol: delete from emp11 where empno=1006;
Exercise
-------------------------------
1. Create table emp12 from table emp ?(Hint : use emp table)-->copy a table
Ans: create table emp12 as select * from emp;
2. create table emp13 from emp and into the new table copy only the structure but do not copy data ?
Ans: create table emp13 as select * from emp where 5=10
3. Increase size of ENAME field to 20 use emp13 table?
Ans: alter table emp13 modify ename varchar(20);
ALias names:
=============
alias name is temporary name for column or table.
syntax:
=======
coluname as aliasname
or
columname aliasname
columnae "Hire_date"
ex:
====
select empno,ename,sal as salary from emp_11;
select e.empno,e.ename,e.sal from emp e;
CONSTRAINTS:
============
CONSTRAINT IS A RULE OR RESTRICTION WHICH IMPOSED ON COLUMNS
OF THE TABLE.
CONSTRAINTS CAN BE APPLIED AT THE TIME OF COLUMN DEFINITION.
CONSTRAINTS CAN BE APPLIED AFTER THE TABLE DEFINITION
CONSTRAINSTS CAN BE APPLIED AT THE ALTER .
CONSTRAINTS ARE DIVIDED INTO THREE
1)DOMAIN INTEGRITY CONSTRAINTS
-------------------------------
NOT NULL
CHECK
2)ENTITY INTEGRITY CONSTRAINTS
--------------------------------
UNIQUE
PRIMARY KEY
3)REFERENTIAL INTEGRITY CONSTRAINTS
------------------------------------
FOREIGN KEY
1)DOMAIN INTEGRITY CONSTRAINTS:
================================
NOT NULL :
==========
NOT NULL CONSTRAINT ARE USED TO AVOID NULL VALUES FOR THE
SPECIFIED COLUMNS.