




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Database management class notes
Typology: Lecture notes
1 / 262
This page cannot be seen from the preview
Don't miss anything!





























































































Relational Database: Definitions Relational database: a set of relations Relation: made up of 2 parts:
Cardinality = 3, degree = 5, all rows distinct Do all columns in a relation instance have to be distinct?
Creating and Modifying Relations Using SQL Relational Query Languages A major strength of the relational model: supports simple, powerful querying of data. Queries can be written intuitively, and the DBMS is responsible for efficient evaluation. The key: precise semantics for relational queries. Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change.
The SQL Query Language Creating Relations in SQL
IC: condition that must be true for any instance of the database; e.g., domain constraints. ICs are specified when schema is defined. ICs are checked when relations are modified.
Let us define Enrolled(studid: string, cid: string, grade: string): CREATE TABLE Enrolled ( studid CHAR(20) , cid CHAR(20), grade CHAR(10), PRIMARY KEY (studid, cid), FOREIGN KEY (studid) REFERENCES Students) Only students listed in the Students relation should be allowed to enroll for courses
Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students. What should be done if an Enrolled tuple with a non-existent student id is inserted? ( Reject it) What should be done if a Students tuple is deleted?
Similar if primary key of Students tuple is updated Referential Integrity in SQL SQL/92 and SQL:1999 support all 4 options on deletes and updates.
Default is NO ACTION ( delete/update is rejected )
CASCADE (also delete all tuples that refer to deleted tuple) SET NULL / SET DEFAULT (sets foreign keyvalue of referencing tuple)
CREATE TABLE Enrolled ( studid CHAR(20) , cid CHAR(20) , grade CHAR(10), PRIMARY KEY (studid, did), FOREIGN KEY (studid) REFERENCES Students ON DELETE CASCADE ON UPDATE NO ACTION)
Formal Relational Query Languages Query is question. A query is formulated for a relation /table to retrieve same useful information from table. Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation:
Preliminaries A query is applied to relation instances , and the result of a query is also a relation instance. We can retrieve rows corresponding to students who are younger than 18 with the following SQL query:
SELECT * FROM Students S WHERE S.age < 18 The symbol ,*, means that we retain all fields of selected tuples in the result. Think of S as a variable that takes on the value of each tuple in Students, one tuple after the other. The condition S. age < 18 in the WHERE clause specifies that we want to select only tuples in which the age field has a value less than 18.
We can also combine information in the Students andEnrolled relations. If we want to obtain the names of all students who obtained anA and the id of the course in which they got an A, we could write thefollowing query: SELECT S.name, E.cidFROM Students S, Enrolled E WHERE S.sid = E.studid AND E.grade = 'A'
Entity sets to tables: Relationship Sets to Tables In translating a relationship set to a relation, attributes of the relation must include:
Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation. All descriptive attributes.
Does every department have a manager?
Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another ( owner ) entity.
Weak entity set must have total participation in this identifying relationship set.
Translating Weak Entity Sets Weak entity set and identifying relationship set are translated into a single table.
Review: ISA Hierarchies Overlap constraints : Can Joe be an Hourly_Emps as well as a Contract_Emps entity? ( Allowed/
disallowed )
Covering constraints : Does every Employees entity also have to be an Hourly_Emps or a
Contract_Emps entity? (Yes/no)
Binary vs. Ternary Relationships (Contd.) The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents. Participation constraints lead to NOT NULL constraints.
Views and Security Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s).
Given YoungStudents, but not Students or Enrolled, we can find students s who have are
enrolled, but not the cid’s of the courses they are enrolled in.
View Definition
A relation that is not of the conceptual model but is made visible to a user as a “virtual
relation” is called a view. A view is defined using the create view statement which has the form create view v as < query expression > where
Once a view is defined, the view name can be used to refer to the virtual relation that the view
generates.
Example Queries A view consisting of branches and their customers Uses of Views Hiding some information from some users Consider a user who needs to know a customer’s name, loan number and branch name, but has no need to see the loan amount. Define a view ( create view cust_loan_data as select customer_name, borrower.loan_number, branch_name from borrower, loan where borrower.loan_number = loan.loan_number ) Grant the user permission to read cust_loan_data, but not borrower or loan Predefined queries to make writing of other queries easier Common example: Aggregate queries used for statistical analysis of data Processing of
Views
When a view is created
e 1 vi
All of these operations take two input relations, which must be union-compatible :
–Same number of fields. `Corresponding’ fields have the same type.
What is the schema of result?
Cross-Product Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names `inherited’ if possible.
condition Join : Result schema same as that of cross-product. Fewer tuples than cross-product, might be able to compute more efficiently
Find names of sailors who’ve reserved boat # Solution 1: Find names of sailors who’ve reserved a red boat Information about boat color only available in Boats; so need an extra join: Find sailors who’ve reserved a red or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats: Find sailors who’ve reserved a red and a green boat Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors):
Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus has variables, constants, comparison ops , logical connectives and quantifiers.
Query has the form: DRC Formulas Atomic formula: –, or X op Y, or X op constant
or
Let us revisit the definition of a query: Find all sailors with a rating above 7 The condition ensures that the domain variables I, N, T and A are bound to fields of the same Sailors tuple.
We have used as a shorthand for