Three types of joins: join operation is a Cartesian Product
• Natural join
• Inner join
• Outer join
Tuples - Row
Cardinality - is the number of Tuples(rows)
Degree - number of columns. If 5 columns the degree is also 5
Primary Key - A column or set of columns that uniquely identifies each tuple.
Foreign Key - A column that creates a link between two tables.
Natural join : it matches tuples with the same values for all common
attributes, and retains only one copy of each common column.
● It automatically matches columns that have the same name and same data
type in both tables.
It only shows one copy of the matching column in the result (not both from each
table).
Normal query without natural join construct:
• select name, course_id
from students, takes
where student.ID = takes.ID;
Query with natural join construct:
• select name, course_id
from student natural join takes;
▪ Beware of unrelated attributes with same name which get equated incorrectly
▪ Example -- List the names of students instructors along with the titles of courses that
they have taken
• Correct version:
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
Incorrect version:
select name, title
from student natural join takes natural join course;
IMPORTANT - Since Natural Join considers all of the same columns sometimes we
might have to join one more of the tables with where to not miss out some of the
data!
▪ An extension of the join operation that avoids loss of information.
▪ Computes the join and then adds tuples from one relation that does not match
tuples in the other relation to the result of the join.
▪ Uses null values.
▪ Three forms of outer join:
• left outer join
• right outer join
• full outer join
Join Type What it shows
LEFT JOIN All from left table, and matching from
right (NULL if none)
RIGHT JOIN All from right table, and matching
from left (NULL if none)
FULL OUTER All from both sides, match when
JOIN possible, NULLs otherwise
INNER JOIN Only rows that match in both tables
Most basic one! Using ON
NATURAL
JOIN All columns with same name
JOINED TYPES AND CONDITIONS:
▪ 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.
▪ Join type– defines how tuples in each relation that do not match any
tuples in the other relation (based on the join condition) are treated.
VIEWS - A view in SQL is essentially a named query—a virtual table defined by a
SELECT statement. When you query a view, the database runs its underlying query and
presents you the results as if it were a real table.
Views Defined Using Other Views
1) One view may be used in the expression defining another view:
-- Base view: list of all CS instructors
CREATE VIEW cs_instructors AS
SELECT instr_id, name
FROM instructor
WHERE dept_name = 'ComputerSci';
-- New view built on top of the first:
CREATE VIEW cs_senior_instructors AS
SELECT instr_id, name
FROM cs_instructors -- ← using the view!
WHERE years_experience > 10;
2) A view relation v1 is said to depend directly on a view relation v2 if v2 is
used in the expression defining v1
If view A’s FROM clause mentions view B, we say A directly depends on B.
● In the example above, cs_senior_instructors directly depends on cs_instructors.
3) A view relation v1 is said to depend on view relation v2 if either v1 depends
directly to v2 or there is a path of dependencies from v1 to v2
CREATE VIEW cs_top_salary AS
SELECT instr_id, name
FROM cs_senior_instructors -- A → Bv
ORDER BY salary DESC;
4) A view relation v is said to be recursive if it depends on itself.
CREATE VIEW emp_hierarchy AS -- Base step: every employee reports to themselves
(or direct manager)
SELECT emp_id, manager_id, 1
AS level
FROM employee
WHERE manager_id IS NOT NULL
UNION ALL
-- Recursive step: climb the chain
SELECT e.emp_id, h.manager_id, h.level + 1
FROM employee AS e
JOIN emp_hierarchy AS h -- ← uses itself
ON e.manager_id = h.emp_id;
View Expansion (Cont.)
Materialized
Run the query once and result is stored.
create materialized view total_sales_per_day as
select sale_date, sum(amount) as total
from sales
group by sale_date;
select * from total_sales_per_day;
it’s very fast, because the data is already materialized (saved).
Let’s say someone inserts a new sale into the sales table.
insert into sales values ('2025-05-03', 100);
You must refresh the materialized view to get up-to-date results:
-- Manual refresh
refresh materialized view total_sales_per_day;
✅ Summary
● A materialized view is a saved version of a view’s result.
● It improves performance by avoiding recalculations.
● But it must be refreshed if the underlying tables change.
● It’s ideal when:
○ Data doesn't change too often
○ You need fast reads (e.g., for reports, dashboards)
Update of a View
When can you update a view:
Transactions
BEGIN;
❌ ERROR (ID
UPDATE users SET balance = balance - 100 WHERE id = 1; -- OK
UPDATE users SET balance = balance + 100 WHERE id = 999; --
doesn't exist)
COMMIT; -- ❌
won't be reached
ROLLBACK; -- Required
You use COMMIT to:
● Permanently save all the changes made in the current transaction
● Finalize a successful sequence of SQL statements
● Ensure data is written to disk and becomes visible to other
users
● Mark the end of the transaction
● Confirm that everything went as expected
You use ROLLBACK to cancel:
● Logic errors
● Data mistakes
● Runtime errors
● User aborts
It ensures your database always stays consistent, no matter what
goes wrong.
Integrity Constraints - Integrity constraints guard against accidental damage to the
database,by ensuring that authorized changes to the database do not result in a
loss of data consistency.
Types of Integrity Constraints:
Constraints on a Single:
1) Not null
2) Primary key
3) Unique
4) check(P),where P is a predicate
Not Null Constraints-Declare name and budget to be not null
Name varchar(20) not null
Budget numeric(12,2) not null
Unique constraint - unique ( A1, A2, …, Am)
• The unique specification states that the attributes A1, A2, …, Am
form a candidate key.
• Candidate keys are permitted to be null (in contrast to primary
keys).
The check (P) clause specifies a predicate P that must be satisfied by
every tuple in a relation.
▪ Example: ensure that semester is one of fall, winter, spring or summer
create table section
(course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year numeric (4,0),
building varchar (15),
room_number varchar (7),
time slot id varchar (4),
primary key (course_id, sec_id, semester, year),
check (semester in ('Fall', 'Winter', 'Spring', 'Summer')))
Referential Integrity-
▪ Ensures that a value that appears in one relation for a given set of
attributes also appear for a certain set of attributes in another relation.
• Example: If “Biology” is a department name appearing in one of the
tuples in the instructor relation, then there exists a tuple in the
department relation for “Biology”.
▪ Let A be a set of attributes. Let R and S be two relations that contain
attributes A and where A is the primary key of S. A is said to be a
foreign key of R if for any values of A appearing in R these values also
appear in S.
Foreign keys can be specified as part of the SQL create table
statement
foreign key (dept_name) references department
▪ By default, a foreign key references the primary-key attributes of the
referenced table.
▪ SQL allows a list of attributes of the referenced relation to be specified
explicitly.
foreign key (dept_name) references department (dept_name)
Cascading Actions in Referential Integrity
Assertions-
1) An assertion is a predicate expressing a condition that we wish the database
always to satisfy
2) The following constraints,can be expressed using assertions;
3) For each tuple in the student relation,the value of the attribute tot_cred must equal
the sum of credits of courses that the student has completed successfully.
4) An instructor cannot teach in two different classrooms in a semester in the same
time slot
5) An assertion in SQL takes the form:
Create assertion <assertion-name>check(<predicate>)
Built-in Data Types in SQL
1. BLOB (Binary Large Object)
● Stores raw binary data
● The database doesn’t interpret it — it’s just bytes
● Used for: images, audio files, PDFs, videos
2. CLOB (Character Large Object)
● Stores very large amounts of character/text data
● Often used for:
○ Articles
○ Books
○ Logs
○ HTML/XML documents
Why Not Just Use VARCHAR or TEXT?
LOBs are designed for:
● Huge data sizes (often gigabytes)
● Efficient streaming, storage, and access
VARCHAR or TEXT may be limited in size and memory efficiency.
Alternative explanation:
▪ Large objects (photos, videos, CAD files, etc.) are stored as a large
object:
• blob: binary large object -- object is a large collection of uninterpreted
binary data (whose interpretation is left to an application outside of
the database system)
• clob: character large object -- object is a large collection of character
data
▪ When a query returns a large object, a pointer is returned rather than the
large object itself.
Domain
Index
Ro
Attacks on databases:
1) Unait