0% found this document useful (0 votes)
10 views24 pages

DBMS File

hu

Uploaded by

shashwat garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

DBMS File

hu

Uploaded by

shashwat garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

RAtse

" REATE T ABLE CREATE TAB LE tablemame(


Symtax
Colunn . oataypc Ceenstraint 1,
column 2 datatype Ccontr ait 1,

calumnN dlatatype Clomba tnty

" ALTERTAB LE: ALTERTABLE tablnamne


Syntax
ADD columm-mame datatype

"DROPTA BLE: DROP TA BLE table.mae j


Syntax

"TRUNC ATE: TRUNC ATE TA BLE bbleL nane ;


Syntex

" RENAME; REN AME ld-tablemame To meu tolle


Syn tax

DBMS Lab File (BCS 551)


2. Output
"CEato Table
CRE ATE TA BLE employees C
emplayee _id NM BER TRIMARY KEY,
kiut mame. VARCHARRC50),
last-mame VAReHA RRC50,
hene-ate DATE,
salry NoMBE RCS, )

ALten Table
ALTERTABLE enploye©s
MoDLPY catmmm et-olatatpe ADD em ail VARCHAR?

Dop tble
DROP TA BLE employees j
uneate
TRUN cATE TABLE tablenae;
" Rename

RENAME employees To stt;

DBMSLab File (BCS 551)


Lnset
Syntaei INSE RT INTO taslename (Colum, calumn
Calumns, - )
VALUES (vale 4, Vale 2, vale sy ooo);

"Seleett
Syntaz:
FROM tablemame }

Syntar: UPDATE toble_name


SET Colmn z valle 1, calun 2=valu)

" Delete

syntaz: DELETE FROM table ame

wHERE Cardiion ;

DBMS Lab File (BCS 551)


2. Output bostmae,
enpleyers (employee- ids
t i- IN SERTINTO
tastmae, hinedata, salay
Jahnson''2024-8-2|, 7Oo00. 0 :
VrOE S C1o03.'Michael:
Seleet - SELECT *
FROM employees ;
Upoate :- neneaue the. salany by s Y of ae emplagees
in the enploy ees table s
U
PDATE n ployees
SET erT solary Salry *("05;
" DELETE - 7o olelete an empbyeet with emp ly ee-i
Io03 1&m he emplo yee.s tab le i
DELETE FRoM employ ees
=l003
WHE RE employee-id
3. Questions and Answers

a. What are the different ways to insert data into a table using the INSERT
statement in SQL? Can you provide examples for each method?

Ans:

DBMS Lab File (BCS 551) 4


CREATE TABLE t6le-name C
Calelmn 1 datrtype cONST RAzNT Corstaintm
Constaint type,
Colunn 2 dlatatype oNSTRAINT cerstraint mam
Centnait type,

To add a
Canit aint ALTER TABLE le- nae
ADD coNSTRAIN T Canstraint-ume Cornitrantb
Ctslumn-merE);

To madi a
ALTERTAB LE tatlemame
()
no DI Y coNSTR AT NT Cantthaint- rame
muecantraint defintion ;
To drap
ALTER TA BLE t l e a e
DROP CoNsTRALNT conatraintMame;

DBMS Lab File (BCS 551)


Examale. - cREArE TABLE employeesC
2. Output
employee-id. TNT PRINARy KEy,
lastmne VARCHA RCSO,
email vARCHA RCI00) UNLAUE,
depav trnemt- iol IT,
FORI ECiN KEY Cdepantment- id)
REFERENCES depatmunts (dlepa-tmente
ALTE R TA BLE ¬mp lo yees
ADD coNSTR PINT cheek-s alary cH ECKCsalay>o;
6) ALTER TABLE
emp loyees
Mo DIFY coSTR ATNT Csmsti aint-me esCanstatnt
olyenitiin;
AL7ER TA BLE emp lo yees
DROP CoNSTRALNT Camigue-enail j
3. Question Answers
a. Explainthe purpose of the NOT NULL COnstraint in SQL. How does it
affect data entry in a table?
Ans:

DBMS Lab File (BCS 551)


Synz
Addiien():- SE LECT Cexpssion 2> + cexprssions As
aliabmne
FROM tatlemamme >
WHERE <Conllians

Divisian():
aliasae
FROM tablemaes
WHER E cantiom>;
PowER c^): SEL EcT POWER C6 aue, expenent) As
aliasmne
FROM etable name;

