0 ratings0% found this document useful (0 votes) 415 views17 pagesSQL Hand Written
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
aruc war (uNKedin)-
Nore l GAUERY (TELEGRAM).
What is $0.9 we
* SU IS stand for structured guery Janguage.
* This database language is mainiy designed for maintaining the
dara yelational database management Sysrerne.
° Sqh is Standard language fer Vecessing And Manipulating
database.
—Lypes_of S02 Cornmands:
Dot Dew Teh.
bara Derminen | | dave ryanipvanos!| | vara conneot | [reenzaction
prea ial
stan LANGUAGE PaNtgeh
INSERT Geant
UPDATE Revoke
Devere
Roresack
SPVEPOINT
ATUL 0 mMAR CLINKEDIN) ~DDL COMMANDS:
Dor (Dara DeFinition Language) used fo change the Hructure
oF the tobe I'ke creating tne foble, altering #he toble
Deleting tne rosie.
All phe Commands in the DBPL are auro Committed that means
It permanentty saves ail the changet /n the darabase.
1. CREATE :
this command jt usted fo create Q net. dafabare or
tole.
Syntax :
CREATE TABLE robe. name (
Column datatype,
column dararyre,
Columns datatype ,
Is
Example:
CREATE TABLE Employee
Employee/0 Int,
Firstname varchar (255),
lastalame varchar (255),
Addretfhine Varchar (255),
City Varchar (255)2 Alter
fhe ALTER TABLE statements jn sfrudured Query Language
allow yo add, modify
, and delete Colurnns of an existing
tagie «
dSyntay :
ALTER TABLE Cable-name
ADO Column_name cararype
Example:
ALTER TABLE Employee
AD Email larchar (255);
GR ALL TTS LN TE
3- Drop
TUL Km. INICED AL
AIOTES GALLERY (TELEGRAM).
Tre DroP Table statement ts used fo crop an exirting
table jna databace. thif Command celetes both pne
structure £ Kecorde sfored jn rable.
fyntox:
DROP TABLE rable name;
Examele if
DRoP TABLE Employee
| emer rcremserncmeeNMSC SSA PEN ACSA NCPR TRREA EAR te
4. TRUNCATE
A truncate SPL Starement it used fo vernove al) vows
( Cornplete dora) From a fable. it jy similar to DELETE
Starernent With no PINERE clause.
Continue —»
ATuh eomar (Linneoir),a.
Syntiye:
TRUNCATE TABLE oble. name ;
Exampie:
TRUNCATE FABLE Emproyee }
[oes Arh ele eet eae eee |
DML _Commanps :
2 INSERT sa
SOL INERT trarement it a SQL query It jg used 10 insert
a single or a multiple verde ina table.
Syntax:
INLERT INTO ¢able_name
values (valuet, valued, value 3..);
Hampie:
INERT TO STUDENTS (ROll_ AlO, NAME , AGE, CITY)
vawwes (1, Yadnyesh 19 , Poné);
| UPDATE.
The OFDALE starement is Used ¢o modify the
existing yewords jn a fable.
Syntax?
UPDATE Fable-name
SET cojurnn] =
Valuet, column 2 =
= yalue 2,...
IAIMERE Condition ;
Continue —_,
ATUL WIMOR [LINKEDIA ). CAb
—
EXampte:
UPDATE Custpo meré ; ; iy
SET confactiame = Yadu , city = Pune’
IAINERE CustomeriD = 1015
3-_Deceré
The DELETE statement js used fo delete exketing rewrds In
@ table.
Somme
Deere From tobie_name [WHekE condition] ;
Example:
‘ ”
DELETE FROY Cy stoners IAINERE CustormerNiame = yadu
ANU aun ww).
“AICTE S GALLERY [ FEL A
4 GRANT
It i used fo give user gecess privileges to a databace.
Syntax:
GPRENT SELECT , UPDATE ON rMy_ TABLETO'SOME_ USER ,
ANOMER_ USER;
2 REVOKE
Pris command wWithdraos the user't access privileges given
by using the GRANT command.
GRANT SELECT, UPDATE ON) My TABLE TO (OME_USER, ANOPHER_
USER}
Syntox: Revo ELECT, UPDATE ON) MAY_. TABLE Peom USER! User 2;
Larue kumar Cemceorn).
Bi
4TCL Commands
4. Conmat
Commit a Transaction. The Commit Command faves al) the
transactions ¢o the databose Lince the lash COMMIT or
ROLLBACK command.
Syntax!
COMMIT;
Example:
DELETE FROM grudent LINERE AGE = 20;
Ceranir ;
Sener TELGRULERY ( TELEGRA!
TF any evrer eccurg with any of qhe LL grouped Sfaternents,
all changes need fo be abosted. The process of reversing
changes if called vollback.
Lyntex:
ROLLBACK ;
Example :
Devere FROM spudent WERE AGE = 20;
ROLLBACK ;
ATUL omar (eink EDIN). : &SQL SORT NOTES
The S@L yeserved words and characters ave called operators,
whith ave usted With a WHERE clause in @ ft query.
[ere Name clats __|owwision | cry
10} VYadnytth toth 4 Pune
102 om loth c Mumbai
103 Sani} 10th D Pune
[ey Ronan Jorn Bb Pune
[les Atul tor A Deni |
| Joe Yadav loth L e [Mumbai
Student Informoarion Fable.
| LS
1. _AND OPERATOR
"ne S@L PND operator }$ used With tne Where clause jn
the £92 Muery - AND operaror IN $42 yerurns enly gnose
records wonrtch Satisty both the conditions In fhe £42 guery.
Continue —>
| aru roman (ommerra)—_
Guery 2
2 ” 5 i
Sevecr *FRon gry dent Keke alame = Yadav “MriD City > ‘nfumbai"s
Ontput >
Koli-alo NAME CINsS Diusion city
1oé Yadav Jom c | Mumbai
fn above example rhe sg) statement returns only one value cause
in given g¢osle and thelr iy only one Stident Who has name
"Yadav" f he belongs 4o mumbai.
Note: Thue if all the conditions ceparared by AND Is True.
. R 1b $< PR (LI KEDIAL,
= ‘ OR OPERATOR. OPERATO ALOT ES ileRy (TELEGRAM).
+ Phis operator disploys rhe yewrds yohere either one of the
conditions londitionl and Condition? evaluates 7o True.
« The OR operator elispleys @ yewrd if any of the conditions
Leparated by OR is Tyue-
Query >
SELECT * PROM student WERE Division 2"C "OR City = “petri”
Ovtput>
Roll-slo_| lame Class Dison city.
jor om [oth C3 Mumbai
105" aque loth a Deni
106 Yadav loth c Numbai
Continue —>
LAruceomon(erniEDIN)- ye—<—
Jn qne above example phe <9! ttaterjent returns phree values.
Cause Given qoble have #wWo Sudentt tho belongs fo C Division
one ts From Delhi.
Alte: TRUE iF any of phe conditions separated by OR it TRve.
3. Between! OPERATOR:
Poe Between operator In fa. shows pre record within the vange
mentioned In the SOL query - The operator operttes on qe
numbers, characters, and dare /¢ime operands.
Query >
evecT * EKO Student WHERE Roll_nlo BETWEEN Jo2 HxD 10uS
wm PR [ LINKED Ial ).
Outpur>
Zoltato | alame Class Oinsion city.
for Om loth a Mumbai |
103 JSQnil joth dD Pune
104 Renan jo B Pune.__|
In phe above example ghe £4] >
Query :
ELECT * PROM Grudent WIMERE CITY LiKe LUy.)s
Aruc Komer [t
Output: Alor ES GALLERY (TE ERAT,
[eon lo Alame class Division! ary.
| yor fadnyesh foth Pane
foe on toh | rfumbal
103 Sant) lorh Pune
toy _|_ Renan Joth Pune
106 Yadav Jor Mumba
Bh Firsr Query yerurns all alame of Students starting from Y.
In second Gyample yeturnfall the records t2ho have setond
Letter 15 UV: ¢ren any Combination oF faster.
5-_Alot OPERATOR _
Alor operator jn SGL thowS those vetordt from che foble
woneve he criteria hs not-met. Alot operator jr used with
Where Clause in a SelecT query
Query :
Seuccr * From Students Were Nol city = “Mumbai”;
Alame Class [ Dinsion! a
i Yadnyesh
Lahil
Kohan
erul eumak (LINKEDIA ).>
in the above example yerurns Phose Ludent yetordst fhoste tno
are not from mumbal. Pris query athows yewords of frudents
Aaving city orher pnan Mumbai.
6+_IN OPERATOR
kshen we want 7° chek for one oY Moye than One Value in
@ Single JGL ery » 49e use IN Operator ith the WHERE
clause ing f€teécr guery-
16 Keo MAR ( LINKEDIN,
Oe Nee a —| i
Query :
Levecr ¥ Feo Srusents wunere city jl "Detni" Pune");
Outpur:
[gon-alo | Name Class Dwsion | City.
Jo} Vadnyes? Jot A | Fane
103 Sari) jor” Dd Fane
104 fonan ton & Pune
a
[ tos Atul fot Deini
In the above example yerurn prose ffudent yewrdsS ¢hose who
are From Delni and pune. other dhdent vill nor displayed:| SQL COMSTRaNTS
SHORT MOTES
PART -3
komen cinkeDi~) «
AOTES GALLERY (TELEGRAM
KEYS i DBM.
A key it an Attyibure ov Set of attributes phat uniquely
identities any yeecord ot ¢uple (one you) From the fable.
DEémo_TagLe
eme-ia Alame Adhas- no | Derr Id
Jol Yadnyesh 2952 Y562 3221 )
lor vedgat 4334 2260 1220 2
103 ahi} 9525 21U) 3663 3
104 Rohan 9692 1258 745% 2 st
105 ahi) 19994 $214 3698 li i |
106 Siddharph | 1234 S678 9622 4
aul Kuma CLiINKEDIAD.O- Soper Key 2. Canclidate Kay
3). Primary Key 4). _Aiternare key
5). Boreign Key 6). Composite Key.
1. Super key
P duper key iS @ combination of all possible attributes phat
can unigely identify phe 7005 Cov peuple) in pre given relarion.
4 Soper key ig a Superset of 4 candidate Key:
* & fete can have many Soper keys. eB
* A Super key may hove addittone attribute that avé nor
needed for unigue jdentity.
Super Keys : From Empleyee Table (Fig//).
1. (Emp-is}
{ panar-noy
{ Dept_Id}
{ Gmp.is , Athar_no}
& { Adhar_no, Dept.id}
6. { Gmp_id, Adhar_no, Dept. 1s}
a { Emp-1d , Jame}
& C Emp-id, Adhar.no, rlame , Dept.Id } ete.2 _Canaidate Key
A Candioare ds an attribute ov 6 of an attribore
Which can uniquely Identity @ tuple.
A Candidare Key ‘tf @ minimes Seper key; or q Super Key
with no redundant attributes.
Candidate Keys are not allewed fo have Note values.
dare Keys: from (A91-1).
1. {Emp_ ia}
2 {Adhar_ no}
3- {pept_ id}
3. Primary Key
A Primary Key is one of dhe Candidate key choten by the
database deslaner fo Uniquely Identify the fuples in the
relation -
ic eumne (LINKEDIN).
E
P The Value & prmary Key can never be aluec.
> Me Jalue of primary Key must af ous be unique.
> It define column is o mandatory Freld.
2 Primary Key do nor have supliicate value.
> Primary kay 15 not compulsory bur recommended.
Lriasy Kegs? From (679 1-1).
1. (émpe. Id }OU of all Candidate Keyl, only One get’ teletted as primary
key , reraining Keys ave Known as Aternare Keys
In the Employee cable :
° Emp.id Is best suited for Primary Kay ~
© Katt of the artrbures ike Adhar_no, Deptld are Considered
as a altemare Keys.
Altemete Kegs: Fyom (Fig 1)
{ Adhay_no}
2 Lept— is}
5° foreign Keys
A Foreign key is:
aaa Key ased fo link (00 fables fogether «
* An attributes (or set of attributes) gn one fable pnat relers
+o the primary Key.
The purpose of qhe foreign key Is
* fo ensure (or maintain) segerentlel integrity of tre clara.
foselgn Kegs: Fyem (6251-1).
1 (Depts ,}_ATYe KOm, LINKEDIN ) «
Aah sean POLE
Ar Key ghat has more fhan one attyibeter fs known arg
ComposHe Key. Ir }'s alre Known as Compound Key.
° A composite key can also be made by the combination
of more ~rQn one Candidar key.
° A Composite Key Cannot be null.
Composite Kays: From (£1911).
' {Dept-td, Emp- Id}