You are on page 1of 19

1

Lunds Universitet-Informatik-Erdogan Uan


Selection
Projection
Set operationer
Cartesian product R x S
Union R S
Intersection : R S
Difference : R - S, S - R
Division : R S
Join
equi
theta
natural
inner
left / right outer
full outer
semi
Relationsalgebra operationer
Input: mngd
Output: mngd
2
Lunds Universitet-Informatik-Erdogan Uan
Selection Projection
a b
c d
e f
a b 1 2
a b 3 4
c d 1 2
c d 3 4
e f 1 2
e f 3 4
1 2
3 4
X
=
S : 3 tuplar och 2 kolumner
R : 2 tupel och 2 kolumner
S * R
3 * 2 = tuplar
2 + 2 = 4 kolumner
Relationsalgebra operationer
Kartesisk Produkt
3
Lunds Universitet-Informatik-Erdogan Uan

=
-
=

=
a 1
a 2
b 1
b 3
c 1
c 2
1
2
a
c
=
Division
Set operationer
studenter som lser alla kurser som Anna lser
4
Lunds Universitet-Informatik-Erdogan Uan
CREATE TABLE anst(pnr char(11),
namn char(20),
adress char(30),
lon number(9,2), -- Oracle , float in SQL Server
ansttid date, -- Oracle , Datetime in SQL Server
avdnr char(11),
CONSTRAINT PK_ANST PRIMARY KEY(pnr),
CONSTRAINT FK_ANST FOREIGN KEY (avdnr)
REFERENCES avd(avdnr))
CREATE TABLE anst(pnr char(11),
namn char(20),
adress char(30),
lon number(9,2), -- Oracle , float in SQL Server
ansttid date, -- Oracle , Datetime in SQL Server
avdnr char(11),
CONSTRAINT PK_ANST PRIMARY KEY(pnr),
CONSTRAINT FK_ANST FOREIGN KEY (avdnr)
REFERENCES avd(avdnr))
SQL
CREATE TABLE avd(avdnr char(11),
namn char(20),
adress char(30),
CONSTRAINT PK_AVD PRIMARY KEY(avdnr))
CREATE TABLE avd(avdnr char(11),
namn char(20),
adress char(30),
CONSTRAINT PK_AVD PRIMARY KEY(avdnr))
DDL Skapa tabell
vningstabeller
5
Lunds Universitet-Informatik-Erdogan Uan
insert into avd values('A1','Kemi','Lund')
insert into avd values('A2','Data','Malm')
insert into avd values('A3','Ekonomi','Lund')
insert into anst values('1','hans', 'Lund ', 10000,'22-JUL-98','A1')
insert into anst values('2','eva', 'Malm',20000,'20-JUL-97','A2')
insert into anst values('3','eva', 'Dalby',12000,'22-APR-98','A1')
insert into anst values('4','anna', 'Malm',19000,'22-DEC-98','A2')
insert into anst values('5','lars', 'Eslv',12000,'22-NOV-98','A3')
insert into avd values('A1','Kemi','Lund')
insert into avd values('A2','Data','Malm')
insert into avd values('A3','Ekonomi','Lund')
insert into anst values('1','hans', 'Lund ', 10000,'22-JUL-98','A1')
insert into anst values('2','eva', 'Malm',20000,'20-JUL-97','A2')
insert into anst values('3','eva', 'Dalby',12000,'22-APR-98','A1')
insert into anst values('4','anna', 'Malm',19000,'22-DEC-98','A2')
insert into anst values('5','lars', 'Eslv',12000,'22-NOV-98','A3')
SQL
DDL insttning
vningstabeller
6
Lunds Universitet-Informatik-Erdogan Uan
SQL
CREATE TABLE Student
(spnr VARCHAR(5) NOT NULL,
sname VARCHAR(10),
sadress VARCHAR(10),
stel VARCHAR(10),
CONSTRAINT STUDENT_PK PRIMARY KEY(spnr))
CREATE TABLE kurs
(kkod VARCHAR(5) NOT NULL,
kname VARCHAR(10),
kadress VARCHAR(10),
poang integer,
CONSTRAINT KURS_PK PRIMARY KEY(kkod))
CREATE TABLE laser
(spnr VARCHAR(5) NOT NULL,
kkod VARCHAR(5) NOT NULL,
betyginteger,
CONSTRAINT LASER_PK PRIMARY KEY(spnr,kkod),
CONSTRAINT LASERSPNR_FK FOREIGN KEY(spnr)
REFERENCES Student(spnr),
CONSTRAINT LASERKKOD_FK FOREIGN KEY(kkod)
REFERENCES Kurs(kkod))
CREATE TABLE Student
(spnr VARCHAR(5) NOT NULL,
sname VARCHAR(10),
sadress VARCHAR(10),
stel VARCHAR(10),
CONSTRAINT STUDENT_PK PRIMARY KEY(spnr))
CREATE TABLE kurs
(kkod VARCHAR(5) NOT NULL,
kname VARCHAR(10),
kadress VARCHAR(10),
poang integer,
CONSTRAINT KURS_PK PRIMARY KEY(kkod))
CREATE TABLE laser
(spnr VARCHAR(5) NOT NULL,
kkod VARCHAR(5) NOT NULL,
betyginteger,
CONSTRAINT LASER_PK PRIMARY KEY(spnr,kkod),
CONSTRAINT LASERSPNR_FK FOREIGN KEY(spnr)
REFERENCES Student(spnr),
CONSTRAINT LASERKKOD_FK FOREIGN KEY(kkod)
REFERENCES Kurs(kkod))
DDL Skapa tabell
vningstabeller
7
Lunds Universitet-Informatik-Erdogan Uan
insert into Student values('P1','anna','lund','046-111') ;
insert into Student values('P2','anna','malm','040-111') ;
insert into Student values('P3','eva','lund','046-222');
insert into Student values('P4','eva','malm','040-222');
insert into Student values('P5','hans','eslv','042-111');
insert into Kurs values('K1','dat001','lund',10) ;
insert into Kurs values('K2','dat002','lund',5) ;
insert into Kurs values('K3','dat003','malm',20);
insert into Kurs values('K4','inf001','lund',10);
insert into Kurs values('K5','inf002','lund',5);
insert into Kurs values('K6','inf003','malm',20);
insert into Kurs values('K7','inf004','dalby',20);
insert into Laser values('P1','K1',6);
insert into Laser values('P1','K2',7);
insert into Laser values('P1','K3',8);
insert into Laser values('P1','K4',6);
insert into Laser values('P2','K1',7);
insert into Laser values('P2','K2',9);
insert into Laser values('P2','K3',7);
insert into Laser values('P3','K1',6);
SQL
DDL insttning
vningstabeller
8
Lunds Universitet-Informatik-Erdogan Uan
SQL
Anstlld

