0% found this document useful (0 votes)
11 views

SQL Class-12

This will helpful vczzx dfggdc fdvfx ffvd. Fffvvf. Fedvfgg. Frrrrvff

Uploaded by

glimpsesofworld0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Class-12

This will helpful vczzx dfggdc fdvfx ffvd. Fffvvf. Fedvfgg. Frrrrvff

Uploaded by

glimpsesofworld0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQLQUERI

ES

Cr
eat
ingDat
abase

Youmustbeawar ethatt
het
abl
esareavai
l
ableindatabases.Ther
efor
e,t
hefi
rstand
fundamental
taski
nMy SQLi
stocr
eateadatabase.Thecommandt ocreat
eadat abaseCr
eat
e
Database.

Sy
ntax:
Creat
eDat
abase<Dat
abaseName>;

Or
,

Cr
eat
eDat
abase[
Ifnotexi
sts]<Dat
abaseName>;

Forexampl
e,

Cr
eat
eDat
abaseMy
Dat
abasel
;

I
tcr
eat
esadat
abasewi
tht
hespeci
fi
ednameMy
Dat
abasel
int
hesy
stem (
ifi
tdoesnotexi
st)
.

Usi
ngDat
abase

Touset
hedat
abase,
thesy
ntaxi
sst
atedbel
ow:

Sy
ntax:
Use<Dat
abase>;

Forexampl
e,UseMy
Dat
abasel
;

Thus,i
twill
opent
heexi
sti
ngdat
abase(
say
,My
Dat
abasel
)wher
ewecanper
for
mvar
ious
operat
ions.

Di
spl
ayDat
abase

I
fyouwantt
odi
spl
ayal
i
stcont
aini
ngal
lthedat
abasesav
ail
abl
einMy
SQLt
henuse

t
hesy
ntaxas:

Sy
ntax:
ShowDat
abases;

I
twi
l
ldi
spl
ayal
ltheexi
sti
ngdat
abaseswhi
chhav
ebeencr
eat
edi
ntheMy
SQL
Remov
ingDat
abase

I
fadat
abasei
snotofanyuset
heny
oucandel
etebyusi
ngt
heDr
opcommand.

Sy
ntax:
DropDat
abase<Dat
abaseName>;

Forexampl
e,Dr
opDat
abaseMy
Dat
abasel
;

Thus,
thespeci
fi
eddat
abase(
say
,My
dat
abasel
)wi
l
lber
emov
edf
rom t
hesy
stem.

Cr
eat
ingTabl
einaDat
abase

creat
eat abl
einthedatabase,
youhav
etospeci
fyt
hef
iel
dsf
oreachcol
umnwi
tht
her
equi
sit
e
datatype.Uset
hefoll
owingsyntax
:

Sy
ntax

Cr
eat
eTabl
eTabl
e_names

(
<Col
umnName><Dat
aTy
pe>(
<si
ze>)
,

<Col
umnName><Dat
aTy
pe>(
<si
ze>)
,

<Col
umnName><Dat
aTy
pe>(
<si
ze>)
);

Forexampl
e,

Cr
eat
eTabl
ePr
oject

(
Stu_
IDchar
(5)
,

Namev
archar
(25)
,

Cl
asschar
(5)
,

Mar
ksi
nt(
5),

Gr
adechar
(5)
);

Const
rai
ntsi
ntabl
est
ruct
ure

Theconst
rai
ntsar
esomer
est
ri
cti
onsenf
orcedont
hedat
acol
umnsofat
abl
e.I
tal
soensur
es
t
heaccur
acyandr
eli
abi
l
ityoft
hedat
ainadat
abase.Somecommonl
yusedconst
rai
ntsi
n

SOLar
ement
ionedbel
ow:

NotNul
l
:Itensur
est
hatacol
umncannothav
eaNULLv
alue.

Def
aul
t:I
tal
l
owsst
ori
ngsamev
aluei
nacol
umnofal
lther
ecor
ds.

Uni
que:
Itdoesnotal
l
owt
oinser
tadupl
i
cat
eval
uei
nacol
umn.

Pr
imar
yKey
:Itenf
orcest
het
abl
etoacceptuni
quedat
aforaspeci
fi
ccol
umn.

Forei
gnKey:
Itcr
eatesali
nkbetweentwoormor
etabl
es.Thepr
imar
ykeyofonet
abl
eis
ref
err
edtoastheforei
gnkeyf
ortheanot
hert
abl
e.

Vi
ewi
ngTabl
eSt
ruct
ure

St
ruct
ureoft
het
abl
ecanbev
iewedbyusi
ngt
hef
oll
owi
ngsy
ntax:

Sy
ntax:
Desc<Tabl
eName>;

Or

Descr
ibe<Tabl
eName>;

Forexampl
e,DescPr
oject
;

I
nser
ti
ngDat
aint
oTabl
e

Toent
ert
hedat
aint
het
abl
ePr
oject
,uset
hef
oll
owi
ng

sy
ntax:

I
nser
tInt
o<Tabl
eName><Col
umnLi
st>Val
ues(
<Val
ue1>,
<Val
ue2>,
<Val
ue3>)
;

Forexampl
e,

Toent
err
ecor
dsi
nthet
abl
e:

I
nser
tInt
oPr
oject(
Stu_
ID,
Name,
Class,
Mar
ks,
Grade)Val
ues(
T001'
,'
Raj
i
vKi
shor
e',
'XI
IA'
,91,
'
A'
);

Showi
ngRecor
dsofaTabl
e
Tov
iewt
her
ecor
dsoft
het
abl
e,uset
hissy
ntax
:

