View
223
Download
0
Category
Preview:
Citation preview
8/18/2019 SQL 2 - Gettting Data Into a DB
1/23
CIS 360:
Business Database Concepts:SQL – Putting Information Into aDatabase
Yili (Kevin) Hong
hong@asu.eduhttp://my.wpcarey.asu.edu/directory/peope/pro!e.c"m#
mailto:hong@asu.edumailto:hong@asu.edu
8/18/2019 SQL 2 - Gettting Data Into a DB
2/23
*ur reationa database
•
+ series o" tabes• ,in-ed together through
primary/"oreign -ey reationships
8/18/2019 SQL 2 - Gettting Data Into a DB
3/23
o create a database•
e need to de!ne – he tabes
– he !eds coumns1 within those tabes
– he data types o" those !eds
• here are S2, commands that do each o" thosethings
•
So ets assume that our database didnt e4istand we needed to create the tabes
C5+ 7D++B+S8SC9+; * ?ISS
databaseAname
8/18/2019 SQL 2 - Gettting Data Into a DB
4/23
C5+ statement create atabe1
C5+ +B, schemaAname.tabeAname coumn>ame' datatype ,,* >,,coumn>ame% datatype ,,* >,,
5I+5E FE Fey>ame1 1GItem Description
schemaAname he schema that wi contain the tabe
tabeAname he name o" the tabe
coumn>ame he name o" the !ed
datatype he datatype o" the !ed
,,*>,,
hether the !ed can be empty i.e. nu1he ame he name o" the !ed that wi serHe as theprimary -ey
8/18/2019 SQL 2 - Gettting Data Into a DB
5/23
4ampe: Creating the Customer abe
C5+ +B, orderdb.Customer
CustomerID I> >* >,,
=irst>ame +5C9+5(&1 >,,
,ast>ame +5C9+5(&1 >,, City +5C9+5(&1 >,,
State +5C9+5%1 >,,
Jip +5C9+5'01 >,, 5I+5E FE CustomerID1 1G
CustomerCustomerID
=irst>ame
,ast>ame
CityState
Jip
Based on this SQL statement:• he ony reKuired !ed is CustomerID L the rest can be e"t
ban-.• CustomerID is de!ned as the primary -ey.
5emember yourschema wi use your
m x yS2, IDi.e.mMMMorderdb.Customer
1
8/18/2019 SQL 2 - Gettting Data Into a DB
6/23
,oo-ing at the NnewO Customertabe
Column
name
Data type
CustomerID I>
=irst>ame +5C9+5(&1
,ast>ame +5C9+5(&1
City +5C9+5(&1
State +5C9+5%1
Jip +5C9+5'01 C u
s t o m e r
he databasemanagement systemstores this in"ormation
about the tabeIts separate "rom thedata in the tabe i.e.Customer in"ormation1
his is caedmetadata L Ndataabout dataO
8/18/2019 SQL 2 - Gettting Data Into a DB
7/23
Data types• ach !ed can contain diPerent types o" data
• hat must be speci!ed when the tabe iscreated
• here are many data typesG were ony goingto coHer the most important onesData type Description 4ampes
I> Integer 3 Q'0
DCI+,nm1 Decima n $ tota digitsm$ digits to the right o" thedecima point1
3.%3 3%1 3.'('&M 6&1
+5C9+5n1 String numbers andetters1
9eo I i-e piRRa yS2,
D+I Date/ime or Tust date1 %0''Q0MQ0' 'U:3&:00%0''Q0(Q'%
B**,+> Booean Haue 0 or 'So why do you thin- we de!ned NJipO as a
+5C9+51 instead o" an I>#
8/18/2019 SQL 2 - Gettting Data Into a DB
8/23
So bac- to our C5+statement
C5+ +B, orderdb.Customer
CustomerID I> >* >,, Firstame !"#CH"#($%) &LL '
,ast>ame +5C9+5(&1 >,, City +5C9+5(&1 >,,
State !"#CH"#() &LL '
Jip +5C9+5'01 >,, 5I+5E FE CustomerID1 1G
=irst>ame can be astring o" up to (&
etters and numbers.
hy $%* +t,s theSQL de-ault.
State can be a string
o" up to % etters andnumbers
8/18/2019 SQL 2 - Gettting Data Into a DB
9/23
statements
C5+ +B, orderdb.V*rderV
*rder>umber I> >* >,, *rderDate D+I >,,
CustomerID I> >,,
5I+5E FE *rder>umber1 1G
C5+ +B, orderdb.roduct
roductID I> >* >,,
roduct>ame +5C9+5(&1 >,,
rice DCI+,&%1 >,,
5I+5E FE roductID1 1G
/rder*rder>umber
*rderDate
CustomerID
0rodu1t
roductID
roduct>ame
rice
DCI+,& %1 indicates price can no arger thanMMM.MM.
8/18/2019 SQL 2 - Gettting Data Into a DB
10/23
5emoHing tabes
D5* +B,schemaAname.tabeAnameG
4ampe: D5* +B,orderdb.CustomerG
• his deetes the entire tabe and
a data• Its a pain to get it bac- i" you can
at a1
Becare"u
Ch i b
8/18/2019 SQL 2 - Gettting Data Into a DB
11/23
Changing a tabesmetadata
+,5 +B, schemaAname.tabeAname"DD coumnAname datatype ,,*>,,G
or
+,5 +B, schemaAname.tabeAnameD#/0 C/L&2 coumnAnameG
or
+,5 +B, schemaAname.tabeAnameCH"34 C/L&2 odAcoumnAnamenewAcoumnAname datatype,,* >,,G
+dds acoumn tothe tabe
5emoHes acoumn"rom the
tabe
Changes acoumn inthe tabe
8/18/2019 SQL 2 - Gettting Data Into a DB
12/23
+n e4ampe o" each
+,5 +B,orderdb.roduct+DD C*,>Vanu"acturerV+5C9+5(&1 >,,G
+,5 +B,orderdb.roduct D5*
C*,> Vanu"acturerG
+ddsWanu"acturer
coumn to
roduct tabe
5emoHes
Wanu"acturercoumn "romroduct tabe
8/18/2019 SQL 2 - Gettting Data Into a DB
13/23
+n e4ampe o" each
+,5 +B,orderdb.roduct C9+>XC*,> rice Saesrice
DCI+,6%1 >,,
+,5 +B,orderdb.roduct C9+>XC*,> rice riceDCI+,6%1 >,,
Changes name o"rice coumn inroduct tabe to
Saesrice and its
data type toDCI+,6%1
Changes data type o"rice coumn inroduct tabe to
DCI+,6%1 buteaHes the name
unchanged.
8/18/2019 SQL 2 - Gettting Data Into a DB
14/23
+dding a ro5 to a tabeHersus coumns1
• + change in the tabestructure
• Done using +,5 +B,
+ddinga
coumn
• + change in the tabe
data• Done using I>S5
I>*
+ddinga row
8/18/2019 SQL 2 - Gettting Data Into a DB
15/23
8/18/2019 SQL 2 - Gettting Data Into a DB
16/23
I>S5 e4ampe
I>S5 I>* orderdb.CustomerCustomerID =irst>ame ,ast>ameCity State Jip1 +,S '00& YChrisYYaubY YrincetonY Y>ZY Y0M'%0Y1G
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
'003 Zames ison ittsgroHe >Z 0M'%'
'00( ric =oreman arminster + 'M'''
;;% Chris 6au< 0rin1eton = ;>;
B+3 6+0: ote that 7eld names are surrounded
8/18/2019 SQL 2 - Gettting Data Into a DB
17/23
Changing a row
D+ schemaAname.tabeAname Scoumn>ame'$Haue'coumn>ame%$Haue% 95 conditionG
•
D+ VtestV.VproductV SVroduct>ameV$Y9oney >ut CheeriosYVriceV$Y(.&0Y 95 VroductIDV$Y%%&'YG
Item Description
schemaAname he schema that contains the tabe
tabeAname he name o" the tabe
coumn>ame he name o" the !ed
Haue he data Haue "or the !ed
condition + conditiona statement to speci"y the recordswhich shoud be changed
8/18/2019 SQL 2 - Gettting Data Into a DB
18/23
D+ e4ampe
D+ orderdb.roduct Sroduct>ame$Y9oney >ut CheeriosYrice$(.&0 95 roductID$%%&'G
0rodu1t+D
0rodu1tame
0ri1e
%%&' Cheerios 3.MM
%%)% Bananas '.%M
%&0& ggo a[es %.MM 0 r o d u 1 t
0rodu1t+D
0rodu1tame
0ri1e
% Honey utCheerios
$.%;
%%)% Bananas '.%M
%&0& ggo a[es %.MM
6he ?sa-est 5ay to &0D"64 is onere1ord at a time'
8/18/2019 SQL 2 - Gettting Data Into a DB
19/23
Changing mutipe rows
D+ orderdb.Customer SCity$YCherry 9iY 95State$Y>ZYG
Customer+D
Firstame
Lastame
City State
9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
'003 Zames ison ittsgroHe >Z 0M'%'
'00( ric =oreman arminster
+ 'M'''
Customer+D
Firstame
Lastame
City State
9ip
;; 3reg House CherryHill
= ;>;
;; Lisa Cuddy CherryHill
= ;>E
;;E =ames ilson CherryHill
= ;>
Be1are-ul8
Eou can do
a ot o"damagewith a
Kuery i-ethis
8/18/2019 SQL 2 - Gettting Data Into a DB
20/23
8/18/2019 SQL 2 - Gettting Data Into a DB
21/23
D, e4ampe
D, =5* orderdb.Customer95 CustomerID$'00(G
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
'003 Zames ison ittsgroHe >Z 0M'%'
'00( ric =oreman arminster + 'M'''
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
'003 Zames ison ittsgroHe >Z 0M'%'
8/18/2019 SQL 2 - Gettting Data Into a DB
22/23
Deeting mutipe rows
D, =5*orderdb.Customer 95CustomerID\'00%G
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
'003 Zames ison ittsgroHe >Z 0M'%'
'00( ric =oreman arminster + 'M'''
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
+ H o i
d
d o i n
g
t h i s S
8/18/2019 SQL 2 - Gettting Data Into a DB
23/23
*ne more D, e4ampe
D, =5* orderdb.Customer95 State$Y>ZY +>D Jip$Y0M'%'Y
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0'00% ,isa Cuddy ainsboro >Z 0M'%3
'003 Zames ison ittsgroHe >Z 0M'%'
'00( ric =oreman arminster + 'M'''
Customer+D Firstame Lastame City State 9ip
'00' Xreg 9ouse rinceton >Z 0M'%0
'00% ,isa Cuddy ainsboro >Z 0M'%3
'00( ric =oreman arminster + 'M'''
Recommended