SQL Notes
SQL Notes
FORETGN KEY
t ib afilda st Aodls tha usxpntia aeertePRIMERYIKy
TCLTIamsactien ohel Longuage)
Aleus te manage tombadions iSa daalas.
Mamane dhomges made-to the dan m tabh by a DML
Copls
coMMT, RoLLBACK, SAVEPOTNT
Soea s Rostre doatalbase
Mabe p@nanad te Jast cowwttoo Sotha y pm Wsak
Staato te that beiit whoreh Sunpl
dho
SQL (sttsad Guthux Lamquan-
OEecde qpezia, driine dota,ima ocohols , uwbedale.
Aucerdb.
ORDMS-Ralatiemal Dotabose Monanemant Susem
b a i e sQ,all mesn database systems Such as-
MS SQL Seue 18M D82,01aca MysQL, Accas
* E e TABLE i baekon uh into- Smaeh anttis callod.
SQL STATEMENTS
CRERTE
DATABASE Gueals a naur datalaRo
ALTER3DATABASE Medis dota Ssas a
Gaalb a nr tall
CRERTE TABLE
ALTERTABLE Maddub o tal
TABL Detis a taN
DRo
DROP
INDEX Deloteb a
SQL SELECT STATEMENT
SELECTceli, col SELECT%
SELECTDISTINCT
SELECT DISTINCT Gurdb Solads distin odbie
FROM Custemeb
wHERE
SELECT cel1,ol22- Vsed te ftlha suotds
FROM talble_mae SQL nadsarounel tzo
wHERE conditioni fios but notuith nei
motrer Lndition.
AND, OR uad fou }tesimg en
SELeCTPRoM tnbl
wHERE Cond 1 AND Lend 2 AND..,JwHERE enel1 oR iencd2ak
wHERCG NOT Cond Use () to mist cendikienb
ORDER BY a ln
SELECTAcol1, col2- usedl to sot oesults by a
paducullah csl in ASC
FROM t a Desc way
TESTNULL VALUES
, 7 Cont be wsed fer tMULL VALUES..
md 1 NOT NULL
e s 2 15 NULL SELECT col- ammis
SELECT col-nome FROM talale1
FROM talola
w H E R El 1 1 6
NOT NUL
wHERE col-1 1sNULL
uistimg Jucotds.in a tale.
UPDATE Vsed te malily
UPDATE
UPDATE table1
tale Schmict, Coh=
GET ContaaNam A e l WHERE
SET Contaatalanme udati L cel col
WHERE GAtomehID
UPDATE MULTIPLE RECORDS
UPDATECovdtadIName
Gstemme nb UPDATES ALL nluls
='Juom'
SET wih Mito s cealu
WHEBE Coumiy e 'MLnics j
3tcotols im a table
Uadte Deleta enistimg
DELETTE Dletesboked ea condlihiba
FROM talble 1
WLETE
HEBE connditien su
uoouu of ttalsle
s o ab
Cutomes Delite all
Delutes
stincdu
DELETE FROM
net daataq tulil Kacpiasq
ORACLE adiblutes k ndoodbs.
SOSERNER
M-ACCES6 MYSa
MN),MAX() Moc
SELECT MAXcok-nonom
Mn MIN
SELECT nome) FROM todolo1
FROM talola,1 wWHEREcnd
wHERE n d i
Rctufvn K
ewb, rotch
Ratuwb the Rutums he a o e
GuaChagt valut o tetal sum o wwm uo
COUNT)AVGL) SUM)-
SELECT CoUNTL an)ANG(mne)|0M(Ll-ram
FROM table_1
wHERE_comdibiarnj
LIKE ubed im.a uhhe dause tesonrh fi ashocite patiesn
The þetuumt siqm hosebtats
CESS
0,1 e altiplochasaclers .
SYNTAYK
SELECT d c2
-Rubesots a Simg FROM tla
WHERC ci LiKEPATTEEN
LIKE PATTERNS
a Stosdt with a -S% in 2a e 8
J a End wth a a-%_Stast udth.ak b n z 2
or% Hone o in awj peSw al,D s t o t h a ne witho
M
AEWILDCARD
single hahaden withim the baaekats
CI L Raebets any
Ral s a h&isbanot_inthe Siackets
Repiesodo a range dosadbos
Cbsp stasttmg i h bns ip [a-c]% staoztig wi ablc
hoct t,kbe at ot fetond Rat
alleuwb t spt madtiple saluus in WHERE olaws
IN
Shedtwom
SELECT wvn-nom e SELeCT ol-nmel9
FROMt-1-
FROM ale-wom wHERE Cel nome tN
wHERE epmnnam IN dv2- SELECT STATEMEN
BETWEEN Tt solects ealuss utbi a qun uaongpe
NOT BETWEEN akblegts
6ELECT col-nom
FROM tallbl-1
WHERE C-nome BETWEEN vi and ;
SELF JoIN
A cal ToIN i a uola jeiun., bud the tadle is, joini
wth to 6ELekT ol1
FROM able-t1,tablet 2
wHERE conditioni
CROSS JoIN
T doRimad os a witeo phedudt o the tne talsos
he
imelnda oim-centaio Somlne eus
o in the
oub 2 talolas
roeedupt p e
DoS blw GRoUP BY & DISTINCT
Both distbinet and GRovP By alhces the ne'o lutnad
ANe weGRoUP BY hn we wamt to apblyan agggale func
GROUP BY
toupb
0
sws thal hae the somu,wallb ine Sumymauy
swb JkRL tUstomeb in each couuma
ORu used u t h 0gate funetins.
EA SeLECT eldnt
FRoM t-1 A
Chiuenotbg4 wHERE Conel
BY -vane
Somihiue. GRoUP
ORDER BY el_name
HAVING
HAVINa dausa thas oddoo to SQL as the wHERE anse
Culom! t be used utth agpugati fucions
E SELECT COUNT( c3),Cotaf
FROM
ORoUP BY
cou
co
HAVING coUNTCCAy 75
75
ORDER 8Y coUNTCCJA) DESC
exiST5
Tost f the andstnnce d Om socehd im aSubguehy
otuwe tue suls quey uiunb ove e. moL saiords
SELECT
FROM S h
wHERE ExiTS (seLECT PreduztKAame FRoM
Paeouats WHERE P: SJel=
=S-SJAL AND Padce2c)
ANY
PNY ePate gusns toue any e the susayusuy ouas
mab th tomd in BRSe subeins "lstSadtishmg csdh
SeLecT Patrann
FRoM Praouucts
wHERE fhotI b ANY SELECT Pdad T
FROM elthDetais wMERE -dte
AL UeAgg th sUmvlah yust ALL pact dANY
RduvS tue all Suluties Satisy, ordn
SELECT INTO Copiob lata fsom onl talsa to antals
b done in Sowme R e n t doalsose
DiEE Å ue ube IN t shlciA daalns
SELECT*INTO Neutabl Nome IN Ocpamal dlb
FROM old tale
wHERE Condn
INSERT INTD SELECX aneheh tab
d o t a faem. ene talsko am intedte t
Solectid Collmm&..
INSERT INTO talS% 2 INSERT INTO 2 d , 2 , - )
SELECT * FRoM talbo1 SELECT l , 2, 3-
wHERE ondn} FRoM t
wMERE condn
EXEC sohdALLateh
-
vu V n
CREATE DATABASE te Guco DG
CREATE DATABASE testDb
DRDP DATABASE Diob nistung SAL datasose
an
detalsase tahu0
ohatvens ocevsima, data uam a maitan
te coet of oddioral wites and mamory to
eata stwctue y uara
Swr
inclones ima bssk.
I Bha ibne ndsn to alp the n, the Vy
hacks ave ow im the tuia tam bsginmigto
a Talle Scan bod joa panRvmancz , Ind,e UnIare yy
outononiy
KELATIONSHIPSIN SG
O o te One - o m b e doimeo ob h e ulationship blw 2 talas
VIEW
A Rwin SQL visdtual talble basid en he a,H
a