SQL DDL and DML Commands Guide
SQL DDL and DML Commands Guide
Chapter 7
SQL, the Stpattured
[Link]
Overview
Introduction
.DDL Commands
. DML Commands
. SQL Statements, Operators Cduses
Dr.
[Link]
[Link]
Mat Aggregate Functions
DROP
[Link]-Madan
SQL: DDL Commands
CREATE TABLE: used to create
database.
created.
SQL: CREATE TABLE Statement
Things to
[Link]-Madan
SQL: DML Commands
INSERT: adds new rows to a table
UPDATE: modifies
DELETE: deletes one
one or mare
or
atfes.
more rows from a
table
SQL: INSERT Statement
To insert a rom into a table, it is reces
harve a value for each attnbute, crd
matters.
rame SET
WHERE selection condition
Example: UPDATE FoodCaa
Madan
To update the content of the table:
UPDATE Statement syntax:
UPDATE ctable
«attr»NCie
sold 349
SQL: DELETE Statement
amtaMadan
To delete rows from the table:
DELETE Statement syrtax
DELETE FROn <table name
HERE condition:
Example: DELETE FROM Food
HHERE fod = hotdog
UHERE date '02/208' AND food 'pizza':
Fodcert FoodCert
date fooosold date food sold date food 6ld date food sold
02/25/02wa 350 02/25/08 pizza 349 02/25/08 pizza 3499 02/25/08 pizza |349
02/26/0hotdog 500 02/26/08 |hotdog |500 02/26/ndtdog 500 02/26/08 pizza |70
02/26/08 Ipizza 70 02/26/08|pizza |700 02/26/08 pizza |70
Note If the VHERE case is oitted cl ros of deta dekted frs the
ce
t
04-11-2022
SQL Statements:
Mamta Madan
SQL Statements, Operations, Clauses SQL: SELECT Statement
A basic SELECT statement includes 3 c
- Select
SELECT attr ibuta name> FROM <toboUHERE <condition>
SQL Operations:
Join SELECI ERON WHERE
ta
.Left Join
Specifies the Speclfles Specifies the
.Right Join attributes that are tables serve
selection condition,
part of the p u t to the including the Join
-Like resulting relatlon Ement condition
Dr. M
sQL Clauses:
orde
Grou By
Having
Mamnt
28 64 Har O34 80
Example: SELECT * FROM ... ERE. George|29 70
Helena | 54 54
Mena 54
Peter 80
Peter |34 80
To get unique rows,te the keyword 2) SELECT weight 3) SELECT distinct weight
DISTINCT after ACT. FROM person
FROM person
Example: SELEDISTINCT FROM WHERE age > 3 WHERE age> 30;
OE-
Weight
| 80
04-11-2022
Example: Mamta
UHERE clause which lists
te join condition. FROM emp left join
on [Link] [Link]:
dept
wRERE1ke
finds ID
_o FROnEm
ordery id asc
that has the second and third character
as 01, eg. 1010, 1011, 1012, 1013, etc puts ID in ascending order, e.g. 1001, 1002, 1003
MamtaMacan
SQL: Aggregate Functions
Are used to provide summarization infog for
SQL: Aggregate Functions (cont.)
FoodCart adan
SQL statements, which rcturn a singlsue. date food sold
02/26/08 plzza
coUNT(attr) 02/26/08 hotdog0o
SUM(attr)
MAX(attr)
MIN(atfr)
02/26/08 pizz 70
AVG(attr) coUNT(attr)- retu of rows that are not null
Ex: CoUNT (distije food) from FoodCart;-2
Note: when Msing aggregate functions, NULL values
SUn(attr)return the sum of values in the attr
are
notsdered, except in CoUNT("). Ex: SUn0 from FoodCart; -> 919
FoodCart
date |food
rMadan
SQL: Aggregate Functions (cont.)
Sold
Dr.MamtaM4adan
02/25/08 pizza Some more add opn
02/26/08|hotdog 500
02/26/08 pizz 70
.MIN(attr) -> return lowest value from the attr
Ex: MIN(Ssold) rFoodCart: - 70
Madan [Link]
Adding a constraint...
Sql> alter table employee addnary
Dropping a constráint key(name);
mta'
Sql> alterable employee drop
Dr. M primary key;
[Link]
foreign key(deptno) refermdes
department;
Date-03-jan-2013'
04-11-2022
Views Mädan
.In SQL a VIEW io a virtual relaton base he result-set of
Views 4àmtaMädan
SQL Relations, Tables &
a SELECT otolement When we say Relation, it coutd be a Tay aView. There
are three kind of relatons:
Avlow conlains rowD and columno, Just llke a real table. The
floldoIn o viow ar0 liolds froUY or moro real tables In Uhe 1. Stored relations
dalaboso. In 0omo cas0s on modity a view and present es
tho dolo os f ho datla yecoming froma oingle table. We sometimes use the t b a s e relation or "base table"
2. Vinual relations
Syntax Viers
WERE Conditlon
04-11-2022
1979
SQL MamtaMadarfs
Querying View exagple
Movie (Ute, year, length, inColor, studioName, producerC#)
Movie Exec (name, address, certt, netWorth)
SQL
(INSERT)
Modifying View
Madan
INSERT INTO ParamountMovie
CREATE VIEW MovieProd AS VALUES (Star Trek', 1979):
SELECT le, name To
make the vlew PaamountMovie updar ule, we naed to add attnbute
FROM Movie, Movie Exec studioName P a L n c a u s e bacuse it makes more sense if the
StudioName is Paramount
WHERE producerC# cert insteacNULL.
SELECT name CREATE VIEW Paramount AS
FROM MovieProd SELECT studloName year
WHERE Lile =" With the Wind; FROM M
Movie
WHERE studmo "Paramount;
Same resusquery from tables Then
SELTahe O ParamountMovie
FROMOvie, MovleExec s(Paramount, 'Star Trek', 1979):
WHERE producerCt# = certt AND Ileo = The War Of the World
Tille year length InColor shudioName
Star Trek' 1079 0 NULL Parmount
producercs
NULL
04-11-2022
UPDATE Mov
DELETE ROM Movie SET yeac 1979
WHRe LIKE %Trek6' AND studioName = 'Paramount; the Movie' AND studioName=
ount'='Star Trek
WHEe
P
DROPLE Movie
04-11-2022
[Link] Madan
Subqueries
SELFJOIN Operations
A SELF JOIN operation is usedtopodce
a result table when the relationsi
interest exists among rows hfe stored
within a single table.
D r . M
P_
-
SEJOIN Operations
SELECT e1.emp_last_name ||. || e1.emp_first nam
Supervisor
Triggers
MaO
e2.emp_last_name ||'. '|| e2.emp_firstnam
FROM employee e1, employee e2 PLUSQL can be used to wpitoaata basse
WHERE e1.emp_ssn e2.emp_supe
=
da gadan
Dr. Manmta M
04-11-2022
[Link]
on
Mafyta Madan
Conclusions Types of triggers
Madan
Whenever we update the evooyee table, a Row Triggers
trigger will be fired e.g A row trigger is fired eacKtime a row in the
Update employee1 table is affected [Link] g n update statement
Set lastname='aofa updates multipleoWS , a row trigger is
fired once for paeh row.
Where empr
We
reco
can now check the audit data,
úst have been inserted. I.e aa
new Statementriggers
triggrwill be fired whenever an update in
A staehent trigger is fired once on behalf
of tridering statement.
the employee table occurs.
uepew eauigiN a
uep
uepew
04-11-2022
DECLARE
Bank Transaction dan
acct_balance NUMBER(11,2);
acct CONSTANT NUMBER(AO3;
Consider the program belowaich
processes a bank transaqion. Before
deblt amt CONSTANT NUGTEER(5,2)
500.00;
allowing you to withdtav $500 from BEGIN
nta
SET bal
ELSE
bal -debit_amt WHERE
account i d = acct;
d a n
Dr. Mamta Madan
Trapping predefined
exceptions
Dr. Mamta
acct_balance, 'Insufficientfds');
COMMIT;
End if
End;
04-11-2022
Trapping Exceptions
Madan
Madan brgamta
mtaMadan
Madann
Another example
[Link]
Trapping user-defined
exceptions
Using the RAISE statement
execution of a PL/SQL bRERdan
or
The RAISE statement stopsadmal
subprogram and transers control to an
exception handlerAISE statements can
raise predefineexceptions, or user-
defined exeáptfons whose names you
decide
pr
04-11-2022
department deptno;
IF SOLNOTFOUND THEN
RAI_invalid_dept END IF;
EXCEPTION