pnr namn adress lon ansttid avdnr
1 hans Lund 10000 22-JUL-98 A1
2 eva Malm 20000 20-JUL-97 A2
3 eva Dalby 12000 22-APR-98 A1
4 anna Malm 19000 22-DEC-98 A2
5 lars Eslv 1200 22-NOV-98 A3

SELECT namn, avdnr
FROM anst
namn avdnr
-------- -----------
hans A1
eva A2
eva A1
anna A2
lars A3
SELECT namn, avdnr
FROM anst
WHERE avdnr = A1
namn avdnr
-------- -----------
hans A1
eva A1
SELECT spnr, betyg * 1.5 NyBetyg
FROM laser
WHERE spnr = 'P3'
Projektion
Selection och
projektion
berkning med flt
DML: Projektion-Selection
spnr NyBetyg
----- ---------------
P3 9.0
9
Lunds Universitet-Informatik-Erdogan Uan

Relationsoperatorer : >, <, !=, >=, <=, =


Logiska operatorer: AND, OR, NOT
SELECT *
FROM anst
WHERE lon > 15000
SELECT namn
FROM anst
WHERE lon != 12000
SELECT namn
FROM anst
WHERE lon > 12000
AND lon < 20000
SELECT namn
FROM anst
WHERE lon > 12000
OR adress ='Lund'
SQL Operatorer
10
Lunds Universitet-Informatik-Erdogan Uan
SELECT namn
FROM anst
WHERE namn LIKE 'e%'
SELECT *
FROM kurs
WHERE kadress LIKE 'lu_d%'
SELECT *
FROM kurs
WHERE kadress LIKE '%lund%'
SELECT namn
FROM anst
WHERE namn LIKE 'e%'
SELECT *
FROM kurs
WHERE kadress LIKE 'lu_d%'
SELECT *
FROM kurs
WHERE kadress LIKE '%lund%'
Andra ops: LIKE,IN, ANY,SOME,EXISTS,BETWEEN, ISNULL
SQL -Operatorer
-- vilket tecken som helst i stllet fr _
-- vilken adress innehller lund ?
-- alla namn som brjar med e
SQL Operatorer
11
Lunds Universitet-Informatik-Erdogan Uan
SELECT namn
FROM anst
WHERE adress IN ('lund', 'malm')
INSERT INTO kurs VALUES('K31',NULL,'eslv',15)
SELECT *
FROM kurs
WHERE kname = NULL
0 rows selected
SELECT *
FROM kurs
WHERE kname IS NULL
KKOD KNAME KADRESS POANG
----- ---------- ---------- ----------
K31 NULL eslv 15
1 row selected.
SQL Operatorer
12
Lunds Universitet-Informatik-Erdogan Uan
SELECT COUNT(*) AS ANTALANSTLLDA
FROM anst
ANTALANSTLLDA
--------------
5
SQL
SELECT COUNT(*) AS ANTALANSTLLDA
FROM anst
WHERE avdnr = 'A1'
ANTALANSTLLDA
--------------
2
MAX, MIN, AVG, SUM, COUNT
SELECT MAX(pnr)
FROM anst
--------------
5
Standart funktioner - aggregate
COUNT(*) rknar ven null
SUM, AVG, COUNT, MAX, och
MIN ignorer null vrde
create table test
(id int, name varchar(10))
insert into test values(1, 'eva')
insert into test values(null,'eva')
insert into test values(null,'anna')
select count(id) from test -- 1
select count(*) from test -- 3
select distinct * from test -- 3
select distinct name from test -- 2
13
Lunds Universitet-Informatik-Erdogan Uan
ORDER BY clause
SELECT *
FROM laser
ORDER BY 1 -- kolumn number
SELECT *
FROM laser
ORDER BY 1 -- kolumn number
SELECT *
FROM laser
ORDER BY kkod
SELECT *
FROM laser
ORDER BY kkod
SELECT *
FROM laser
ORDER BY kkod,spnr
SELECT *
FROM laser
ORDER BY kkod,spnr
SPNR KKOD BETYG
P1 K1 6
P1 K2 7
P1 K3 8
P1 K4 6
P2 K1 7
P2 K2 9
P2 K3 7
P3 K1 6
spnr kkod betyg
----- ----- -----------
P1 K1 6
P2 K1 7
P3 K1 6
P2 K2 9
P1 K2 7
P1 K3 8
P2 K3 7
P1 K4 6
spnr kkod betyg
----- ----- -----------
P1 K1 6
P2 K1 7
P3 K1 6
P1 K2 7
P2 K2 9
P1 K3 8
P2 K3 7
P1 K4 6
Asc eller desc
Null rknas minst
Sorteringsorder : Collation
14
Lunds Universitet-Informatik-Erdogan Uan
Lite mer...
SET ROWCOUNT 4 -- SET ROWCOUNT 0 tillbaka till normal
SELECT upper(kkod)U, substring(kkod,2,1)S, 'har' H, -betyg B, 2+4 M
FROM laser
U S H B M
---------- ---- ---- ----------- -----------
K1 1 har -6 6
K2 2 har -7 6
K3 3 har -8 6
K4 4 har -6 6
U S H B M
---------- ---- ---- ----------- -----------
K1 1 har -6 6
K2 2 har -7 6
K3 3 har -8 6
K4 4 har -6 6
SELECT top 3 'DB r rolig'
FROM laser
-----------
DB r rolig
DB r rolig
DB r rolig
-----------
DB r rolig
DB r rolig
DB r rolig
SELECT kkod+spnr KkodOchPnr, kkod
FROM laser
KkodOchPnr kkod
------------------ --------
K1P1 K1
K2P1 K2
KkodOchPnr kkod
------------------ --------
K1P1 K1
K2P1 K2
SELECT 'DB r rolig'
FROM laser
WHERE rownum < 4; SQL Server
Oracle
15
Lunds Universitet-Informatik-Erdogan Uan
SELECT ISNULL(name, 'oknd')
FROM test
----------
eva
eva
anna
oknd
----------
eva
eva
anna
oknd
Lite mer...
SELECT DECODE
(sadress,null,'ingen', sadress)
FROM student;
SQL Server
Oracle
16
Lunds Universitet-Informatik-Erdogan Uan
UPDATE anst
SET namn = 'Eva'
WHERE namn = 'eva'
UPDATE anst
SET namn = 'Eva'
DELETE anst
WHERE namn = 'eva'
DELETE anst
update och delete kan ing i J OIN, SUBQUERY osv
som i SELECT
Update och delete
17
Lunds Universitet-Informatik-Erdogan Uan
SELECT AVG(betyg)
FROM laser
SELECT spnr, AVG(betyg)
FROM laser
select spnr, AVG(betyg) from laser
*
ERROR at line 1:
ORA-00937: not a single-group group function
Server: Msg 8118, Level 16, State 1, Line 1 Column 'laser.spnr' is invalid
in the select list because it is not contained in an aggregate function and
there is no GROUP BY clause.
Gruppering
-----------
7
AVG(BETYG) : (single set)
scalar aggregate query
SPNR KKOD BETYG
P1 K1 6
P1 K2 7
P1 K3 8
P1 K4 6
P2 K1 7
P2 K2 9
P2 K3 7
P3 K1 6
18
Lunds Universitet-Informatik-Erdogan Uan
SELECT spnr, AVG(betyg) Genomsnitt , COUNT(betyg) Antal
FROM laser
GROUP BY spnr
spnr : rad vrde
AVG(betyg), COUNT(betyg) : set
vrdevector aggregate query (multiplesets multiplesets )
spnr Genomsnitt Antal
----- ----------- -------
P1 6.75 4
P2 7.6 3
P3 6 1
Gruppering
laser
6 K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6 K4 P1
8 K3 P1
7 K2 P1
6 K1 P1
19
Lunds Universitet-Informatik-Erdogan Uan
SELECT spnr
FROM laser
GROUP BY spnr, kkod, betyg
SELECT spnr,betyg
FROM laser
GROUP BY spnr
SELECT namn,adress
*
ORA-00979: not a GROUP BY expression
--ok allt som finns i GROUP BY lista behver inte finnas i SELECT lista
-- inte ok allt som finns i GROUP BY lista mste finnas i SELECT lista
Gruppering
20
Lunds Universitet-Informatik-Erdogan Uan
SELECT COUNT(*)antal, betyg
FROM laser
GROUP BY betyg
antal betyg
------ ------
3 6
3 7
1 8
1 9
laser
6
K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6
K4 P1
8
K3 P1
7
K2 P1
6 K1 P1
9
K1 P3
8
K3 P1
7
K3 P2
7 K1 P2
7 K2 P1
6 K1 P3
6
K4 P1
6
K1 P1
3
3
1
1
Gruppering
21
Lunds Universitet-Informatik-Erdogan Uan
SELECT COUNT(*)antal,spnr,betyg
FROM laser
GROUP BY spnr,betyg
antal spnr betyg
-------- ----- --------
2 P1 6
1 P3 6
1 P1 7
2 P2 7
1 P1 8
1 P2 9
SELECT COUNT(*)antal,spnr,betyg
FROM laser
GROUP BY betyg,spnr
antal spnr betyg
-------- ----- ------
2 P1 6
1 P1 7
1 P1 8
2 P2 7
1 P2 9
1 P3 6
laser
laser
6 K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6 K4 P1
8 K3 P1
7 K2 P1
6 K1 P1
6 P3
9 P2
8 P1
7 P2
7 P2
7 P1
6 P1
6 P1
6 K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6 K4 P1
8 K3 P1
7 K2 P1
6 K1 P1
6 P3
9 P2
7 P2
7 P2
8 P1
7 P1
6 P1
6 P1
Gruppering
22
Lunds Universitet-Informatik-Erdogan Uan
SELECT sname
FROM student
333 Lund Eva P3
222 Lund Anna P2
111 Lund Anna P1
stel sadress sname spnr
GROUP BY eliminerar dupletter
SELECT DISTINCT sname
FROM student
sname
----------
Anna
Eva
sname
----------
Anna
Anna
eva
sname
----------
Anna
Eva
SELECT sname
FROM student
GROUP BY sname
sname antal
---------- -----------
anna 2
eva 1
SELECT sname, COUNT(*) antal
FROM student
GROUP BY sname
23
Lunds Universitet-Informatik-Erdogan Uan
* WHERE kommer efter FROM
HAVING kommer efter GROUP BY
* WHERE kan inte innehlla aggretages
HAVING kan
* WHERE eliminerar tuplar innan grouping
HAVING eliminerar efter grouping
WHERE vs HAVING
24
Lunds Universitet-Informatik-Erdogan Uan
SELECT SELECT spnr spnr, COUNT(*) , COUNT(*) Antal Antal, ,
MAX(betyg MAX(betyg) Maximum ) Maximum
FROM laser FROM laser
WHERE WHERE betyg betyg > 6 > 6
GROUP BY spnr
SELECT SELECT spnr spnr, COUNT(*) , COUNT(*) Antall Antall, , MAX(betyg MAX(betyg) )
FROM laser FROM laser
GROUP BY GROUP BY spnr spnr
HAVING MAX(betyg) > 6
spnr Antal Maximum
----- ------ -----------
P1 4 8
P2 3 9
spnr Antal MAX
----- ----- -----
P1 4 8
P2 3 9
P3 1 6
spnr Antal Maximum
----- --------- -----------
P1 2 8
P2 3 9
WHERE vs HAVING
laser
6 K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6 K4 P1
8 K3 P1
7 K2 P1
6 K1 P1
7 P2
9 P2
7 P2
8 P1
7 P1
25
Lunds Universitet-Informatik-Erdogan Uan
WHERE vs HAVING
SELECT spnr, count(*) antal, max(betyg) maximum
FROM laser
GROUP BY spnr
HAVING betyg > 6
spnr betyg Antal
----- -------- -----------
P1 6 2
P3 6 1
SELECT spnr, betyg ,COUNT(*) Antal
FROM laser
GROUP BY spnr,betyg
HAVING betyg =6
Betyg not contained in either an aggregate
function or the GROUP BY clause.
laser
6 K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6 K4 P1
8 K3 P1
7 K2 P1
6 K1 P1
26
Lunds Universitet-Informatik-Erdogan Uan
SELECT spnr, COUNT(*) Antal, AVG(betyg) Gnitt
FROM laser
GROUP BY spnr
HAVING AVG(betyg) > 5
spnr Antal Gnitt
----- ----------- -----------
P1 4 6.75
P2 3 7.6
P3 1 6
WHERE vs HAVING
SELECT spnr, count(*) antal, max(betyg) maximum
FROM laser
GROUP BY spnr
HAVING spnr > P1
spnr antal maximum
----- ----------- -----------
P2 3 9
P3 1 6
laser
6 K1 P3
7 K3 P2
9 K2 P2
7 K1 P2
6 K4 P1
8 K3 P1
7 K2 P1
6 K1 P1
27
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student, laser
Kartesisk produkt Kartesisk produkt: :
student * laser
3* 3 =9 rader
4 +3 =attribut
Student Laser
333 Lund Eva P3
222 Malm Anna P2
111 Lund Anna P1
stel sadress sname spnr
5 K2 P1
6 K1 P3
4 K1 P1
betyg kkod spnr
6 K1 P3 333 Lund Eva P3
5 K2 P1 333 Lund Eva P3
4 K1 P1 333 Lund Eva P3
6 K1 P3 222 Malm Anna P2
5 K2 P1 222 Malm Anna P2
4 K1 P1 222 Malm Anna P2
6 K1 P3 111 Lund Anna P1
5 K2 P1 111 Lund Anna P1
4 K1 P1 111 Lund Anna P1
betyg kkod spnr stel sadress sname spnr
Kartesisk produkt
28
Lunds Universitet-Informatik-Erdogan Uan
spnr : JOIN kolumn; spnr : JOIN kolumn;
JOIN kompatibel JOIN kompatibel
Vanligtvis PK och/elller FK Vanligtvis PK och/elller FK
SELECT *
FROM student s, laser l
WHERE s.spnr = l.spnr
6 K1 P3 333 Lund Eva P3
5 K2 P1 333 Lund Eva P3
4 K1 P1 333 Lund Eva P3
6 K1 P3 222 Malm Anna P2
5 K2 P1 222 Malm Anna P2
4 K1 P1 222 Malm Anna P2
6 K1 P3 111 Lund Anna P1
5 K2 P1 111 Lund Anna P1
4 K1 P1 111 Lund Anna P1
betyg kkod spnr stel sadress sname spnr
6 K1 P3 333 Lund Eva P3
5 K2 P1 111 Lund Anna P1
4 K1 P1 111 Lund Anna P1
betyg kkod spnr stel sadress sname spnr
KP +
selektion
Join
29
Lunds Universitet-Informatik-Erdogan Uan
SELECT s.*, l.kkod, l.betyg
FROM student s, laser l
WHERE s.spnr = l.spnr
Student Laser
6 K1 333 Lund Eva P3
5 K2 111 Lund Anna P1
4 K1 111 Lund Anna P1
Betyg kkod stel sadress sname spnr
333 Lund Eva P3
222 Malm Anna P2
111 Lund Anna P1
stel sadress sname spnr
6 K1 P3
5 K2 P1
4 K1 P1
Betyg kkod spnr
Natural Join
SELECT s.*, l.kkod, l.betyg
FROM student2 s INNER JOIN laser2 l
ON s.spnr = l.spnr
-- Samma resultat :
SQL 92 ; INNER behvs inte;
default
30
Lunds Universitet-Informatik-Erdogan Uan
Frst KP
sedan
tillmpa
join vilkor
6 K1 P3 333 Lund Eva P3
5 K2 P1 333 Lund Eva P3
4 K1 P1 333 Lund Eva P3
6 K1 P3 222 Malm Anna P2
5 K2 P1 222 Malm Anna P2
4 K1 P1 222 Malm Anna P2
6 K1 P3 111 Lund Anna P1
5 K2 P1 111 Lund Anna P1
4 K1 P1 111 Lund Anna P1
betyg kkod spnr stel sadress sname spnr
Theta Join spnr spnr
----- -----
P2 P1
P2 P1
P3 P1
P3 P1
Theta Join
SELECT s.spnr,l.spnr
FROM student s, laser l
WHERE s.spnr > l.spnr
31
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student s1, student s2
WHERE s1.sadress = s2.sadress
Student Student
Malm Anna P2 Malm Anna P2
Lund Anna P1 Lund Eva P3
Lund Eva P3 Lund Eva P3
Lund Eva P3 Lund Anna P1
Lund Anna P1 Lund Anna P1
Lund Eva P3
Malm Anna P2
Lund Anna P1
sadress sname spnr
Lund Eva P3
Malm Anna P2
Lund Anna P1
sadress sname spnr
Sjlv join
Vilka personer bor p samma adress ?
-- ger samma relation; hjlper inte !
SELECT *
FROM student s
WHERE s.sadress = s.sadress
spnr sname sadress
----- -------- ------------
P1 Anna Lund
P2 Anna Malm
P3 Eva Lund
32
Lunds Universitet-Informatik-Erdogan Uan
SELECT s1.spnr, s1.sname, s1.sadress
FROM student s1, student s2
WHERE s1.sadress = s2.sadress
AND s1.spnr != s2.spnr
spnr sname sadress
----- ---------- ----------
P1 Anna Lund
P3 Eva Lund
Sjlv join
-- hjlper inte !
SELECT distinct s1.spnr,
s1.sname, s1.sadress
FROM student s1, student s2
WHERE s1.sadress = s2.sadress
spnr sname sadress
----- -------- ------------
P1 Anna Lund
P2 Anna Malm
P3 Eva Lund
33
Lunds Universitet-Informatik-Erdogan Uan
anstnr anstamn anstadress ln chefnr
--------- ---------- ---------- ---- -------
A1 Anna Lund 10 NULL
A2 Eva Lund 20 A1
A3 Anna Malm 5 A2
A4 Lars Eslv 15 A2
A5 Hans Dalby 8 A3
anstnr chefnr Cln Aln
-------- -------- ------ ----
A2 A1 10 20
A5 A3 5 8
SELECT arbetare.anstnr,arbetare.chefnr,
chef.ln Cln, arbetare.ln Aln
FROM anst arbetare, anst chef
WHERE arbetare.chefnr = chef.anstnr
AND chef.ln < arbetare.ln
Sjlv join - Rekursiv
Vem tjnar mer n sin chef ?
anstnr anstamn anstadress ln chefnr
--------- ---------- ---------- ---- -------
A1 Anna Lund 10 NULL
A2 Eva Lund 20 A1
A3 Anna Malm 5 A2
A4 Lars Eslv 15 A2
A5 Hans Dalby 8 A3
34
Lunds Universitet-Informatik-Erdogan Uan
anstnr anstamn anstadress ln chefnr
--------- ---------- ---------- ---- -------
A1 Anna Lund 10 NULL
A2 Eva Lund 20 A1
A3 Anna Malm 5 A2
A4 Lars Eslv 15 A2
A5 Hans Dalby 8 A3
SELECT arbetare.anstnr,
arbetare.chefnr
FROM anst arbetare, anst chef
WHERE arbetare.chefnr = chef.anstnr
AND arbetare.anstnr = 'A2
Sjlv join
anstnr anstamn anstadress ln chefnr
--------- ---------- ---------- ---- -------
A1 Anna Lund 10 NULL
A2 Eva Lund 20 A1
A3 Anna Malm 5 A2
A4 Lars Eslv 15 A2
A5 Hans Dalby 8 A3
-- A2 chef : A2 roll: arbetare
anstnr chefnr
---------- ----------
A2 A1
SELECT arbetare.anstnr,
arbetare.chefnr,
FROM anst arbetare, anst chef
WHERE arbetare.chefnr = chef.anstnr
AND chef.anstnr = 'A2
-- A2 anstllda: A2 roll: chef
anstnr chefnr
---------- ----------
A3 A2
A4 A2
Sjlv join
35
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student s, kurs k
WHERE s.sadress = k.kadress
Student Kurs
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
5 Malm INF052 K3
10 Dalby INF051 K2
10 Lund INF001 K1
5 Malm INF052 K3 333 Malm Eva P3
10 Lund INF001 K1 111 Lund Anna P1
Left-right outer join
36
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student s, kurs k
WHERE s.sadress *= k.kadress
SELECT *
FROM student s, kurs k
WHERE s.sadress =* k.kadress
*=, =* SQL SERVER (+) ORACLE
SELECT *
FROM student s, kurs k
WHERE s.sadress (+) = k.kadress
5 Malm INF052 K3 333 Malm Eva P3
10 Dalby INF051 K2 null null null null
10 Lund INF001 K1 111 Lund Anna P1
5 Malm INF052 K3 333 Malm Eva P3
Null Null Null Null 222 Eslv Anna P2
10 Lund INF001 K1 111 Lund Anna P1
Left-right outer join
37
Lunds Universitet-Informatik-Erdogan Uan
SELECT s.spnr,s.sadress, k.kkod, k.kadress
FROM student s LEFT OUTER JOIN kurs k
ON s.sadress = k.kadress
-- eller ; samma resultat
SELECT *
FROM kurs k RIGHT OUTER JOIN student s
ON s.sadress = k.kadress
SELECT s.spnr,s.sadress, k.kkod, k.kadress
FROM student2 s RIGHT OUTER JOIN kurs2 k
ON s.sadress = k.kadress
-- eller ; samma resultat
SELECT s.spnr,s.sadress, k.kkod, k.kadress
FROM kurs2 k LEFT OUTER JOIN student2 s
ON s.sadress = k.kadress
SQL 92 SQL 92 SQL 92
Left-right outer join
Spnr sadress kkod kadress
----- ---------- ----- ----------
P1 lund K1 lund
P2 Eslv NULL NULL
P3 Malm K3 Malm
spnr sadress kkod kadress
----- ---------- ----- ----------
P1 lund K1 lund
NULL NULL K2 Dalby
P3 Malm K3 Malm
38
Lunds Universitet-Informatik-Erdogan Uan
SELECT k.kkod,k.kadress,s.spnr, s.sadress
FROM kurs2 k FULL OUTER JOIN student2 s
ON s.sadress = k.kadress
kkod kadress spnr sadress
----- ---------- ----- ----------
K1 lund P1 lund
NULL NULL P2 Eslv
K3 Malm P3 Malm
K2 Dalby NULL NULL
Full outer join
39
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student
WHERE spnr ='P1'
--Vem har samma namn
-- som person P1 ?
spnr sname sadress stel
----- -------- -------- -----
P1 eva dalby 111
SELECT *
FROM student
WHERE sname =eva
SELECT *
FROM student
WHERE sname =
(SELECT sname
FROM student
WHERE spnr ='P1')
SELECT *
FROM laser
WHERE betyg =
(SELECT MAX(betyg)
FROM laser)
SELECT MAX(betyg)
FROM laser
SELECT *
FROM laser
WHERE betyg = 9
-- Vem har hgsta betyg ?
MAX(BETYG)
----------
9
Subqueries
40
Lunds Universitet-Informatik-Erdogan Uan
SUBQ som returnerar mer n ett vrde:
SELECT *
FROM student
WHERE sadress =
(SELECT kadress
FROM kurs
WHERE kadress ='lund')
ORA-01427: single-row subquery returns more than one row
Subqueries
Enbart en tupel om dessa relationsoperatorer anvnds :=, !=, <, <= , >, >=
41
Lunds Universitet-Informatik-Erdogan Uan
SELECT spnr
FROM student
WHERE spnr IN
( P1,P1,P1,P2, P2,P3)
SELECT spnr
FROM student
WHERE spnr IN
( P1,P1,P1,P2, P2,P3)
--Inner Query
SELECT spnr
FROM laser
Student
Laser
P1
P1
P1
P2
P2
P3
SELECT spnr
FROM student
WHERE spnr IN
SELECT spnr
FROM student
WHERE spnr IN
Outer
Query
Inner
Query
(SELECT spnr
FROM laser)
(SELECT spnr
FROM laser)
333 Malm Eva P3
444 Malm Eva P4
555 Eslv Hans P5
222 Eslv Anna P2
111 Lund Anna P1
stel sadress sname spnr
6 K1 P3
9 K2 P2
7 K1 P2
8 K3 P1
7 K2 P1
6 K1 P1
Betyg kkod spnr
Spnr
----
P1
P2
P3
V
a
d

