Beruflich Dokumente
Kultur Dokumente
Creating a database
Creating a table
Constraints
Tables In database:
1) at!h"detail : bat!h"no# bat!h"name# trainer# duration
$) fa!ult%"detail : trainer"id# trainer"name# &ualifi!ation#
e'perien!e# !onta!t"no
() student"detail: student"id# student"name#!onta!t"no#
bat!h"no# bat!h"name# address
Creating a database
)%nta':
Create database database"name
*+g+
!reate database Institute,
Creating a table
)%nta':
!reate table table"name
-!ol1"name#!ol$"name#.#!oln"name)
*+g+
create table batch_detail
(
batch_no integer PRIMARY KEY
batch_na!e "archar #$IQ#E
trainer "archar
batch%duration integer
&
Constraints
T%pes of !onstraints
1) N/T N011: 2revent a !olumn from
a!!epting N011 values+
$) 0NI30*: *nsures uni&ueness of the
values in a !olumn+
()2RI45R6 7*6: )ame as 0NI30*# but onl% one
table per !olumn is allowed+
8)C9*C7: Controls the value of a !olumn being inserted+
:) D*;501T: 5ssign a default value for the !olumn+
<) R*;*R*NC*): 5ssign a ;oreign 7e% !onstraint+
Inserting data in to a table
)%nta':
insert into table table"name
=alues-!ol1"val#!ol$"val#.++!oln"val)
*+g+
insert into table batch_detail
"alues( '( )Testing_'(**Ra+i" Arora*,&
-to insert !ulti.le ro/s use this 0uer1 2or an1 no o2 ti!es3
Retrie"ing the in2or!ation
)%nta':
)*1*CT >!olumn"list?
;R/4 >table"name
@W9*R* >!ondition?A
@BR/02 6 >!olumn"name-s)?A
@95=INB>!ondition?A
@/RD*R 6 >e'pression?A
45ERE Clause
)%nta':
sele!t E from table"name,
sele!t !ol1"name#++ Coln"name from table"name,
W9*R* >!ondition?
e+g+
select - 2ro! batch_detail
45ERE Trainer 7)Ra+i" Arora* 8
Result9
( testing_'( Ra+i" Arora ,
:R;ER <Y Clause
)%nta':
)*1*CT >!olumn"list?
;R/4 >table?
W9*R* >!ondition?
/RD*R 6>!olumns? @5)CGD*)CA ,
=R:#P <Y Clause
)%nta':
)*1*CT >!olumn"list?
;R/4 >table"name?
BR/02 6 >!olumn-s)
95=INB>!ondition?
*+g+
)*1*CT E ;R/4 bat!h"detail
BR/02 6 bat!h"name
95=INB trainerCH4r+ RaIiv 5roraJ ,
Quer1ing Multi.le Tables
)et /perators
1) 0nion
$) Interse!t
() 4inus
E0ui >oins
)%nta':
)*1*CT !olums"list ;R/4 tab1#tab$
W9*R* tab1+!ol"name C tab$+!ol"name ,
*+g+ :
SELECT student_id student_na!es3batch_no
batch_na!e trainer
?R:M student_detail s batch_detail b
45ERE s3batch_no 7 b3batch_no8
Cartesian >oins
)%nta':
)*1*CT !olumn"list ;R/4 tab1#tab$,
*+g+
SELECT student"id# student"name# bat!h"no# bat!h"name#
trainer ?R:M student"detail # bat!h"detail ,
:uter >oins
)%nta':
)*1*CT !olums"list ;R/4 tab1#tab$
W9*R* tab1+!ol"name C tab$+!ol"name ,
*+g+ :
SELECT student_idstudent_na!es3batch_no
batch_na!etrainer
?R:M student_detail sbatch_detail b
45ERE s3batch_no (@& 7 b3batch_no8
Sel2 >oins
)%nta':
)*1*CT !olumn"list ;R/4 tab1#tab$,,
SET :.erators
)%nta':
)*1*CT >stmt1?
0NI/N
)*1*CT >stmt$?
*+g+
)*1*CT student"name ;R/4 student"detail
W9*R* bat!h"no C O1
0NI/N
)*1*CT student"name ;R/4 student"detail
W9*R* bat!h"no C O$
Continue6
)%nta':
)*1*CT >stmt1?
INT*R)*CT
)*1*CT >stmt$?
*+g+
)*1*CT student"name ;R/4 student"detail
W9*R* bat!h"no C O1
0NI/N
)*1*CT student"name ;R/4 student"detail
W9*R* trainer C HRaIiv 5roraJ