Professional Documents
Culture Documents
Create Database
Create Tables
Queries
Joins
Drop Table
Drop database
September 10, 2013
Create Database
CREATE DATAASE my!db"
#or e$ample%
CREATE DATAASE SC&E'A"
September 10, 2013
CREATE TA(E table!name
)
column!name* data!type)si+e,-
column!name. data!type)si+e,-
column!name/ data!type)si+e,-
,"
#or e$ample%
CREATE TA(E E'0(12EE
)
Emp!Id I3TE4ER)/5,-
#irst!name 6ARC&AR)78,-
Age I3TE4ER)*8,-
,"
Create Table
September 10, 2013
SQL Not Null Constrants
CREATE TA(E table!name
)
column!name* data!type)si+e, constraint!name-
column!name. data!type)si+e, constraint!name-
column!name/ data!type)si+e, constraint!name-
,"
CREATE TA(E 0ersons
)
0!Id int 31T 39((-
#irst3ame :archar).88, 31T 39((-
Address :archar).88,-
City :archar).88,
,"
September 10, 2013
SQL !N"Q!# Constrant
CREATE TA(E 0ersons9ni;ue
)
0!Id int 31T 39(( 93IQ9E-
(ast3ame :archar).88, 31T 39((-
#irst3ame :archar).88,-
Address :archar).88,-
City :archar).88,
,
"nsert Quer$
September 10, 2013
About "nsert Quer$
1btained Result%
id!register Email 0assword
/ gariyashee<duttaAyahoo<com
3o:ember*CC.
September 10, 2013
Q/,=hat is the ;uery to search at table user!register in undefined orderD)using
>ey,
A/, SEL C *F OMusu_rMgu eru resu_rMgu e
G ur_rnal DagabaM
G resu_rMgu
G 1 FAK!AF"#$_%arl&'%
G ( _eM)r&*_aur$_%arl&'%
G _aur*aMreOgga$*ar''&'%
G
G
bgarne *MOlg+
G id!register Email 0assword
G / gariyashee<duttaAyahoo<com 3o:ember*CC.
Delete records
Queres
September 10, 2013
Queres
Queries allow the user to describe desired data- lea:ing the database
management system )D'S, responsible for planning- optimi+ing- and
performing the physical operations necessary to produce that result as it
chooses<
September 10, 2013
Queres'
I3SERT
SE(ECT
SE(ECT DISTI3CT
90DATE
A(TER
93I13
DE(ETE
TR93CATE
September 10, 2013
"NS#&T'
The I3SERT I3T1 statement is used to insert new records in a table<
I3SERT I3T1 table!name
6A(9ES ):alue*-:alue.-:alue/-<<<,"
)1R,
I3SERT I3T1 table!name )column*-column.-column/-<<<,
6A(9ES ):alue*-:alue.-:alue/-<<<,"
September 10, 2013
S#L#CT'
The SE(ECT statement is used to select data from a database<
SE(ECT E #R1' table!name"
)1R,
SE(ECT column!name-column!name
#R1' table!name"
September 10, 2013
S#L#CT D"ST"NCT'
=hat if we only want to delete the data inside the table- and not the
table itselfD Then- use the TR93CATE TA(E statement%
A sub ;uery is used to return data that will be used in the main
;uery as a condition to further restrict the data to be retrie:ed<
Correlated Subquery:
If the outcome of a sub;uery is depends on the :alue of a column of its
parent ;uery table then the Sub ;uery is called Correlated Sub;uery<
September 10, 2013
SQL Sub Quer$ * #-ample 3
To find all the orders that are made from outside of I3DIA%
SE(ECT
1rderid-
customerid-
shipname
#R1'
orders
=&ERE
customerid 31T I3
)SE(ECT
customerid
#R1'
customers
=&ERE
country F NI3DIA?,
September 10, 2013
SQL Nested Quer$ * #-ample 2
The following ;uery selects top fi:e customers by sales
SE(ECT
companyname-
city-
)SE(ECT
S9')unitprice E ;uantity,
#R1'
orders
I33ER J1I3
orderdetails 13 orderdetails<orderid F orders<orderid
=&ERE
orders<customerid F customers<customerid, AS total
#R1'
customers
1RDER 2 total DESC
(I'IT 8"
September 10, 2013
Ad/antages and Dsad/antages
Advantages of using subquery:
Sub;ueries is a comple$ ;ueries where a comple$ ;uery can be
bro>en down into a series of logical steps for easy understanding and
code maintenance<
Sub;ueries allow you to use the results of another ;uery in the outer
;uery<
Disadvantages of using subquery
=hen sub;uery is used- the database ser:er )actually the ;uery
optimi+er, may need to perform additional steps- such as sorting-
before the results from the sub;uery are used< If a ;uery that contains
sub;ueries can be rewritten as a Roin- you should use Roin rather than
sub;ueries< This is because using Roin typically allows the ;uery
optimi+er to retrie:e data in the most efficient way<
September 10, 2013
&e0erences
http%SSwww<tutorialspoint<comSs;lSs;lHsubH;ueries<htm
http%SSwww<+entut<comSs;lHtutorialSs;lHsub;ueryS
http%SSmsdn<microsoft<comSenHusSlibrarySofficeSaa.*TUM5):Foffice<**,<asp$
1ons "n DBMS
September 10, 2013
T$pes o0 1ons
Inner Join
1uter Join
Self Join
September 10, 2013
"nner 1on
Inner Roin creates a new result table by combining column :alues of two
tables )A and , based upon the RoinHpredicate<
September 10, 2013
S$nta-
E$ample
E;uiHJoin
3atural Join
September 10, 2013
#2u 1on
E$ample
E$ample
The result of a left outer Roin )or simply left Roin, for tables A and
always contains all records of the WleftW table )A,- e:en if the RoinHcondition
does not find any matching record in the WrightW table ),<
E$ample
A right outer Roin )or right Roin, closely resembles a left outer Roin- e$cept
with the treatment of the tables re:ersed< E:ery row from the WrightW table
), will appear in the Roined table at least once< If no matching row from
the WleftW table )A, e$ists- 39(( will appear in columns from A for those
records that ha:e no match in <
E$ample
A full outer Roin combines the effect of applying both left and right outer
Roins< =here records in the #9(( 19TER J1I3ed tables do not match-
the result set will ha:e 39(( :alues for e:ery column of the table that
lac>s a matching row< #or those records that do match- a single row will
be produced in the result set <
E$ample