r

f
r

g
a
n

?
Subqueries
42
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student
WHERE spnr IN
(SELECT spnr
FROM laser)
spnr sname sadress stel
------ --------- ------------ -------
P1 Anna Lund 111
P2 Anna Eslv 222
P3 Eva Malm 333
SELECT *
FROM student s ,laser l
WHERE s.spnr = l.spnr
spnr sname sadress stel spnr kkod betyg
----- ------- ---------- ----- ----- ----- -------
P1 anna lund 111 P1 K1 6
P1 anna lund 111 P1 K2 7
P1 anna lund 111 P1 K3 8
P2 anna eslv 222 P2 K1 7
P2 anna eslv 222 P2 K2 9
P3 eva malm 333 P3 K1 6
DISTINCT s.* ger samma resultat som ovan
Subqueries
IN tolkas som Intersection ; mngd operation
43
Lunds Universitet-Informatik-Erdogan Uan
-- Vem fick betyg 6 ?
SELECT *
FROM student
WHERE spnr IN
(SELECT spnr
FROM laser
WHERE betyg = 5)
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = s.spnr)
Inner Query r oberoende.
Evauleras frst.
Passar resultatet till outer Q uery.
Inner Query r beroende av
outer Query. Behver vrde
frn outer Query.
Passar resultatet till outer Query.
Correlaterade Queries
44
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = s.spnr)
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = P1)
betyg
------
6
7
8
SELECT *
FROM student s
WHERE 5 IN (6,7,8)
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = P2)
betyg
------
7
9
SELECT *
FROM student s
WHERE 5 IN (7,9)
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = P3)
betyg
------
6 SELECT *
FROM student s
WHERE 5 IN (6)
1
2
3
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
stel sadress snamn spnr
6 K1 P3
9 K2 P2
7 K1 P2
8 K3 P1
7 K2 P1
6 K1 P1
Betyg kkod spnr
Correlaterade Queries
45
Lunds Universitet-Informatik-Erdogan Uan
-- Rader somhar anna
-- Dum men kul lsning
SELECT *
FROM student
WHERE sname IN
(SELECT sname
FROM student
WHERE sname = 'anna')
-- --studenter studenter som som inte inte l l ser ser n n gra gra kurser kurser -- -- DIFFERENCE ( student DIFFERENCE ( student laser) laser)
SELECT *
FROM student
WHERE spnr NOT IN
(SELECT spnr
FROM laser)
-- Frga ?
SELECT *
FROM kurs
WHERE kkod NOT IN
(SELECT kkod
FROM laser)
Mngd operationer
-- en annan dumlsning
SELECT s1.spnr, s1.sname, s2.spnr
FROM student s1, student s2
WHERE s1.sname = 'anna'
AND s2.spnr = s1.spnr
SELECT *
FROM student
WHERE sname = 'anna'
46
Lunds Universitet-Informatik-Erdogan Uan
Spnr sname sadr stel kkod kname kadress poang spnr kkod
----- ------- ------- ------- -------- ---------- ----------- --------- --------- ---------
P1 Anna Lund 111 K1 INF001 lund 10 P1 K1
P1 Anna Lund 111 K2 INF051 Dalby 10 P1 K2
P1 Anna Lund 111 K3 INF052 Malm 5 P1 K3
P2 Anna Eslv 222 K1 INF001 Lund 10 P2 K1
P2 Anna Eslv 222 K2 INF051 Dalby 10 P2 K2
P3 Eva Malm 333 K1 INF001 Lund 10 P3 K1
SELECT *
FROM student s
WHERE spnr IN
(SELECT spnr
FROM laser)
spnr sname sadress stel
----- ---------- ---------- --------
P1 Anna Lund 111
P2 Anna Eslv 222
P3 Eva Malm 333
SELECT *
FROM student s, kurs k, laser l
WHERE l.kkod = k.kkod
AND l.spnr = s.spnr
IN vs Join
47
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student
WHERE spnr NOT IN
( SELECT spnr
FROM laser)
SELECT *
FROM student s
WHERE NOT EXISTS
( SELECT spnr
FROM laser l
WHERE l.spnr = s.spnr)
SELECT *
FROM student -- P1 frn student
WHERE NOT EXISTS
( SELECT spnr
FROM laser
WHERE spnr = P1) -- false
spnr
-----
P1
P1
P1
SELECT *
FROM student -- P2 frn student
WHERE NOT EXISTS
( SELECT spnr
FROM laser
WHERE spnr = P2) -- false
spnr
-----
P2
P2
1
2
IN vs EXISTS
6 K1 P3
9 K2 P2
7 K1 P2
7 K2 P1
8 K3 P1
6 K1 P1
betyg kkod spnr
333 Malm Eva P3
444 Malm Eva P4
222 Eslv Anna P2
111 Lund Anna P1
stel sadress sname spnr
48
Lunds Universitet-Informatik-Erdogan Uan
SELECT * SELECT *
FROM student FROM student P3 frn student
WHERE NOT EXISTS WHERE NOT EXISTS
( SELECT ( SELECT spnr spnr
FROM laser FROM laser
WHERE WHERE spnr spnr = = P3 P3 ) ) -- -- false false
spnr
-----
P3
3
6 K1 P3
9 K2 P2
7 K1 P2
7 K2 P1
8 K3 P1
6 K1 P1
betyg kkod spnr
333 Malm Eva P3
444 Malm Eva P4
222 Eslv Anna P2
111 Lund Anna P1
stel sadress sname spnr
SELECT * SELECT *
FROM student FROM student P4 frn student
WHERE NOT EXISTS WHERE NOT EXISTS
( SELECT ( SELECT spnr spnr
FROM laser FROM laser
WHERE WHERE spnr spnr = = P4 P4 ) ) -- -- true true
spnr
-----
tom
4
--svaret
spnr sname sadress stel
----- ---------- ---------- ----------
P4 eva malm 040-222
IN vs EXISTS
49
Lunds Universitet-Informatik-Erdogan Uan
lser
kurs
student
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
stel sadress sname spnr
6 K1 P3
9 K2 P2
7 K1 P2
7 K2 P1
8 K3 P1
6 K1 P1
betyg kkod spnr
5 Malm INF052 K3
10 Dalby INF051 K2
10 Lund INF001 K1
Poang kadress knamn kkod
IN vs EXISTS vs Join
50
Lunds Universitet-Informatik-Erdogan Uan
SELECT distinct s.spnr, s.*
FROM student s, kurs k, laser l
WHERE s.spnr = l.spnr
AND k.kkod = l.kkod
SELECT *
FROM student s
WHERE spnr IN
(SELECT spnr
FROM laser
WHERE kkod IN
(SELECT kkod
FROM kurs) )
SELECT *
FROM student s
WHERE EXISTS
(SELECT spnr
FROM laser l
WHERE EXISTS
(SELECT kkod
FROM kurs k
WHERE kkod = l.kkod
AND s.spnr = l.spnr) )
spnr sname sadress stel
----- ----- ---------- ------
P1 anna lund 111
P2 anna eslv 222
P3 eva malm 333
-- eller kurs uppgifter
SELECT *
FROM kurs
WHERE kkod IN
(SELECT kkod
FROM laser
WHERE spnr IN
(SELECT spnr
FROM student) )
-- eller kurs uppgifter
SELECT *
FROM kurs
WHERE kkod IN
(SELECT kkod
FROM laser
WHERE spnr IN
(SELECT spnr
FROM student) )
IN vs EXISTS vs Join
51
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM student s
WHERE NOT EXISTS
(SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND s.spnr = l.spnr))
SELECT s.spnr, s.sname, COUNT(*)
FROM student s, laser l
WHERE s.spnr = l.spnr
GROUP BY s.spnr, s.sname
HAVING COUNT(kkod) =
(SELECT count(kkod)
FROM kurs)
spnr sname COUNT
----- -------- --------
P1 anna 3
P2 anna 2
P3 eva 1
3
EXISTS Fr starka nerver -verkurs
Studenter som lser alla kurser
A
B
52
Lunds Universitet-Informatik-Erdogan Uan
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
( SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND l.spnr = 'P1')
(0 row(s) affected)
EXISTS Fr starka nerver -verkurs
SELECT *
FROM student s
WHERE NOT EXISTS
(SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND s.spnr = l.spnr))
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= 'K1'
AND l.spnr = 'P1')
spnr kkod betyg
----- ----- -----------
P1 K1 6
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= K2
AND l.spnr = 'P1')
spnr kkod betyg
----- ----- -----------
P1 K2 7
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= K3
AND l.spnr = 'P1')
spnr kkod betyg
----- ----- -----------
P1 K3 8
5 Malm INF052 K3
10 Dalby INF051 K2
10 Lund INF001 K1
Poang kadress knamn kkod
Lser P1 K1 ?
Lser P1 K2 ?
Lser P1 K3 ?
P1 existerar (lser) i alla
kurser som finns i kurs
Kurs
53
Lunds Universitet-Informatik-Erdogan Uan
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND l.spnr = 'P3')
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND l.spnr = 'P3')
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND l.spnr = 'P2')
SELECT kkod
FROM kurs k
WHERE NOT EXISTS
(SELECT *
FROM laser l
WHERE l.kkod= k.kkod
AND l.spnr = 'P2')
kkod
-----
K3
kkod
-----
K2
K3
EXISTS Fr starka nerver-verkurs
P2 existerar inte i mngden
(lser) fr kursen K3 : P2 lser
inte K3
P3 lser inte K2 och K3
6 K1 P3
9 K2 P2
7 K1 P2
7 K2 P1
8 K3 P1
6 K1 P1
betyg kkod spnr
54
Lunds Universitet-Informatik-Erdogan Uan
NAMN
----------
anna
eva
INF001
INF051
INF052
Student
Kurs
Dupletter (anna) elimineras.
UNION ALL behllerdupletter
SELECT sname NAMN
FROM student
UNION [ALL]
SELECT kname
FROM kurs
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
5 Malm INF052 K3
10 Dalby INF051 K2
10 Lund INF001 K1
Mngd operationer - UNION
UNION kompatibilitet:
kolumner som ingr I UNION
mste vara av samma typ eller
klarar implicit konvertering
55
Lunds Universitet-Informatik-Erdogan Uan
KOD NAMN ADRESS
----- ---------- ----------
K1 INF001 lund
K3 INF052 malm
P1 anna lund
P3 eva malm
SELECT spnr KOD , sname NAMN, sadress ADRESS
FROM student
WHERE sadress IN ('lund', 'malm')
UNION
SELECT kkod, kname, kadress
FROM kurs
WHERE kadress IN ('lund', 'malm')
UNION
Student
Kurs
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
5 Malm INF052 K3
10 Dalby INF051 K2
10 Lund INF001 K1
56
Lunds Universitet-Informatik-Erdogan Uan
SELECT *
FROM laser
WHERE betyg< 5
OR (betyg> 6 AND betyg< 8)
STEP 1
The type of query is SELECT
FROM TABLE
laser
Nested iteration
Table Scan
-- samma resultat , men olika exekveringsplan
SELECT *
FROM laser
WHERE betyg< 5
UNION
SELECT *
FROM laser
WHERE betyg> 6
AND betyg< 8
STEP 1
The type of query is INSERT
The update mode is direct
FROM TABLE
laser
Nested iteration
Index : LASER_BETYG_INDEX
TO TABLE
Worktable 1
STEP 2
The type of query is INSERT
The update mode is direct
FROM TABLE
laser
Nested iteration
Index : LASER_BETYG_INDEX
TO TABLE
Worktable 1
STEP 3
The type of query is SELECT
This step involves sorting
FROM TABLE
Worktable 1
Using GETSORTED Table Scan
UNION
6 K1 P3
9 K2 P2
7 K1 P2
7 K2 P1
8 K3 P1
6 K1 P1
betyg kkod spnr
aser
57
Lunds Universitet-Informatik-Erdogan Uan
SELECT sadress
FROM student
INTERSECT
SELECT kadress
FROM kurs
kadress
----------
lund
malm
SELECT kadress
FROM kurs
WHERE kadress IN
(SELECT sadress
FROM student)
-- Eller EXISTS
Student
Kurs
-- ORACLE --Alla RDBMS
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
5 Malm INF052 K3
10 Dalby INF051 K2
10 Lund INF001 K1
Oracle vs SQL Server - Intersection
58
Lunds Universitet-Informatik-Erdogan Uan
SELECT sadress
FROM student
MINUS
SELECT kadress
FROM kurs
SELECT kadress
FROM kurs
MINUS
SELECT sadress
FROM student
SELECT kadress
FROM kurs
WHERE kadress NOT IN
(SELECT sadress
FROM student)
-- Eller NOT EXISTS sadr ess
- - - - - - - - - -
esl v
kadr ess
- - - - - - - - - -
dal by
--Alla RDBMS -- ORACLE
Oracle vs SQL Servern - Difference
59
Lunds Universitet-Informatik-Erdogan Uan
-- anvnd alias
SELECT sname , l.*
FROM student s, laser l
SELECT Nybetyg = betyg * 2, spnr --SQL server
FROM laser
ORDER BY nybetyg
SELECT betyg * 2 [AS] nybetyg , spnr -- Alla
FROM laser
ORDER BY nybetyg
! Anvnd standard !
Lite mer ...
60
Lunds Universitet-Informatik-Erdogan Uan
INSERT INTO studentcopy
SELECT *
FROM student
INSERT INTO kurs(kkod,kadress)
SELECT spnr, sadress
FROM student
CREATE TABLE test (name varchar(10))
INSERT INTO test
SELECT kname
FROM kurs
-- ej standart SQL Server
UPDATE kurs
SET poang =
case
when poang = 12 then poang + 1
when poang = 7 then poang + 2
else poang
end
kr standart
alternativa lsningar
praktiska lsningar
Lite mer ...
61
Lunds Universitet-Informatik-Erdogan Uan
-- ta bort studenter som inte lser ngon kurs
DELETE student
WHERE NOT EXISTS
( SELECT spnr
FROM laser
WHERE spnr = student.spnr)
Lite lite till...
-- Vad r frgan ?
UPDATE student
set sname = ' BRA'
WHERE spnr IN
( SELECT spnr
FROM laser l)
62
Lunds Universitet-Informatik-Erdogan Uan
Frenkla komplexa queries; gmma underliggande databas objekt
Skerhet; begrnsa tkomst till vissa
- Kolumner ( vertikal)
- Rader (horizontal)
* Tupel aggregation
View
63
Lunds Universitet-Informatik-Erdogan Uan
CREATE VIEW LaserView AS
SELECT s.spnr, s.sname
FROM student s, laser l
WHERE s.spnr = l.spnr
SELECT *
FROM LaserView
View
spnr sname betyg
----- -------- -------
P1 anna 6
P1 anna 7
P1 anna 8
P2 anna 7
P2 anna 9
P3 eva 6
SELECT spnr, sname, betyg
FROM LaserView -- ok
SELECT sadress
FROM LaserView -- ej ok!
Vyer frenklar komplexa queries;
gmma underliggande databasobjekt
333 Malm Eva P3
222 Eslv Anna P2
111 Lund Anna P1
stel sadress sname spnr
Student
64
Lunds Universitet-Informatik-Erdogan Uan
View
CREATE VIEW VertikalView (myname) AS
SELECT sname
FROM student
SELECT myname
FROM VertikalView -- ok
SELECT sname
FROM VertikalView -- ej ok
synonym
65
Lunds Universitet-Informatik-Erdogan Uan
CREATE VIEW VertikalView AS
SELECT spnr, sname
FROM student --kolumn masking
CREATE VIEW HorizontalView AS
SELECT *
FROM student
WHERE spnr = 'P1 -- rad masking
CREATE VIEW VertikalOchHorizontalView
AS
SELECT sname
FROM student
WHERE spnr = 'P1 -- mixed masking
View horizontal och vertikal
66
Lunds Universitet-Informatik-Erdogan Uan
ALTER TABLE laser DROP CONSTRAINT LASERSPNR_FK
ALTER TABLE student add CONSTRAINT STUDENT_PK PRIMARY KEY (spnr)
Uppdatera View
-- Vyer kan uppdateras med write operationer: INSERT, DELETE, UPDATE
-- insttning i vy pverkar bas tabellen student
INSERT INTO VertikalView VALUES('P99', 'Test')
SELECT *
FROM student
-- Vyer kan uppdateras med write operationer: INSERT, DELETE, UPDATE
-- insttning i vy pverkar bas tabellen student
INSERT INTO VertikalView VALUES('P99', 'Test')
SELECT *
FROM student
spnr sname sadress stel
----- ---------- ---------- ----------
P1 anna lund 111
P2 anna malm 222
P3 eva lund 333
P99 Test NULL NULL
mste vara NULL I kolumn definition
67
Lunds Universitet-Informatik-Erdogan Uan
-- Virtuell vy tabell) ; ORACLE enbart
UPDATE
(SELECT sname
FROM student
WHERE sname='eva') -- resultat av subquery uppdateras
SET sname = 'eva'
ORDER BY kan inte anvndas i vy
-- Aggregate vyer ; kan frbttra prestanda
CREATE VIEW LaserAggregate AS
SELECT spnr,COUNT(*)AntalStudent, MIN(betyg)Min
FROM laser
GROUP BY spnr
SELECT *
FROM LaserAggregate
spnrAntalStud Min
----- ------------ -----------
P1 3 6
P2 2 7
P3 1 6
Aggregate vyer
68
Lunds Universitet-Informatik-Erdogan Uan
BILAGOR
BILAGOR
69
Lunds Universitet-Informatik-Erdogan Uan
Exact Numerics
Integers
bigint
Integer (whole number) data from -2^63 through 2^63-1 int
Integer (whole number) data from -2^31 through 2^31 - 1
smallint
Integer data from 2^15 (-32,768) through 2^15 - 1 (32,767).
tinyint
Integer data from 0 through 255.
bit
bit
Integer data with either a 1 or 0 value.
decimal and numeric
decimal
Fixed precision and scale numeric data from -10^38 +1 through 10^38 1.
numeric
Functionally equivalent to decimal.
SQL Server Datatyper
70
Lunds Universitet-Informatik-Erdogan Uan
money and smallmoney
money
Monetary data values from -2^63 through 2^63 - 1
smallmoney
Monetary data values from -214,748.3648 through +214,748.3647
Approximate Numerics
float
Floating precision number data from -1.79E + 308 through 1.79E + 308.
real
Floating precision number data from -3.40E + 38 through 3.40E + 38.
datetime and smalldatetime
datetime
Date and time data from January 1, 1753, through December 31, 9999smalldatetime
Date and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute.
SQL Server Datatyper
71
Lunds Universitet-Informatik-Erdogan Uan
Character Strings
char
Fixed-length non-Unicode character data with a maximum length of 8,000 characters.
varchar
Variable-length non-Unicode data with a maximum of 8,000 characters.
text
Variable-length non-Unicode data with a maximum length of 2^31 - 1 (2,147,483,647) characters.
Unicode Character Strings
nchar
Fixed-length Unicode data with a maximum length of 4,000 characters.
nvarchar
Variable-length Unicode data with a maximum length of 4,000 characters.
sysname is a system-supplied user-defined data type that is functionally equivalent to nvarchar(128)
and is used to reference database object names.
ntext
Variable-length Unicode data with a maximum length of 2^30 1 characters.
SQL Server Datatyper
72
Lunds Universitet-Informatik-Erdogan Uan
Binary Strings
binary
Fixed-length binary data with a maximum length of 8,000 bytes.
varbinary
Variable-length binary data with a maximum length of 8,000 bytes.
image
Variable-length binary data with a maximum length of 2^31 - 1 (2,147,483,647) bytes.
Other Data Types
cursor
A reference to a cursor.
sql_variant
A data type that stores values of various SQL Server-supported data types,
except text, ntext, timestamp, and sql_variant.
table
A special data type used to store a result set for later processing .
timestamp
A database-wide unique number that gets updated every time a row gets updated.
uniqueidentifier
A globally unique identifier (GUID).
SQL Server Datatyper
73
Lunds Universitet-Informatik-Erdogan Uan
String Functions
These scalar functions perform an operation on a string input value and return a string or numeric value.
ASCII NCHAR SOUNDEX
CHAR PATINDEX SPACE
CHARINDEX REPLACE STR
DIFFERENCE QUOTENAME STUFF
LEFT REPLICATE SUBSTRING
LEN REVERSE UNICODE
LOWER RIGHT UPPER
LTRIM RTRIM
All built-in string functions, except for CHARINDEX and PATINDEX, are deterministic.
They return the same value any time they are called with a given set of input values..
String funktioner