Professional Documents
Culture Documents
Lukács Gergely
Elsődleges és külső kulcs
• Tábla kulcsa (primary key): olyan attribútum(ok), melyek
egyértelműen meghatározzák a sort
• Külső kulcs (foreign key): egy (vagy több) oszlop egy táblában,
amely(ek) egy másik tábla elsődleges kulcsának értékeit
tartalmazzák
• Hivatkozó reláció (referencing relation)
• Hivatkozott reláció (referenced relation)
Rekordok kiválasztása
R reláció
Rekordok kiválasztása,
melyekre teljesül:
A=B and D > 5
σ A=B and D > 5 (r)
• r reláció
r x s:
• r, s relációk
r ∪ s:
• r, s relációk:
r – s:
• r, s relációk:
• r ∩ s
• r és s relációk R és S sémájú relációk.
A két reláció természetes összekapcsolása (natural
join) egy R ∪ S sémájú relációt eredményez, melyet
a következőképpen kapunk meg:
• Az r reláció minden tr rekordját, valamint az s
reláció minden ts rekordját tekintjük.
• Ha a tr és ts minden R ∩ S-be tartozó attribútuma
értéke egyezik, akkor t rekord az eredménybe
beletartozik. Értékei az r-en a tr, , az s-en a tr –
értékeivel egyeznek.
• r, s relációk:
Természetes összekapcsolás
r s
Bevezetés az SQL-be
• Áttekintés
• Adatdefiníció
• Egyszerű lekérdezések
• További egyszerű műveletek
• Halmazműveletek
• NULL értékek
• Aggregáló függvények
• Egymásba ágyazott lekérdezések
• Az adatbázis változtatása
Történet
• IBM Sequel System R projekt, IBM San Jose
Research Laboratory
• Átnevezés: SQL
• ANSI and ISO standard SQL:
• SQL-86, SQL-89, SQL-92
• SQL:1999, SQL:2003, SQL:2008
• Az SQL-92-t az elterjedt rendszerek (szinte) kivétel
nélkül támogatják. Az újabb szabványokból néhány
elemet (ki ezt, ki azt), és saját bővítéseket.
Data Definition Language (DDL)
• Relációk sémája
• Attribútumok domain-je
• Integritási feltételek (constraint-ek)
• További információk
• Indexek
• Biztonsági és hozzáférési jogosultságokat
érintő kérdések
• Relációk fizikai tárolása.
Domain típusok
• char(n)
• varchar(n)
• int
• smallint
• numeric(p,d). (fixpontos, összesen p digit, n a
tizedesponttól jobbra)
• real, double precision
• float(n)
Create Table Construct
• Reláció létrehozása:
create table r (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
• Ai – az r reláció attribútumának nevei
• r – reláció (tábla) neve
• Di – az Ai domain-jének a neve
• Példa:
create table instructor (
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2))
• insert into instructor values (‘10211’, ’Smith’, ’Biology’, 66000);
• insert into instructor values (‘10211’, null, ’Biology’, 66000);
Integritási feltételek a CREATE
TABLE-ben
• not null
• primary key (A1, ..., An )
• foreign key (Am, ..., An ) references r
.
create table instructor (
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department)
• Ai attribútum
• Ri reláció
• P predikátum
• Egy SQL lekérdezés eredménye szintén egy
reláció lesz.
A select rész
• A select rész tartalmazz az eredményként kívánt
attribútumokat
• Megfelel a relációs algebra projekciójának
• Pl.: keresd meg az összes tanár nevét:
select name
from instructor
• Figyelem, az SQL parancsok nem érzékenyek a kis- ill
nagybetűkre. Azért konvenciók léteznek.
A select rész (folyt.)
• AZ SQL megengedi, hogy egy tábla többször
szerepeljen egy lekérdezésben vagy egy attribútum
az eredményben.
• A duplikált sorok kiszűrése:
• Keresd meg azon részlegek nevét, ahol oktatók
vannak. Az eredményből szűrd ki a duplikátumokat.
select distinct dept_name
from instructor
• Az all explizit megadja, hogy minden adatra
szükség van, és a duplikátumokat ki kell szűrni.
(select course_id from section where sem = ‘Fall’ and year = 2009)
intersect
(select course_id from section where sem = ‘Spring’ and year = 2010)
(select course_id from section where sem = ‘Fall’ and year = 2009)
except
(select course_id from section where sem = ‘Spring’ and year = 2010)
select name
from instructor
where salary > some (select salary
from instructor
where dept_name = ’Biology’);
Definition of Some Clause
• F <comp> some r ⇔ ∃ t ∈ r such that (F <comp> t )
Where <comp> can be: <, ≤, >, =, ≠
0
(5 < some
5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5 < some
5 ) = false
0
(5 = some
5 ) = true
0
(5 ≠ some
5 ) = true (since 0 ≠ 5)
(= some) ≡ in
However, (≠ some) ≡ not in
Example Query
• Find the names of all instructors whose salary is
greater than the salary of all instructors in the Biology
department.
select name
from instructor
where salary > all (select salary
from instructor
where dept_name = ’Biology’);
Definition of all Clause
• F <comp> all r ⇔ ∀ t ∈ r (F <comp> t)
0
(5 < all
5 ) = false
6
6
(5 < all
10 ) = true
4
(5 = all
5 ) = false
4
(5 ≠ all
6 ) = true (since 5 ≠ 4 and 5 ≠ 6)
(≠ all) ≡ not in
However, (= all) ≡ in
Test for Empty Relations
• The exists construct returns the value true if the
argument subquery is nonempty.
• exists r ⇔ r ≠ Ø
• not exists r ⇔ r = Ø
Correlation Variables
• Yet another way of specifying the query “Find all
courses taught in both the Fall 2009 semester and in
the Spring 2010 semester”
select course_id
from section as S
where semester = ’Fall’ and year= 2009 and
exists (select *
from section as T
where semester = ’Spring’ and
year= 2010
and S.course_id=
T.course_id);
• Correlated subquery
• Correlation name or correlation variable
Not Exists
• Find all students who have taken all courses
offered in the Biology department.
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
from course
where dept_name = ’Biology’)
except
(select T.course_id
from takes as T
where S.ID = T.ID));
Note that X – Y = Ø ⇔ X ⊆ Y
Note: Cannot write this query using = all and its variants
Test for Absence of Duplicate Tuples
• The unique construct tests whether a subquery has any
duplicate tuples in its result.
• (Evaluates to “true” on an empty set)
• Find all courses that were offered at most once in 2009
select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id= R.course_id
and R.year = 2009);
Subqueries in the From Clause
• SQL allows a subquery expression to be used in the
from clause
• Find the average instructors’ salaries of those
departments where the average salary is greater than
$42,000.
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;
• Note that we do not need to use the having clause
Subqueries in the From Clause (cont.)
• Another way to write above query
select dept_name, avg_salary
from (select dept_name, avg (salary)
from instructor
group by dept_name)
as dept_avg (dept_name, avg_salary)
where avg_salary > 42000;
Subqueries in the From Clause (Cont.)
• And yet another way to write it: lateral clause
select name, salary, avg_salary
from instructor I1,
lateral (select avg(salary) as avg_salary
from instructor I2
where I2.dept_name=
I1.dept_name);
• Lateral clause permits later part of the from clause (after the
lateral keyword) to access correlation variables from the earlier
part.
• Note: lateral is part of the SQL standard, but is not supported on
many database systems; some databases such as SQL Server
offer alternative syntax
With Clause
• The with clause provides a way of defining a
temporary view whose definition is available only
to the query in which the with clause occurs.
• Find all departments with the maximum budget
• or equivalently
insert into course (course_id, title, dept_name,
credits)
values (’CS-437’, ’Database Systems’,
’Comp. Sci.’, 4);