Sy
ntax:

Sel
ect*Fr
om <Tabl
eName>;

Sel
ect*Fr
om Pr
oject
;

Modi
fyi
ngDat
ainaTabl
e

Themodifi
cati
onofdat
acanbeperfor
medbyusi
ngUPDATEcommandal
ongwithWHERE
cl
ause.Thenewdatacanbeent
eredbyusi
ngt
hekeywor
dSET.Uset
hesy
ntax,
asshownbel
ow:

Sy
ntax:

Updat
e<Tabl
eName>

Set<Fi
eldName><newv
alues>

Wher
e<Fi
eldName>=<ex
ist
ingv
alue>;

Forexampl
e,

Updat
ePr
oject

SetMar
ks95

Wher
eMar
ks=91;

Del
eti
ngDat
afr
om aTabl
e

TheDELETEcommandi
susedt
oremov
eanyr
ecor
dsf
rom at
abl
e.Youcanper
for
m

t
askbyusi
ngt
hesy
ntaxas:

Sy
ntax:

Del
eteFr
om <Tabl
eName>

Wher
e<Fi
eldName><Condi
ti
on(
ifany
)>:

Forexampl
e,
Del
eteFr
om Pr
oject

Wher
eMar
ks<90;

Edi
ti
ng/
Modi
fyi
ngCol
umnsi
naTabl
eusi
ngofAl
tercommand

Underedi
ti
ngcol
umns,
youcanper
for
mthef
oll
owi
ngmodi
fi
cat
ionsi
nthef
iel
dsofat
abl
e.They
ar
e:

Addi
ngCol
umnsi
naTabl
e

Youcanaddoneormorecolumnsintheexi
sti
ngt
abl
e.I
tcanbedonebyusi
ngAl
terTabl
e
commandalongwi
thAddclause.I
tisusedas:

Sy
ntax:

Al
terTabl
e<Tabl
eName>Add(
<Col
umnName><Dat
aTy
pe><Si
ze>)
;

Forexampl
e,

Al
terTabl
ePr
oject

Add(
Adm_
Nov
archar
(25)
);

Thus,
itwi
l
lcr
eat
eanewcol
umn(
fi
eld)asAdm_
Noi
ntheexi
sti
ngt
abl
e.

Changi
ngor
derofaCol
umn

songwit
hModif
yclauset
ospeci
fyt
heposi
ti
onofcol
umn(t
e,Fi
rstorucant
heModi
fycl
ause
tospeci
fyt
heei
tcanbeperf
ormedusi
ngAlt
erTabl
ecommand

Sy
ntax

Al
terTabl
e<Tabl
eName>

Modi
fy<Col
umnName><Dat
aTy
pe(
size)
>Fi
rst<Col
umnName:

Or
,
Al
terTabl
eTabl
eName>

Modi
fy<Col
umnName><Dat
aTy
pe(
size)
>Af
ter<Col
umnName>;

Forexampl
e:

Al
terTabl
ePr
oject

Modi
fyAdm_
Nov
archar
(25)

Af
terSt
u_I
D;

Renami
ngaCol
umn

TheAlt
erTabl
ecommandi sal
sousedt
orenameacol
umni
nthet
abl
e.I
tisusedal
ongwi
tht
he
Changecl
ause,asshown:

Sy
ntax:

Al
terTabl
e<Tabl
eName>

Change<Ol
dCol
umnName><NewCol
umnName>Dat
aTy
pe(
Size)
;

Forexampl
e,

Al
terTabl
ePr
oject

ChangeAdm_
NoI
ndex
_Nov
archar
(25)
;

Del
eti
ngCol
umnsi
naTabl
e

Youcandel
eteacol
umninatabl
eusi
ngAl
terTabl
ecommandal
ongwi
tht
heDr
opCol
umnI
t
canbeusedasmenti
onedbel
ow:
Sy
ntax:

Al
terTabl
e<Tabl
eName>

Dr
opCol
umn<Col
umnName>;

Forexampl
e,

Al
terTabl
ePr
oject

Dr
opCol
umnI
ndex_
No;

Thus,
thecol
umn'
I
ndexNo'
wil
lbedel
etedf
rom t
het
abl
e.

Remov
ingaTabl
e

Removi
ngatabl
emeansdel
eti
ngtheent
ir
etabl
efr
om adat
abase.I
tcanber
emov
edf
rom a
dat
abasebyusi
ngt
hesynt
axas:

Sy
ntax:

Dr
opTabl
e<Tabl
eName>;

Forexampl
e,

Dr
opTabl
ePr
oject
;

Thus,
thet
abl
ePr
ojectwi
l
lbedel
etedper
manent
lyf
rom t
heexi
sti
ngdat
abase.

TouseWHEREcl
ausewi
thSQLcommands

TheWHEREclauseisacondi
ti
onalcl
auseal
lowi
ngt
oret
ri
evet
hesel
ect
iver
ecor
dsbasedona
gi
vencondi
ti
on.I
tisusedasshownbelow:

Sy
ntax:
SELECT<col
umn1>,
<col
umn2>

FROM <Dat
abase>.
<Tabl
ename>

WHERE<condi
ti
on>;

Mostofthecases,t
heWHEREclauseisusedalongwi
thl
ogi
caloper
ator
s(AND,OR,NOT)
,
BETWEENoper at
or,I
NandNOTI Noperat
ors,I
SNULLandISNOTNULLoperator
s.LI
KE
oper
ator
,rel
ati
onaloper
ator
s(<,
<=,>=,
=,!=)et
c.

You might also like