Beruflich Dokumente
Kultur Dokumente
Relations
More fundamentals
Terminology
Keys
Example
CREATE TABLE student (id serial PRIMARY KEY, name text, surname text, born date, gender
char(1) CHECK (gender = ‘F’ OR gender = ‘M’));
Two constraints are defined:
o PRIMARY KEY
o CHECK: a domain constraint
Other type of constraints
o NOT NULL: column does not accept null values
o UNIQUE: column does not accept repeated values
o REFERENCES : column is a foreign key
Example 1
CREATE TABLE signs (student_id int REFERENCES student(id), course_id int REFERENCES
course(id), PRIMARY KEY(student_id, course_id);
CREATE ASSERTION section_size CHECK (NOT EXISTS (SELECT COUNT(*) FROM section
GROUP BY course_id HAVING COUNT(*) > 25));
o Anytime SECTION is updated the assertion is checked; if it evaluates to FALSE the
update is rejected. Most DBMS do not implement assertions
Example 2
Triggers
Triggers 1
A trigger can fire BEFORE or AFTER a given event (INSERT, DELETE, UPDATE)
The function can use NEW (the new row, after the event is executed) and OLD (the old row,
as it as before the event is executed) row level triggers
NEW can be modified in BEFORE trggers
If several triggers are defined for the same table and event they fire in cascade
Triggers 2
The trigger that fires BEFORE for each row can return null (the action of the event is
skipped), or a row in case of INSERT or UPDATE events
AFTER triggers should return NULL
A trigger that propagates information to other tables (ex. log) shoudl fire AFTER to make
sure all BEFORE triggers have already fired
Triggers: define the function
CREATE FUNCTION CheckDegree()
RETURNS TRIGGER AS $$BEGIN IF NOT (SELECT degree_id from course WHERE id =
NEW.course_id) = (SELECT degree_id FROM signs WHERE student_id=NEW.student_id) THEN
RAISE EXCEPTION ‘Error: course does not belong to t.he degree of the student.’;
END IF; RETURN NEW; END$$
LANGUAGE plpgsql;
Views
Are virtual tables, showing to the user only the information he has access to
Are the result of executing a query
Avoids exposing at the conceptual/external level the information of the logic level
Views can be defined from one or more relations
o They are updated anytime the underlying tables are updated
o Views are not materialized (they are not stored, they are executed every
time they’re used)
Views (example)
Closure property
The algebra operands are the relations or variables that represent relations, and the
operators do common manipulations with relations in the database
The relational algebra is a closed system: the result of an operator, being a relation, can be
fed to other operator, and so on:
o operator1(…(operatorn(expression))…)
SELECT: example
PROJECT: example
JOIN: example
Join characteristics
It’s an important operator, allows linking tables
As it checks potentially all rows from each table, it’s a costly operation
As it is generally used frequently, it’s internal execution must be optimized (will see later)
Set operators
The relations must be compatible: they must have the same number of attributes with the same
type (same schema)
Cartesian product
Not used
R × S combines all rows of R with all rows of S. The cardinality of the result is the product
of the cardinality of R and S and the degree is the sum of their degrees. If there are
columns with the same name, they are renamed
SQL SELECT
SELECT [ALL | DISTINCT] select_expr, ...
FROM table_references
[WHERE where_definition]
[HAVING where_definition]
GROUP BY
Grouping:
o max, min
o count
o avg
o sum
o …
Result of a SELECT