DBMS Lab File (BCS 551)


20
2. Output
Example ;- 0 SEL ECT Q0 +5 AS SUM

FROM olual;

SELE CT 2O /4 AS quotient
FROM dal;
SELECT PoNER (2, 3) As neslt
FROM olual;

DBMS Lab File (BCS 551)


Syntr 0AND:- SELECT olumn 1, Colum 2y
PROM toblenane
NHERE Conditien 4 AND Candithane AN

FROM talemame
wHE RE Candithien 4. oR Condiisn 2 OR.

SELEcT Calun 1, calunn , r


FROM table marne
WHERE NOT canditian j
4)
BETwEENi- SELECT coln 2, Colmn2,
FRO M table.-mamne
WHERE Callmn-mane BEr WEE N vale 1
AN D valle 2;
6) ANyl So ME;- SELECT
calumn
FROM talename
,
calumn2,
WHERE ealunn name
apeaten ANY Csutbquen
SELECT Colum 4, calun, o
FROM tablemame
WnERE Calumm-mame openatay SoM£Qbquwy
6) ALL: S
ELECT colmm 1, calumn
FROMtoble-mame ,n
wH ERE
colnmame epey atey ALL
Csubgurg
DBMS Lab File (BCS 551)
2. Output Examples;-0 SELEe Temployee-i, jiutmumi,
last e
FROM enployeea
WHERE depantnent= 'sales' AND Slary >50000;
SELECT employee-id, firatmame, last_ mme
FROM emplayees
NHERE depastmint =sAlE Sales' oR depantment ='Manketing'
SELECT enployee - id, instname, laatmame
FROM employees
wHERE NOT department= 'S all';
SEL ECT emplayee -id,
FROM emmp loyees hiut-mame, lastmae, salay
WHERE salan4 BETwEEN yoo00 AND 6 0O003

SÈECT depatyt_aone &ELECT employe e_idy


FRoM depantmts a FROM mployees föatmame,
WHERE EXLKS WHER£ Salany s ANy
SEL AeT CSELET
FROM ployees bemchayk_salag
benchnarks);
FROM salang
wHERE e.daprtmmt id= dt
otepasctrnentto
SELECT aole-id, nlen aato
FROM olers
WHERE arlen olate < = ALLCSEL ECT
heliolay date FROM
DBMSLab File (BCS 551) haliolaydates)
yaoeSesare

. UPPER ()and LOWER (): Converts a string to uppercase or lowercase.


Example:SELECT UPPER('hello')AS upper_case; (Outputs HELLO)
. TRIM (): Removes leading and trailing spaces from a string.
Example: SELECT TRIM ('Hello World ') AS trimmed_string; (Outputs Hello
World)
junotisns in saL arnel
sel
Five commanly wsed 9ggt
huin agplicatians ; -

af
"Pupose: Ceunts the mum bun
Column an tab le.
cutames,
use Case: Ceuntina the to tal mumker af
anders in a databae.
ynaducts , aM
2 SUM)
the 6um of all mmerical valus
"Pupase:- lallates
a speeified calmn,
total sales tatal
" Use Casei
caleulatin the
total imvontwg value.
expensey
"Pwnp ase- Caleulate the aMerage value
values on aa speetied calun.
taleulating te avenqye solany f emplayeesy
OMeage precluet plee , a anen age ader value.
aviage

DBMS Lab File (BCS 551)


2. Output

Purpase i- Detomins the mini nun valu in 6L

-Calumm,

"e Cas.;- Findlng the daweat pradiet piee, the canut


edon date, ay the empleyee wíth he loweat
S. MAXO:

Purpose : Detemines he mn axi mum volua ma speclhinod


Calun.
" lae Cae : Findling the hiyhet pnaduct the latest
arder olate, an the em ployee with the highest salavy.
3. Question Answers

a. What is the difference between the COUNT (*) and COUNT


(column_name) functions? Can you give an example where both would
produce different results?
Ans:

DBMS Lab File (BCS 551)


F20M table-ame t, tatle-are?
WHERE toble mane 1. talurn amez tabloeme2.
Calumnae;
Leßt aucty jain i- &ELECT calame l6)
FRoM tble4
LEFTJOIN to6lo2
ON bble 1.,calumn.name =tabla R. colunn-nae;

Right auttn jain


