0 ratings0% found this document useful (0 votes) 30 views33 pagesMaster PHP and MySQL Material 5th Unit New
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
y use SQL
rned how to design a database, you’
des gn. TO do that you use the set of SQL sat to learn how to
data definition language (DDL). In this chapter ‘atements that are
statements that work with tables and inde; , You'll learn how
stator atements that work with users and prog you las learn
SQL workbench to design your databace, Heges.
«that contains the DDL statements needed to eos a enerate
can run that script to create the database. However, it’s z sitabane.
to understand DDL statements for several reasons. Fir. : till
F ant L statements helps you understand how to use M Re , under-
nd you May need to edit the DDL statements in os aie Wor
sey. SoCo may need to write DDL statements on your owa t motify
script. age th t already exists.
sc
How to work with databases
How to create a database
How to select a database...
How to drop a database
How to work with tables
‘An introduction to MySQL data'types ..
How to create a table
How to code a primary key
How to alter a table.
How to drop a table ..
How to work with indexes .
How to create an index
How to drop an index ..
How to work with users and privileges
‘A summary of privileges ..
How to create, rename, and drop users
How to grant privileges
How to revoke privileges «.
How to view privileges ..
Other skills for creating a database -
How to load data from text files ...
566sj ac ctin of a natear
cette vs a
hemo nec tare, M8 wa ARIS
gave nants
00 on wee,
Secret int
eee amet A TARO ora am
Seneca oe ch le asa So em
ene een nese 1 we BBL geen wee
“oe
database
esto aca
to creat in
vay £uitar_shop2. If a databas ‘BASE,
Se already thn, fiption
ee
pee iy
tee el sana and dose ect ga .
Se a sO the execution of the eet ne Mo the USE StAlEMent to Selecta datibae NN.
s te ye IF NOT EXISTS Keyworth Yous he DROP DATABASE so
fs a SEDO opts,
nis shown in the second examy
re docs’ taleady exist nH he
sxe this YOU
sean 0 pase ae
CcREaTE Dice ihe
aly
a never dr0p 2 production datas
parr you won't iho
wres, howe ¥ have the pvege doa aaa
Ge gre 0)
tabase
How to select a dat
ne USE statement to select the dtabare
ind exo
‘he ind uals oe although the USE statement int a bat wa
eran common hen ai
“
sQh dass
How to drop a database
“plo ange wsthe DROP DATABASE statement od
et eran shop? that Was created in the Fist camper
tate tine nthe ee database, 50 use it wi xan
Ses ve dost exis, this statement generates an error that a
srl ph you cao Be IF EXISTS
souninte fit example, Then, the statement onyy use SQL
rned how to design a database, you’
des gn. TO do that you use the set of SQL sat to learn how to
data definition language (DDL). In this chapter ‘atements that are
statements that work with tables and inde; , You'll learn how
stator atements that work with users and prog you las learn
SQL workbench to design your databace, Heges.
«that contains the DDL statements needed to eos a enerate
can run that script to create the database. However, it’s z sitabane.
to understand DDL statements for several reasons. Fir. : till
F ant L statements helps you understand how to use M Re , under-
nd you May need to edit the DDL statements in os aie Wor
sey. SoCo may need to write DDL statements on your owa t motify
script. age th t already exists.
sc
How to work with databases
How to create a database
How to select a database...
How to drop a database
How to work with tables
‘An introduction to MySQL data'types ..
How to create a table
How to code a primary key
How to alter a table.
How to drop a table ..
How to work with indexes .
How to create an index
How to drop an index ..
How to work with users and privileges
‘A summary of privileges ..
How to create, rename, and drop users
How to grant privileges
How to revoke privileges «.
How to view privileges ..
Other skills for creating a database -
How to load data from text files ...
566sj ac ctin of a natear
cette vs a
hemo nec tare, M8 wa ARIS
gave nants
00 on wee,
Secret int
eee amet A TARO ora am
Seneca oe ch le asa So em
ene een nese 1 we BBL geen wee
“oe
database
esto aca
to creat in
vay £uitar_shop2. If a databas ‘BASE,
Se already thn, fiption
ee
pee iy
tee el sana and dose ect ga .
Se a sO the execution of the eet ne Mo the USE StAlEMent to Selecta datibae NN.
s te ye IF NOT EXISTS Keyworth Yous he DROP DATABASE so
fs a SEDO opts,
nis shown in the second examy
re docs’ taleady exist nH he
sxe this YOU
sean 0 pase ae
CcREaTE Dice ihe
aly
a never dr0p 2 production datas
parr you won't iho
wres, howe ¥ have the pvege doa aaa
Ge gre 0)
tabase
How to select a dat
ne USE statement to select the dtabare
ind exo
‘he ind uals oe although the USE statement int a bat wa
eran common hen ai
“
sQh dass
How to drop a database
“plo ange wsthe DROP DATABASE statement od
et eran shop? that Was created in the Fist camper
tate tine nthe ee database, 50 use it wi xan
Ses ve dost exis, this statement generates an error that a
srl ph you cao Be IF EXISTS
souninte fit example, Then, the statement onyree
o rk wit
i row? ope
1 it
or eens 3 stench
th tables.
ist pti oe Sc
= mySat data types oe plume ess
peat be mon common MYSQL. dats ott”) Cadman eta ne ewok
aston in Fo tee BFOUPS: mei, sing Mt Th = en — 2
i pe 27 pe ft 0 a fr tring gc
(eee at eS acta he TINYINT types
pas VIN BRS) pe ony takes hee aug gee
ssn gese YN pr VIN yaks Oe nic laga een sae
your ase WPS “will only store integer value i fcr The Sine Bune ae
rg ap ES wey renege vate bce = mu
Seow ee Ha cis ie
ml aroma TINYINT type (0 Save storage space, p12) ‘uct lngh cancen Wer tem ca
ak mal an aloes Cn Yo cn ea ‘ahaa ieee Te
Pena guess 0021 When YOU dena eg ‘bleh en PO edi
et orgy Spe thas ed nd ee,
ST be ih hated nye ed
SpEteeNL SO ml do srig mont oo
case =
2 nents he ttl Mabe of ig Seen ina) OOD Di |
opens DENA, nits he nb of decimal pt eat fr tat a ea.
Pisin e008 es lpn a “erie ge 48329 DL Na
Cates te a pc fit wo a wed or toring shor ene may 00 tah ea 3.9.
oth ie Es ands nS, which on sould you wey coarser Oe gat fr dpa ad ay ama
cash sae ora colo wl stall have the Same Dumber
‘ie ect ype cae eas eg
Seven eta clu wil wal te ‘on
sires Honest eth VARCHAR ype Beatie rege ese pS ke an cena tse
‘ess seg for ths type of data. q Whe with 1 byte Per ae a
a Gia or VARCHAR the size refers tothe mint ea garter set rvs or Sa i
na ee tera ye By default, MySQL uses the Latin {te Uren)
“arue sets designed for storing Fae eee ype is ypiealy se fo ith args an we et
SSSiserstac tye per character. As ares tks CHAR date YP rage regres fe alge eSB TI
‘curses. However, if you need to store characters from, same ami shorter tan the speed Be na
sean snd ory cate the Unicode carcerat ine a ee A i
Sem auererrmracee vasa “a sete
shes tenet ae « Fe vARCHAR dt 3 0 est
aegis ee fr storing xtemely Long string values S025 pe was ony the moun’ SOE aesetoal arb HESS
CARER Spe oe Oss. the TEXT type works Hike he «Te precision of a DECIMAL ail Beil as
Siemans o's eemstimam numberof characters Himited oly
lon ouster ht an be stored in a tow, By default is
SQL wcnahgacas ==. bu this may vary depending on
sign forthe
Seine a pe. Th ph into
Stored tothe right oft
Fae 172 pa roucon @ SOL 229son tg i pe BATT pe genie
ei i se estes TMM hy
sete ses tne 3 3 Ph
ee MyBO eae se
i oe Os ene ae,
“Sora oa mh pene tm
a aon Tree
1 ou
= many of
wae
ro te et
i a ateent, Ou ean took yee
er ent Cate ANEW able in dhe
eae
Sm TABLE statement consists of
ames 20 < of its columns
i rare ir noereoea at
eed oye ae
ae pa clan does xe
the name ot
a ce aetdae Be
ca arto ream oe
ae ets to define a column tat can't contain
i jane he DEFAULT a
Sk yell when Oddo
eS haa stconespond he dpe er
SS tle aaa forthe paymentTotal column i et
Teor ein stl ee hat if yu run the statements shown
ins ives my asia sop? database, the statements will fll
eva ge die ead conan ables mane
atone ors ak yeu want tst these statements, you need
‘mons tds a tem against that database.
ine CREATE TABLE statement
. Inder ae roe ee ee
oe ae pate Spent
a ries
= ohare.
snes ste
ww ees a
Pome? Temasey REE
porno obs with column attributes
Fhomsso SE
Drewsastsia) bor mat
Deseription > C
4 The CREATE-TABLE statement cbs ble sed on the clu defitions
tnd column atributes you speeily
4 Sice the NOT NUL WREFUNIQUE abet lini he typeof ata that coarn
constr, they are known 3s constas:
«+ Toreview the complete syntax ofthe CREATE TABLE semen, you ci serch
Fone? Row o eat ateoy hey
oe
Se eee a ITO tp
ct or for tat clue
oR HI he hy
ay tn wg ee fs
ns dd th ol,
ae clin
inary fy HOWE
pe aceon example
poet
eve 8
i example
prodactlD column.
ey Inte thi
eid cokumn ad
i pes is to ode the PRIM AR,
Soak ATS Ne
=n a hone tthe nga KY
mp
ey, Th,
300
ines
ch,
doc provide one capability that gy
sey itcan refer 0 multiple column y
jr to lip coms, you mus we
the primary Key fr the orden,
ana eotumn-vel primary kay
sae yy tanciaass) REE REE ms
es EE aw,
ogy a tabetevel primary key.
pro aug contamers ¢
eeecat. eumags) SETS
= EF i Se,
yas va touetonsD}
i a two-column primary key
Re cere
ce ‘prema (30,2)
Sipe xB (OAS "pedbettD)
peseription
reed forthe primary ke, an 8 es bt ali NULL a
+ Yous
‘column or eolarns wii hat ein.
Gs
do 4s
&
0
bs
tion and you name is
va cofum-level primary ey ptt be din be cae
Yeon, You code a ablelvel pinay ey wikispaces
aA
nary Ay oq th cach a ie fe ects
§\
So
34
gS trJ nano aa om
ont Mann nd 22 orm hn
“Suporte shea
aa ttrnsactionbate = ee,
3 aga new SIND Sr apg
tenn ssammn eae
SASH god an exsting coo ra
ipo to eo ana
an
nnn 8 Sey ee
ent that RANGES the definition oper
font i lon otaZaoe
rear a renee et
pater
See ee
soo name axing coma In i that changes the data mA a,
sonst emailer ca “patterns cperonare wouure teetee ESS”
Fe tte abate for tea NOS se on ee EY te
eer ‘ pent thai may CAuSe dela to belay |
the detniton of. ater euagomers MOTE there
ae ape fOr that coun, tn nero SHH oan,
3 a a changed VARCHAR potent nat sets te LFA Va ot ol -
gerne ae yore ee tae ea sone en | Te
ent that drops the defaut valu ¢
Salle amon tocans th a YPS10 8 tens ant atop We 2S cn
“ions eine VARCHARGOO att =" xm orem
scrapie bomen ari :
wit ruse the ALTER TABLE steer
Chior dw eo change the datatype toa feren, Peasant th ene omen ctaig
Tee or example the fistName cola a oe eam
part 8 ares hs ma) Tes in a a
re pant this a. As esl, a name of Fore eves cher abiaes
a cng he fat ave fora cola
‘esi com dtl valve of an empty string to the frtNeme
cnt ese coon Is ofthe VARCHAR type er
sg ee era NULL va se fut vale
Met rest drop the Gfault valve from a alsa Ts
scene tsenen ne coahvalethat was en the eo
vcd mht tne
doing that. sly won't have the privileges for
Fave 17-5 Howto aleralaieOL ee
ete
oo
bl
pata
How 12 goa the DROP TABLE state
ons 0. Sly when You're working WER
onto rons atale
8
reo os
won "en stot
2 am mt a i Tao 99h Ket
bli eae, it a a i ord ie =
sis oe ee ei he et om
toca Tet er 8 crt an enor at 2/0807 DROP TABLE sen
eons tom to cone 0 ay aes MYSQL dete is dts nae
eae antes Sees “seen, sno tetas,
eg a even if other tabs 4B oe ec tig
kn cmp MS aie 3 doen aa
Ne et SSG ches wh or oer ona pve dopa al
7 aa, MySQL 00 allow you te on sl Sony on thave De ge
soe sset to de the Sooty = tex
Bere yee vee cosiomers table if a foreign 15 Me the tate, BM legs for ding ea
see nae tee) ma ope rk ri >
ee peso lcbetoe 2(2
yeep 199 ah psy indexes that have been S cowl
sas pent gre, yl 1a Row to Work wih the
sed
indasofeleto use SQL to
a a database bea
the essential SQL statem 5 ‘
hale table or from multiple tables. Then Sn gan oe
0 mary ae ies and subqueries. Finally, it shov See
E, and DELETE statements to add new ae feaauiet or
s le, to
wil
Ke uPDAT
SER isting FOWS in a table, and to remove rows from a table.
a
* of
How to select data from
How to select columns from a ee table ee
How to use an alias for a column
How to select rows with a LIMIT clause
How to select rows with a WHERE clause
How to use the logical operators
How to use the IS NULL operator
How to use the LIKE operator ..
How to sort rows with an ORDER BY clause
How to select data from multiple tables ..
How to code an inner join
‘When and how to use table
How to code summary queries ...
How to code aggregate functions
How to group queries by column
How to code subqueries
‘Where to use subqueries .
How to code correlated subqueries
How to insert, update, and delete rows
How to insert rows -
How to update rows
How to delete rows
Perspective ..-
jiasesSecon Mater strong
ont
576
How to use an alias for a column
(0rd crane came, 2uatreten price
Je
ane ta
rs tron
g : we
a oe © @
cota nich ay
raf ieee ee oa
wena tectum aan Tir the cag aan lowed yy
‘Sean noon Sada Inte CANS, pose tf Ag
easedetng ae Sim tenet
rae ec i eee am
Ena Saas ieee aca
Fe Miegoe cote an alls you must ens he isn dak ee és
lis consins a space or is a Keyword that's reserved by Mipsee (eS e_
seeping meter covet ae ae os
Sea So nee 1 et ta
‘When sou specify an ais, the result set ses he exact eapittcatog
utp Arar 3oshoold be tussle a
Mys
Yorn
ents re ace nda
{spn the ene ealclation asthe name, which ean be ened
7
pescripti column in the a
al arnt at YO Wa YOUN THY ONE
ech ns
ahi anersve
o select rows with @ LIMIT cla,
gna mae
SE ne ect tae, Sa
a yon amare tounge" Te
seh ce aS
oe ee rt oe eee
‘chown yew ees thea neat
Te cco campi a ee
oben ling rows aig
Co ee i fer ane eke in,
poses i a amg ow te eos
Le ee iy matic ot Totes a
How
poses
es an ofl of 3, Ad 300
oi ppstyAdinin automaticaly adds 9 LIMIT coos to,
sex bet ement so hey ely rete the fit 30 rows af abe Ts
‘abe Ts
Moar since it prevents you fom accidently retevng
on yon pom a database, However, f You Wat ose php
than 30 rows, you can override this def ie
Fee het arer ere ects)
Ahowands off |
ua LIMIT lose
ami clause
wet
“veo ror oe onl at
imi th ember fos tat a nln
wee clause
Oresn we LIMIT
a)How ewe SOLO wok ithadanboe ‘S89
Sent steer SSO pop
che 8
How to use the logical operat
ca BES 185 hos how ota oper WHEE
i IND and OR operators to combine wg WE an
conan mse rete, combs eft Hen : res te AND operator
‘san cnn ent nat use tuuaoercene = 20
Ft rapes there ra tne OR Operator
see re te
Eine sks est no sin
Hans meat be ent 08 we the OR operator. though DD CT ate TRU Sees a
's must be TRUE. So, the second eran ron lyon oes c anaes
seas ta ek sit
“paca ptrasein tina NOT opr
ei baanic rer
gee ic Nae ca a
er nee tances: Beane oat yu sie eee cone 500
sly dott by rewriting th WHE SoH ag at ys a
een A cece a ea on wtnout parentheses
svt fourth exam DO ne oo cone epee atacomerercet, ateaieg le
ast wo examples show bow the oder of eis os y1-01' on AustPrice © 5 « wt
argatas wos By defeat, MySQL evatuates ce the topics -
te(MBevet afte ns NOP se eB oo G
end at tg : “
icra eae os
mesentery jo
setcpaemocr se see do
Parentheses 50 the Iwo ec ea fo instan
paths ige Mt cdi some goa
I rate ting gical ope a assed
mG Ml pvc tcmtny yap : pound coneition with parenthes
‘Siena alte cc ome kt san, ‘nema compound coniion wi penne.
Description
‘+ Youcan use the AND and OR loglcal operators to create compound condition hat
‘omit of two of more conditions. You use the AND operator to specify that the
sch must satisfy both ofthe conditions, and you use the OR operator to specify
atthe earch must sty atleast one ofthe condos.
+ Youcan use the NOT operator to negate a conn.
* When MySQL evaluates a compound codon it evsluts the operators in this
Seqience: (1) NOT. 2) AND, and (3) OR. You can use parentheses to override this
(re of precedence oro elif the sequence in which the operation wil be evaluated.Coane Ih Howton thane
ene clause withthe LIKE operon
ow to ure the LIKE operator a
seca eae een
information in 2 database that can't be found lave ton,
Ityoncicodthe LIKt oper og wah cy het ecg
this figure to specify the string pattern, oF mask, that yer at *PO™N at the? Me
the first example, the LIKE phi . that You want to marth
sun cts neat eters
‘sls Here te porcent (7) sgn indicates that ny eh ry
Ire ler. This includes “Pener Precision” and teres £30 eg
s Src oe es Nae es sna
Theol xan eles al oduct ht have eee —. os
wherein the name. This includes “Fendr Sratocater"tn ta = sqeanea latest
Tho ind xampe we he decent ow ane OEE sine Forceps Tee
‘cade begins with 076 followed by ny two charsctrs, This eo Mp sors IE Mareen sean
opting aa
‘Sie wd-ter ere SNS
The foun example cet al odes that have a date nS 201, ior osee eet enter
soc cos emma ira
i one Team use spesa charts, aed wide
heen satis the conn.
wii vas in the clon -
sel epee ae
erotic
este.
+ Nessus tht
rales