DBMS Unit 3
DBMS Unit 3
UNIT -3
SQL
Declarative
language
•
SQL GIS)
core
components extensions
leg
• : + :
DDL
1. CREATE
•
Schemas tables , domains , views , assertions , triggers
,
•
PSQL : database
1. I create schema
named of
catalog collection schemas
-
-
Schema called INFORMATION - SCHEMA
Default schema (
creating schema) : owner name
1. 2 Create Table
•
Default schema (creating table) : PUBLIC
PUBLIC schema
CREATE TABLE EMPLOYEE ←
•
Prefix table name with schema name while
creating
- , I
7
1.3 Virtual Relations
type ,
Tl
(a)
String
Fixed CHARM CHARACTER Cn)
length :
•
1 '
Enclose in
single quotes
•
•
Default n = 1
Fixed
length blank
padded
•
: -
(b) Numeric
INTEGER , SMALLINT
Integer INT
•
:
,
•
Float : FLOAT , REAL DOUBLE PRECISION
,
'
-
i :
precision ( total
digits)
of decimal default =D
-
j : scale Cno .
places -
DECIMAL (5) 2) ,
DECCIO)
(c) Bit
string
1311Th) BIT VARYING In )
Fixed length Variable length :
•
:
,
Default n = I
'
and prefixed 1001 )
'
Enclosed in l '
with B
leg :B© vibha’s
•
notes 2021
(d) Boolean
•
True and False land unknown for 3- valued logic)
(e) Date
Ten
positions :
yyyy MM DD
. - -
6
Enclosed '
in
single quotes
•
•
Sometimes prefixed with DATE
' '
Eg DATE 2019-02-28
•
:
Cf) Time
Eight positions HH : MM :S
•
:
•
Time Ci) -
i extra
digits for decimal of seconds
É!
(g) Timestamp
cfraction of second)
DATE + TIME -1 6
positions
•
"" "
"" " " "" " "
"""
•
' '
•
TIMESTAMP 2021-09-28 19:40:30 643792 .
( h) Interval
•
Relative value to increment / decrement date ,
time , timestamp
types
(a) CLOB
documents)
Character
large object [ text
•
•
PSQL : text ( 1GB Max )
•
Oracle : Max 4GB
(b) BLOB
videos etc
Images
-
( Max 1GB)
PSQL :
bytea
-
•
Oracle : Max 4GB
Eg image blob(10MB)
•
:
movie blob(2GB)
PSQL to insert images
use
psycopz
•
:
•
Renamed default datatype with or without constraints
for domain
Easy to
change datatype
•
Improves readability
•
create
is
type
Defined c. UDTs )
User Types
•
1. Attribute constraints
1. 1 NOT NULL
•
Implicit for PK
1.2 DEFAULT
-
DEFAULT (value >
2.2
Aliasing
SELECT E. f-name
,
5. Lname from EMPLOYEE AS E EMPLOYEE AS S
,
2- 3 WHERE
Missing
•
select all rows
•
IT relational operation
•
Cartesian product -
multiple tables
2-4
Joining
SELECT JOIN DEPARTMENT ON CDNO
* FROM EMPLOYEE DEPARTMENT DNVMER
)j
-
-
NATURAL JOIN
-
RIGHT OUTER JOIN
default : select
By all
•
Result :
•
UNION
°
EXCEPT ( difference) postgres ,
MINUS -
Mysql
•
INTERSECTION
© vibha’s notes 2021
2.8 Pattern
Matching
•
Postgres only -
varchar
•
Oracle - dates also
•
LIKE
_
: one char
% :
any number of chars
'
'
SELECT f- NAME , ADDRESS FROM EMPLOYEE WHERE ADDRESS LIKE % Houston
-1 .
;
-
For dates in
postgres
-
typecast
'
SELECT BDATE FROM EMPLOYEE WHERE BDATE : TEXT
'
LIKE 1965-1
'
- .
;
2-9 BETWEEN
Including
of
2. 11
Ordering Tuples
•
ORDER BY
u
Default : ASC
•
DESC
© vibha’s notes 2021
SELECT LNAME FROM EMPLOYEE ORDER BY SALARY DESC ;
•
Order by multiple columns
Diff orders
3. INSERT
5. UPDATE
Postgres
-
( Oracle dob>
1. text :
large text -
2.
bytea :
images ( byte array) ( Oracle -
blob)
text
3. ntext : even
larger
psycopg 2 :
python
•
logic
•
NULL due to 3 reasons
, ,
nested
using
1. 2-1 IN
, . . .
IN
operator is same as =
ANY
Sats in dept 5
salary > all
-
•
=
all 4 > all
may give 0 in most instances
.
=
any or = some
1. 2-3 ALIAS
1. 3 CORRELATED QUERIES
•
normal : inner then outer
more
✗ optimised
check if inner
query is
empty
.
like IN
from )
( select f-name
,
I name
employee , dependent where essn -
- Ssn
(b)
Eg
Ca)
mgr -
ssn and ssn = essn ;
Q3: Retrieve the name of each employee who works on all the
projects controlled by department number 5
leftover projects
÷.i÷:*
emp
É
have leftover
works on
1. 6 JOIN in FROM
Col
•
1.7 Functions
Aggregate
from
•
NULL
ignored aggregate tune on
particular cot
•
count (* ) includes NULL in few columns
, , , ,
use
out
groups)
valid if I value
per group
•
is
Group by NULL one
group
•
Group by with
having
°
Result
order u
correct :
dnos
get →
{ }
write statements
auxiliary
-
with
c- optional
abc
keyword
as
eg :
C
complex query
:
7
select * from abc
;
•
WITH mainly for readibility ; can use nested query instead
•
CASE : like switch case
2- 1 ASSERTIONS
model constraints
Not in
pg , only Oracle
, MySQL
2-2 TRIGGERS
event
Triggers : actions taken when occurs
-
faction (function
/ procedure)
© vibha’s notes 2021
( can return) Cno ret)
Functions
Idf → list
others
-
can use
$$
declare
- -
variable declaration ( local)
begin
logic
- -
end ;
$$
CREATE TRIGGER -
/
BEFORE AFTER -
ON
-
CREATE PROCEDURE
no return
cursor.cat/ 1)
"
virtual table
•
•
can make a view of a
join
strategies
modification
(a)
Query approach
to Csub
Maps view name
query query?
•
-
maps
stores view
physically
•
Updatim strategies
•
immediate update
lazy update [most)
-
periodic update
-
Updation of views
view constraints
•
Restrict access to hidden details
Grant
permissions to users
.
•
Online Analytical Processing
•
Allows data to be summarised and viewed in different ways
in an online fashion
Negligible delay
•
For schema
explanation
•
:
can
upon
attribute
quantity of the sales relation
eg:
-
•
Dimension attributes : define the dimensions on which measure
.
Data that can be modelled as dimension attributes and
measure attributes are called multidimensional data
•
ROW headers - values of one attr
column headers -
•
All combinations with summarisatims
•5
•
Number of ways to
group tuples for aggregation
3×3×4 = 36
•
with all
4×4×5=80
23=8 groups
group by on each group © vibha’s notes 2021
3. PIVOT
•
cross -
tab is 2D view
dimensions used
change in cross tab
•
-
4. SLICING
•
Slice of data cube
5. ROLL UP
6. DRILL DOWN
•
coarse granularity → fine
granularity
•
Must be generated
Implementation of OLAP
•
MOLAP : Multidimensional attr
•
ROLAP : Relational
•
HOLAP :
Hybrid