almi- SELECT column-armelC
FROM toble a
RE GHT TOIN table?
ON able4. alumm.ameztoble 2 Colm me

"Full autn jain- SELECT


(alumm-ame Cs)
FROM tablo. 1
FULL oUTER JoTN toklee
ON table 1. calnae = tale 2-colueame
WHERE Cerolitun;

DBMS Lab File (BCS 551)


2. Output Exam ple- SEL ECT Custobib. euAternem None,

FROM Cuatemes
LEFT ToLN deolos oN eUETO Cuutemecs,
CustameID rddos. utomoD
ORDER 1BY Cuuternes. Custorme Norre j

SELECT ades. andertD, Empleyees, Last Nome, Enployels.


First Name FR0 Mardo
RtGHT JO ZN Employees ON trdens. Enployee ID= Employ ees,
Enp loyeeID 0R DER BY andens. ardon ID;

SELECT Custemers. custenen Name, bdas.ordonD


FROM ustemus
FUL OUTER TOLN ON

Cuutem@s. CuteeNTD z Odes stmID


ORDER BY Ctoms Custenon Mne;

DBMSLab File (BCS 551)


DATE: -

Program No. -8

1. Objcctive: Write SQL queries using subqueries and SET operators.


u t a 0Subquy - SELE CT calumnmame C, Calmn-mame]:
FROM table 4 C, takle 23
WERE alumn-mame oPERA TOR
(SELE CT calmnae C, CalumnnaneI
FROM-table I, table 21
C WHEREI)
A nian :- SE LE CT Columnmarne FROM table 1
UNZON
SELEcT colunname FROM too 2

lnian Nli- S ELECT colum-name EROM table 4

SELECT colunname FROM tble 2;

SELECT Calmame FROM table


LNT ER SECT
SELECT Colunn- mame FROM toble 2;
6) Mimus - SEL E eT
M NUS
calun-mane FROM table

SELECT olumm- rame FROM table 2:

DBMSLab File (BCS 551)


2.
Ouiput Exmpl: Cuatemes
O Sa> Slect * Fem
Whece td in Ciele ct id som Catomus
Where salay > 45 0o);

SELECT Gelumnae k FROM Fiut


UNION
SELECr FROM Seeand;

SELECT * FROM Fist


UNto NAL
SELECT* FROM Secord

SELET FROM Fit


INTERSECT
SELE CT * FROM Seeed

SE LE CT *FROM Fist
MINUS
SELEeT * FROM Secand

DBMS Lab File (BCS 551) 20


deelane
pi comutant numbey (4,2)!
3ar9;
iadius mumben (5)3
egin
nadins i =3;while
nadius l=7 loop
tnsort into einele values Conasllua,aa);
Madius:zhadiLes tI;

Seleet rom ciele


set sunenautput on
oeolane
mumb (4):= &n

b umbu (2) =l
Cnumben (9)
while m >o
loop

dbms autput. put-line Co);


endleop;
ed;

DBMS Lab File (BCS 551)


Date:

P'rogram No.9

. 0bjective: Crcatecontrol structure programs in PL/SQL.


P/SQL 0s a combination of SQL along with the procedural features of
progranming languages. It was developed by Oracle Corporation in the
carly 90's to enhance the capabilities of SQL. PL/SQL is one of three key
programming languages embedded in the Oracle Database, along with
SQL itself and Java.

deolane a
mamben (a) &a;
b mumben (2) b;

begin i a>b and a>c the


dbms.autpte puttinn ('getut' l03
b>o a d b>e then
elsèf
dlb ms atput.put tina C'yuaut 1bD;

dbmsautput.put eine C'guatyt' Ilc);

end;

DBMSLab File (BCS 551)


RODIUS
2. Output 28-26
50.24

5 78.5

old stb mumbenl?) s=b;

3
meue : cnnbe1(4):z Y;
PL SQLptocedwe sueceshuy guateut y
3. Question Answer PLISQL oudwte Sucelsully tungl
a. What are the primary control structures used in PL/SQL? Describe each
with an example.

Ans:

DBMSLab File (BCS 551)


44
Date: -

Program No. -10a

1. Objective: Create procedure and function in PL/SQL.


