Download presentation
Presentation is loading. Please wait.
1
SQL – Part 2
2
Joined Relations – Birleştirilmiş Çizelgeler
Join operations take two relations and return as a result another relation. These additional operations are typically used as subquery expressions in the from clause Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated. 2
3
Joins Explicit join semantics needed unless it is an INNER join
SELECT (column_list) FROM table_name [INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name ON qualification_list WHERE … Explicit join semantics needed unless it is an INNER join (INNER is default)
4
Inner Join – İç Birleştirme
Only the rows that match the search conditions are returned. SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid Returns only those sailors who have reserved boats SQL-92 also allows: FROM Sailors s NATURAL JOIN Reserves r “NATURAL” means equi-join for each pair of attributes with the same name
5
SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid
6
Outer Join – Dış Birleştirme
An extension of the join operation that avoids loss of information. Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join. Uses null values. 6
7
Left Outer Join (Sol Dış Birleştirme)
Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid Returns all sailors & information on whether they have reserved boats
8
SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid
9
Right Outer Join (Sağ Dış Birleştirme)
Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & information on which ones are reserved.
10
SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid
11
Full Outer Join Full Outer Join returns all (matched or unmatched) rows from the tables on both sides of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & all information on reservations
12
SELECT r.sid, b.bid, b.name FROM Reserves r FULL OUTER JOIN Boats b ON r.bid = b.bid
Note: in this case it is the same as the ROJ because bid is a foreign key in reserves, so all reservations must have a corresponding tuple in boats.
13
Left Outer Join course prereq course natural left outer join prereq 13
14
Right Outer Join course natural right outer join prereq course prereq
14
15
Full Outer Join course prereq course natural full outer join prereq 15
16
Joined Relations in SQL – Examples
course inner join prereq on course.course_id = prereq.course_id What is the difference between the above and a natural join? 16
17
Joined Relations in SQL – Examples
course prereq course left outer join prereq on course.course_id = prereq.course_id 17
18
Views (Görünümler) In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) Consider a person who needs to know an instructors name and department, but not the salary. This person should see a relation described, in SQL, by select ID, name, dept_name from instructor A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view. 18
19
View Definition A view is defined using the create view statement which has the form create view v as < query expression > where <query expression> is any legal SQL expression. The view name is represented by v. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. View definition is not the same as creating a new relation by evaluating the query expression Rather, a view definition causes the saving of an expression; the expression is substituted into queries using the view. 19
20
Example Views A view of instructors without their salary create view faculty as select ID, name, dept_name from instructor Find all instructors in the Biology department select name from faculty where dept_name = ‘Biology’ Create a view of department salary totals create view departments_total_salary(dept_name, total_salary) as select dept_name, sum (salary) from instructor group by dept_name; 20
21
Integrity Constraints (Review) (Bütünlük Kısıtları)
An IC describes conditions that every legal instance of a relation must satisfy. Inserts/deletes/updates that violate IC’s are disallowed. Can be used to ensure application semantics (e.g., sid is a key), or prevent inconsistencies (e.g., sname has to be a string, age must be < 200) Types of IC’s: Domain constraints, primary key constraints, foreign key constraints, general constraints. Domain constraints: Field values must be of right type. Always enforced. 5
22
Integrity Constraints on a Single Relation
not null primary key unique check (P), where P is a predicate 22
23
General Constraints Useful when more general ICs than keys are involved. Can use queries to express constraint. CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 ) 8
24
General Constraints Constraints can be named. ( sname CHAR(10),
CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT noInterlakeRes CHECK (`Interlake’ <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) Constraints can be named. ‘Interlake’ cannot be reserved constraint This works because bid is key for Boats and so B.bname returns unique name. Still the <> is hacky. 8
25
Domain Constraints A user can form a new domain using the CREATE DOMAIN statement, which makes use of CHECK constraints CREATE DOMAIN ratingval INTEGER DEFAULT 0 CHECK ( VALUE >= 1 AND VALUE <= 10 )
26
Constraints Over Multiple Relations
CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100) Number of boats plus number of sailors is < 100 Awkward and wrong! If the Sailors table is empty, the constraint is defined to hold always, so if Sailors is empty, the number of Boats tuples can be anything! 9
27
Constraints Over Multiple Relations
Number of boats plus number of sailors is < 100 CREATE ASSERTION smallClub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT(B.bid) FROM Boats B) < 100) Table constraints are required to hold only if the associated table is nonempty. We can create ASSERTIONs. Assertions are not associated with any table. 9
28
Triggers Trigger: procedure that starts automatically if specified changes occur to the DBMS Three parts: Event: A change to the database that activates the trigger. Condition: tests whether the triggers should run Action: A procedure that is executed when the trigger is activated and its condition is true Triggers monitor the database
29
Triggers: Example: Oracle 7 Syntax
30
Triggers: Example (SQL:1999)
Set-oriented trigger. Insets info for all new young sailors (less than 18).
31
Exercise 5.10 Consider the folllowing relational schema. An employee can work in more than one department; the pct_time field of the Works relation shows the percentage of time that a given employee works in a given department. Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, budget: real, managerid: integer) Write SQL integrity constraints (domain, key, foreign key or CHECK constraints or assertions) to ensure each of the following, independently. Employees must make a minimum salary of $1000. A manager must always have a higher salary than any employee that he or she manages.
32
Read the examples from the textbook Chapter 5
End of SQL discussion Read the examples from the textbook Chapter 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.