You are on page 1of 56

DBMS Concepts

September 10, 2013


Agenda

Create Database

Create Tables

Insert new records

Search records satisfying some conditions

Delete records satisfying some conditions

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$

Insert ;uery is used to insert or add data into database<

9sing Insert ;uery we can add single or multiple :alues<

=hile adding such data we ha:e to worry about >ey?s<


September 10, 2013
!sng t%e "NS#&T Command

asic synta$ for inserting data into e:ery column%


I3SERT I3T1 tablename
6A(9ES )column1_value- column2_value- @ ,"
e<g insert into student :alues)*-?Ram?-?chennai?,"
insert into student :alues).-?shyam?-null,"

asic synta$ for inserting into selected columns


I3SERT I3T1 tablename )columnname1- columnname2- @,
6A(9ES )column1_value- column2_value- @ ,"
e<g insert into student )id-name, :alues)*-?Ram?,"
September 10, 2013
"nsert Quer$
*< Insertion of single row in table
I3SERT I3T1 user!details 6A(9ES )*-?Ram?-?ramAgmail<com?-?0une?,"
I3SERT I3T1 user!details 6A(9ES )8-?Ram?-?ramAgmail<com?-null,"
.<Insertion of multiple rows using single insert ;uery in table
I3SERT I3T1 user!details 6A(9ES ).-?Shyam?-?smAgmail<com
?-?'umbai?,- )/-?Chaitanya?-
?cmAgmail<com?-?3agpur?,"
Searc% records
September 10, 2013
Table Creaton
Q.,3ow-populate the table user!register with a few rows%
A.,
I3SERT I3T1 user!register 6A(9ES )?'AB<SA'58Agmail<com?- ?'u>esh?,"
I3SERT I3T1 user!register 6A(9ES )?gdspicygariAgmail<com?-?gari8?,"
I3SERT I3T1 user!register 6A(9ES )?gariyashee<duttaAyahoo<com?-?3o:ember*CC.?,"
3 o : e m b e r * C C . g a r i y a s h e e < d u t t a A y a h o o < c o m /
g a r i 8 g d s p i c y g a r i A g m a i l < c o m .
' u > e s h ' A B < S A ' 5 8 A g m a i l < c o m *
0 a s s w o r d E m a i l i d ! r e g i s t e r
September 10, 2013
Q/,=hat is the ;uery to search at table user!register in undefined orderD
)using >ey,
A/,SE(ECT E #R1' user!register where id!register F / "
1riginal Database
id!register Email 0assword
* 'AB<SA'58Agmail<com 'u>esh
. gdspicygariAgmail<com gari8
/ gariyashee<duttaAyahoo<com 3o:ember*CC.

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

SQ( means Structured Query (anguage<

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'

In a table- a column may contain many duplicate :alues" and sometimes


you only want to list the different )distinct, :alues<

The DISTI3CT >eyword can be used to return only distinct )different,


:alues<

SE(ECT DISTI3CT column!name-column!name


#R1' table!name"

September 10, 2013


!(DAT#'

The 90DATE statement is used to update e$isting records in a table<


90DATE table!name
SET column*F :alue*-column. F :alue.-<<<
=&ERE some!column F some!:alue"
September 10, 2013
ALT#&'

The A(TER TA(E statement is used to add- delete- or modify columns


in an e$isting table<

To add a column in a table- use the following synta$%


A(TER TA(E table!name
ADD column!name datatype

To delete a column in a table-use the following synta$ %


A(TER TA(E table!name
DR10 C1(9'3 column!name
September 10, 2013
!N")N'

The 93I13 operator is used to combine the resultHset of two or more


SE(ECT statements<

SE(ECT column!name)s, #R1' table*


93I13
SE(ECT column!name)s, #R1' table."

SE(ECT column!name)s, #R1' table*


93I13 A((
SE(ECT column!name)s, #R1' table."
September 10, 2013
D#L#T#'

The DE(ETE statement is used to delete rows in a table<

DE(ETE #R1' table!name


=&ERE some!columnFsome!:alue"
September 10, 2013
T&!NCAT#'

=hat if we only want to delete the data inside the table- and not the
table itselfD Then- use the TR93CATE TA(E statement%

TR93CATE TA(E table!name



Nested Queres and
SubQueres
September 10, 2013
SQL Sub Quer$ * +%at s t,

A sub ;uery is a ;uery within another SQ( ;uery and embedded


within the =&ERE clause<

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<

Sub ;ueries can be used with the SE(ECT- I3SERT- 90DATE-


and DE(ETE statements along with the operators li>e F- I- J-
JF- IF- I3- ET=EE3 etc<
September 10, 2013
SQL Sub Quer$ * #-ample 1
Consider the customer table from the shopping cart application%
KHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHK
LCust!ID L 3ameL AddressL StateL CityL
LHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHK
L * L Ram L 3 0ar> L 3 End L 3CityL
L . L heem L S 0ar> L S End L Scity L
L / L &ari L 3E 0ar> L 3E End L 3city L
L 7 L Shi: L = 0ar> L = End L =city L
+----------------------------------------------------------------------------+
September 10, 2013
SQL Sub Quer$ * #-ample 1
Contnued..
Consider the 9ser table from the shopping cart application%
KHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHK
LCust!ID L 9ser 3ame L0asswd L no!orders
LHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHK
L * L ram!* L EEEEE L 7
L . L bheem!* L EEEEEEE L M
L / L hari!* L EEEEEEEEEE L *.
+------------------------------------------------------------------------------------+
September 10, 2013
SQL Sub Quer$ * #-ample 1
Contnued..
3ow- if we want all the users who are customers from a particular city<
=e could write a sub;uery li>e this%
SE SE(ECT E #R1' users =&ERE cust!id I3 HH outer ;uery
)SE(ECT cust!id #R1' customers =&ERE city F N3city?," HH inner
;uery
KHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHK
LCust!ID L 9ser 3ame L 0asswd L
LHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHK
L * L ram!* L EEEEE L
L / L hari!* L EEEEEEEEEE L
+----------------------------------------------------------------------+

September 10, 2013
SQL Sub Quer$ * #-ample 2
September 10, 2013
SQL Sub Quer$ * #-ample 2
To find all products that ha:e been ordered by a customer- you could use this
;uery%
SE(ECT 0roduct3ame #R1' 0roducts =&ERE EOISTS )SE(ECT
1rders<1rderID #R1' 1rders I33ER J1I3 P1rder DetailsQ 13
1rders<1rderID F P1rder DetailsQ<1rderID =&ERE 0roducts<0roductID F
P1rder DetailsQ<0roductID A3D CustomerID F ?A(#BI?,
September 10, 2013
SQL Nested Quer$ * +%at s t,

If a Sub;uery contains another sub;uery- then the sub;uery inside


another sub;uery is called nested sub;uery<

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

An ?inner Roin? is a commonly used Roin operation used in applications< It


can only be safely used in a database that enforces referential integrity
or where the Roin fields are guaranteed to not be 39((<

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-

SE(ECT E #R1' Tablename I33ER J1I3 department 13


Tablename*<Attribute F Tablename.<Attribute"

E$ample

SE(ECT E #R1' employee I33ER J1I3 department 13


employee<DepartmentID F department<DepartmentID"
September 10, 2013
T$pes o0 "nner 1on

E;uiHJoin

3atural Join
September 10, 2013
#2u 1on

An e;uiHRoin is a specific type of comparatorHbased Roin- that uses only


e;uality comparisons in the RoinHpredicate< 9sing other comparison
operators )such as I, dis;ualifies a Roin as an e;uiHRoin<

E$ample

SE(ECT E #R1' employee J1I3 department 13


employee<DepartmentID F department<DepartmentID"
September 10, 2013
Natural 1on

A natural Roin is a type of e;uiHRoin where the Roin predicate arises


implicitly by comparing all columns in both tables that ha:e the same
columnHnames in the Roined tables< The resulting Roined table contains
only one column for each pair of e;ually named columns<

E$ample

SE(ECT E #R1' employee 3AT9RA( J1I3 department"


September 10, 2013
)uter 1on
An outer Roin does not re;uire each record in the two Roined tables to
ha:e a matching record< The Roined table retains each recordVe:en if no
other matching record e$ists< 1uter Roins subdi:ide further into left outer
Roins- right outer Roins- and full outer Roins- depending on which table?s
rows are retained )left- right- or both,<
September 10, 2013
T$pes o0 )uter 1ons

(eft 1uter Roin

Right 1uter Join

#ull 1uter Join


September 10, 2013
Le0t )uter 1on

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

SE(ECT E #R1' employee (E#T 19TER J1I3 department 13


employee<DepartmentID F department<DepartmentID"
September 10, 2013
&g%t )uter 1on

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

SE(ECT E #R1' employee RI4&T 19TER J1I3 department 13


employee<DepartmentID F department<DepartmentID"
September 10, 2013
3ull )uter 1on

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

SE(ECT E #R1' employee #9(( 19TER J1I3 department 13


employee<DepartmentID F department<DepartmentID"
September 10, 2013
Sel0 1on
A selfHRoin is Roining a table to itself<
Drop Tables 4 Database
September 10, 2013
Drop Table
DR10 TA(E command remo:es one or more tables from an e$isting
database
The user who is using the DR10 command- must ha:e DR10 pri:ilege
for each table )s,he wants to delete
The command remo:es all the data and table definition from the
database
September 10, 2013
S$nta-
DR10 TA(E table3ame"
September 10, 2013
Drop Database

DR10 DATAASE statement is used to drop an e$isting database in


SQ( schema

Drop pri:ilege is re;uired to use the drop command

The command remo:es the database and all its information


September 10, 2013
S$nta-
DR10 DATAASE Database3ame"

You might also like