Procedures: These are the subprograms that do not return a value
directly; mainly used to perform an action.
EPakdweme
"Creakinga Phscdwe:- (REATE CoR REPL CEJ PROCEDUR
typcG..I)]
L(paramten-nam CINÍoUrlEN dUT]
$LS|AS3
<erecdwrc baly
END eeedune -mame
Ceating a
Funekin ! CR EATE (ORREPLACEJ FUNCTTON uretiemneme
LCparameon mame LIN loUTlINOuT*type JI
RETURN etwu- dataype

<bunotten bady
END CHunetian.mame}
Calling
DECLARE
c mum ben (2);
BEGIN

ciz telal CusteminsC; dkms-output.putt line (" Total

END;

DBMS Lab File (BCS 551)


geeigs iD
O CREATE OR REPL ACE PRoCE DURE
As
2. Output BECrTN
doma-altpct put.lineC'Hello wWanld ');
END; SELECT CaUnt ( into total
CREATE 0R REPLAE FUNCTIoN
tatal Custorws FROM Cuttanes
RETURN mumors IS RETURN to-tal
EN D;
BECIN
sutt-7efal no, af Cstomes
3 Whern Cade is exeouted atsal ponet
3. Question Answers
stored procedure and a
a. What is the primary difference between a
function in DBMS?

Ans:

b. How can a function return a value, and how does this differ from a stored
procedure in DBMS?

Ans:

DBMSLab File (BCS 551)


CREATE TABLE empbeta
emgloyee-id TNT,
jirstayy VPR¢HAR (255,
fast
alay
.mamme (255),
VAR CH AR C
DEcIH AL (10,2)

NSERT INrO omplbytes emplaytt iolyost -rume, lost-om, Salay


VALOES
(,'Tehn', 'Doe, 6o000,00),
(R,'Jane', 'Dac, 70000. 000
(3,'Beb','Smith', Yo0 00.00,
(9, 'Alice,'Jehmsan, &0000. 00);
DECLARE
CURSOR employee- cwar IS
<ELECT Umpbyee-id, kiut mame, lastue
PROM emplo yees
WHERE salary so000j
ernpleyee- id employees. empbyee-iol %TY?E
houtmame emgloyees. forut mame%T YTE;
Last.eme employ ees. latame d"TY?E
BE ChEN
o7EN employee-cwwan;
Loop
FETCH mp loy ee - owaN DNTO emp loyee-id , joutame,
ExIT WM EN last-meej
mgbyee-cwnsy o NOT FoUN D:
DBMS_OUTPUT. PUT LINE Emplayee TD: (1 emp lay ee-joU3
DBMS-OUTPUT. PUT- LINEc'Name:' hyoutmarne I|' )
DBMS_0UTPUT.PUT- LINE C---.
END LO oP: -
eotna}
CLOSE emgloyeewYeI
ENDRMS Lab File (BCS 551)
50
Date:

Program No. -10b


1. Objective: Create cursor in PL/SQL.

atput- Emplbyee TD! |


Nane John Doe
Employee TD :2
Name Jane Doe

Emplo yee TD:4


Name : Qice Johruon

2. Question Answer
a. What is the primary difference between a procedure and a function in
PL/SQL? Provide examples of when you might use each.
Ans:

the
b. How do parameters work in procedures and functions? Explain
difference between IN, OUT, and IN OUT parameters with examples.

Ans:

C. How do you call aprocedure and a function in


PL/SQL? Provide examples
in SQL statements or
for each and explain how the results can be used
PL/SQL blocks.

Ans:

DBMS Lab File (BCS 551)


Date:

ProgramNo. -10c
I. Objective: Create triggers in PLSQL.
CREATE TABLE Cmploy ees(
Cmployee-id INT,
sutaname VARCH PR(Q55),
laut.mame vARCHpRC265,
Salary DEczMAL (Io, 2)
);
eR EArE oR REPLACE TRI GGER gdato salany
BEFORE UPDArE OF Salay oN empkbyees
FoR,EACH RON
8EGLN
TE :NEN salarye:0LD Salany THEN
RPISE- APPLICATION ERROR (-200o t, 'Salarå camnet ke oleeeaato }
ENDIF;
END;
ZNSE RT INTO emgloyees (erplay ee-io irut. mame, lact mamey salamg
VALUES
(I, "Tohn'oe, 6o000,o0);
UPDATE ernployees
SET Salay70000.00
WHERE employ ee ioh =l;
UPDA TE employees
SET Slany z 8000 0. 00
wERE employee-id=lj
utt:

DBMSLab File (BCS 551)

You might also like