Unit-2 Dbms To Follow
Unit-2 Dbms To Follow
Relational Model: Introduction to the Relational Model, Integrity Constraints over Relations,
Enforcing Integrity Constraints, Querying Relational Data, Logical Database Design: ER to
Relational, Introduction to Views, Destroying/Altering Tables and Views
Relational Algebra: Selection and Projection, Set Operations, Renaming, Joins,Division, More
Examples of Algebra Queries
Relational Calculus: Tuple Relational Calculus, Domain Relational Calculus
__________________________________________________________________________________
2.1 Introduction to the Relational Model
Relational Model was proposed by E.F. Codd to model data in the form of relations or tables.
After designing the conceptual model of Database using ER diagram, we need to convert the
conceptual model in the relational model which can be implemented using any RDBMS
languages like Oracle SQL, MySQL etc.
Relational Model represents how data is stored in Relational Databases.
A relational database stores data in the form of relations (tables). Consider a relation STUDENT
with attributes
Roll Number,Name,CGPA shown in Table.
• A relation is the fundamental structure used in the relational model to store data. It is
conceptually represented as a table with rows (tuples) and columns (attributes or fields).
• A relation consists of
1.Relation Schema.
2.Relation Instance.
Relation Schema vs Relation Instance:
• 1.Relation Schema:
The schema species the relation's name, the name of each field names (or column, or
attribute), and the domain(data type) of each field.
A domain (data type). is referred to in a relation schema by the domain name and has a set
of associated values.
Example: We use the example of student information in a university database to show
relation schema as
Students(sid: string, name: string, login: string, age: integer, gpa: real).
for instance, that the field named sid has a domain named string.
This schema shows that the relation "Students" has five fields, each with its domain
2.Relation Instance:
An instance of a relation is a set of tuples, also called records, in which each tuple has the
same number of fields as the relation schema .
A relation instance can be thought of as a table in which each tuple is a row, and all rows
have the same number of fields.
Represents the actual data in the relation at a specific moment
Example of a tuple:
<sid: 50000, name: Dave, login: dave@cs, age: 19, gpa: 3.3>
Key Characteristics of the Relational Model:
1.Uniqueness of Tuples:
No two rows (tuples) in a relation can be identical. This ensures that each tuple is unique.
2.Order Irrelevance:
The order of tuples (rows) does not matter.
Similarly, the order of fields (columns) is irrelevant unless the database system explicitly
defines a convention for ordering fields
3.Domain Constraints:
Each attribute is associated with a domain that restricts the values it can hold.
Domain constraints ensure that the values in each column adhere to their data type.
• Example: If "age" has the integer domain, only whole numbers are allowed in that
column.
Formal Notation for a Relation Instances:A relation schema can be expressed as:
R(f1: D1, ..., fn: Dn) .Each field fi has an associated domain Di,each tuple in an instance of R
must satisfy these domain constraints
Example set of tuples:{ <f1: d1, ..., fn: dn> | d1 ∈ Dom1, ..., dn ∈ Domn }
Angular brackets <……> identify fields of a tuple.
EX:<sid: 50000, name: Dave, login: dave@cs, age: 19, gpa: 3.3>-------------
-Tuple
Curly brackets { : : : } denote a set of tuples. {<>,<>,<>……}
The vertical bar ( | ) means “such that,” and ∈ indicates that values belong to a
specific domain.
Key Terminology of Relation:
1.Arity (Degree):
• The degree, also called arity, of a relation is the number of fields.
• Example: The "Students" relation has 5 columns (sid, name, login, age, gpa).
2.Cardinality:
• The number of tuples (rows) in a relation instance is called Cardinality.
• Example: If the relation contains 6 rows, its cardinality is 6.
.
3.Relational Database:A collection of relations with distinct relation names.
The following Relations:
1. Students2. Faculty3. Courses4. Rooms5. Enrolled6. Teaches7. MeetsIN are considered as
example for relational database
4.Relational Database Schema: A collection of relation schemas for all the relations in the
database
Example: University Database
5.Relational Database Instance: A collection of relation instances, one for each relation schema,
representing the current state of the database.
Examples
SQL Commands:
1.CREATE TABLE: statement is used to define a new table
• To create the Students relation, we can use the following statement:
2. INSERT:
• Tuples are inserted using the INSERT command. Add new tuples (rows) to a table.
• We can insert a single tuple into the Students table as follows:
3. DELETE:
• We can delete tuples from a table using the DELETE command.
• We can delete all Students tuples with name equal to Smith using the command:
4. UPDATE:
• Modify the column values in an existing rows in a table.
• For example, we can increment the age and decrement the gpa of the student with sid :53688:
The WHERE clause is applied first and determines which rows are to be modied
• The SET clause specifies how to modify the filtered rows.
• If modifying a column's value based on its current state, the old value is used in the
calculation
Example of Update with Condition:
UPDATE Students S SET S.gpa = S.gpa - 0.1 WHERE S.gpa >= 3.3;
If this query is applied on the instance S1 of Students we obtain the instance as follows:
2.2 INTEGRITY CONSTRAINTS OVER RELATIONS
• A database is only as good as the information stored in it, and a DBMS must therefore help
prevent the entry of incorrect information.
• Integrity Constraint:An integrity constraint (IC) is a condition that is specified on a
database schema, and restricts the data that can be stored in an instance of the database.
• EXAMPLE- A brood group must be ‘A’ or ‘B’ or ‘AB’ or ‘O’ only (can not any other values
else).
• Constraints may apply to each attribute or they may apply to relationships between tables.
• If a database instance satisfies all the integrity constraints specified on the database
schema, it is a legal instance.
• A DBMS enforces integrity constraints, in that it permits only legal instances to be stored in
the database.
INTEGRITY CONSTRAINTS OVER RELATIONS
• 2.2.1. Key Constraints
-Specifying key constraints in SQL
• 2.2.2. Foreign Key Constraints
-Specifying Foreign key constraints in SQL
• 2.2.3. General Constraints
Types of Integrity Constraint:
A set of fields that uniquely identifies a tuple according to a key constraint is called a
candidate key for the relation
• A candidate key (or key) is such a unique set of fields, e.g., sid in the Students relation.
• Two or more columns can be combined together to retrieve data uniquely:Candidate Key
EX: {login, age}
In the given SQL query,StudentsKey is the name of the primary key constraint.
PRIMARY KEY (sid): Specifies that the sid column is the primary key.
CONSTRAINT keyword: Allows assigning a name to the constraint.
If the constraint is violated, the constraint name is returned and can be used to identify the error.
Ex2:
• A similar problem arises whenever we try to insert a tuple with a value in a field that is not in
the domain associated with that field, i.e., whenever we violate a domain constraint.
• Deletion does not cause a violation of domain, primary key or unique constraints. However,
an update can cause violations, similar to an insertion
2.4. QUERYING RELATIONAL DATA
• A relational database query (query, for short) is a question about the data, and the answer
consists of a new relation containing the result. For example, we might want to find all
students younger than 18 or all students enrolled in Reggae203.
• · A query language is a specialized language for writing queries.
• · SQL is the most popular commercial query language for a relational DBMS.
• To selecting a subset of tuples, a query can extract a subset of the fields of each selected
tuple.
• We can compute the names and logins of students who are younger than 18 with the
following query:
• SELECT S.name, S.login FROM Students S WHERE S.age < 18;
• The symbol * means that we retain all fields of selected tuples in the result
• SELECT * FROM Students WHERE age < 18;
• We can also combine information in the Students and Enrolled relations. If we want to obtain
the names of all students who obtained an A and the id of the course in which they got an A,
we could write the following query:
• SELECT S.name, E.cid
FROM Students S, Enrolled E WHERE S.sid = E.sid AND E.grade = ‘A’
The primary key attributes of each participating entity set, as foreign key
fields.
The descriptive attributes of the relationship set.
• The set of non-descriptive attributes is a superkey for the relation. If there are no key
constraints, this set of attributes is a candidate key
Consider a relationship report, as shown below: This relationship set can be converted to a table as
follow
2.5.3. Translating Relationship Sets with Key Constraints
• If a relationship set involves n entity sets and some m of them are linked via arrows in the ER
diagram, the key for any one of these m entity sets constitutes a key for the relation to which
the relationship set is mapped.
• · Thus we have m candidate keys, and one of these should be designated as the primary key.
• · Consider the relationship set Manages shown in below. The table corresponding Manages
has ssn, did, since.
Approach 1: (creating separate table for relationship)
• By this approach the relationship associated with more than one entities is separately
represented using a table. For example - Consider following ER diagram. Each Dept has at
most one manager, according to the key constraint on Manages.
• The table for manages relationship consists of attributes ssn, did, since.did as primary keyand
ssn, did becomes foreign key
Now this relationship consists of three tables Employee, Department and Manages.
Approach 2:No separate table for relationship
• A second approach to translating a relationship set with key constraints is often superior
because it avoids creating a distinct table for the relationship set.
• Modify the table which corresponds to primary key inabove relation(i.e;didof department) by
adding key field of employee and descriptiveattribute “since”.
•
Every department is required to have a manager, due to the participation constraint, and at
most one manager, due to the key constraint
• This relationship set can be converted to a table as;
The NO ACTION specification is the default it tells that ssn employee can’t be deleted if it is pointed
by the Dept_Mgr table
2.5.5. Translating Weak Entity Sets
• A weak entity set always participates in a one-to-many binary relationship and has a key
constraint and total participation at a weak entity
• Consider the following ER diagramof weak entity
The weak entity can’t be uniquely identified by its partial key, so it should be combined with
the primary key of owning entity to form the primary key of the relationship. This relation can
be written as:
The cascade option ensures that information about the policy is deleted if the employee tuple
is deleted.
1.Creating Views
• The motivation behind the view mechanism is to tailor how users see the data.
• Users should not have to worry about the view versus base table distinction.
• This goal is indeed achieved in the case of queries on views; a view can be used just like any
other relation in defining a query. However, it is natural to want to specify updates on views
as well.
• Here, unfortunately, the distinction between a view and a base table must be kept in mind.
• An update on such a restricted view can always be implemented by updating the underlying
base table in an unambiguous way. Consider the following view:
Syntax for updating Views:
•
We can implement a command to modify the gpa of a GoodStudents row by modifying the
corresponding row in Students. We can delete a GoodStudents row by deleting the
corresponding row from Students.
• Need to Restrict View Updates
• · The SQL-92 rules on updatable views are more stringent than necessary, there are some
fundamental problems with updates specified on views, and there is good reason to limit the
class of views that can be updated. Consider the Students relation and a new relation called
Clubs:
• · A tuple in Clubs denotes that the student called mname has been a member of the club
cname since the date jyear 4 Suppose that we are often interested in finding the names and
logins of students with a gpa greater than 3 who belong to at least one club, along with the
club name and the date they joined the club. We can define a view for this purpose:
• If we decide that we no longer need a base table and want to destroy it (i.e., delete all the rows
and remove the table definition information), we can use the DROP TABLE command.
For example, DROP TABLE Students RESTRICT destroys the Students table unless some
view or integrity constraint refers to Students; if so, the command fails.
• If the keyword RESTRICT is replaced by CASCADE, Students is dropped and any
referencing views or integrity constraints are (recursively) dropped as well; one of these two
keywords must always be specified.
• A view can be dropped using the DROP VIEW command, which is just like DROP TABLE.
• ALTER TABLE modifies the structure of an existing table. To add a column called maiden-
name to Students, for example, we would use the following command:
Syntax:
DROP VIEW VIEW_NAME;
ALTER:
The definition of Students is modified to add this column, and all existing rows are padded
with null values in this column
ALTER TABLE can also be used to delete columns and to add or drop integrity constraints
on a table; we will not discuss these aspects of the command beyond remarking that dropping
columns is treated very similarly to dropping tables or views
Users specify what data to retrieve and how to retrieve it by providing a sequence of
operations.
Examples: BASIC, FORTRAN
Users specify what data to retrieve but not how to retrieve it; the system determines the
process.
Examples: SQL, PROLOG, LISP
Relational Algebra
• There are two formal query languages associated with relational model and those are
relational algebra and relational calculus.
• Relational Algebra can be simply called as Algebra. Queries in algebra are composed
using a collection of operators.
• Every operator in the algebra accepts (one or two) relation instances as arguments and
returns a relation instance as the result.
• A relation on the operator is called as relational algebra expression.
• Unary operators take one expression, and binary operators will take two expressions.
1. Selection(σ) : operators used to select rows from a relation where the given condition is
satisfied is called Selection operator
syntax:
EX: We can retrieve rows corresponding to expert sailors by using the σ operator. The
expression
The subscript rating>8 specifies the selection criterion to be applied while retrieving tuples.
output:
The selection operator “σ”specifies the tuples to return through a selection condition.
Selection condition will be in the form of attribute op constant orattribute1opattribute.
op is one of the comparisonoperator from <,>, <=, >=, or =.
2. Projection Operation (∏):
The projection operator(π) allows to extract columns from a relation.
For example,we can get all Sailor names and rating from s2 by using π as follows
EX:
πage(S2) returns
Since the output of a query is also a relation, we can write the query as follows.
It produces the result by applying selection on S2, and applying projection on output relation.
The final output is as follows.
3.Set operations:
Thefollowing standard operations on sets are also available in relation algebra:
1.union(∪)
2.intersection(∩),
3.set-difference(−),and
4.cross-product(×).
1. Union Operation (∪): It performs union operation between two given relations. It
combines rows from two given relations.
Notation: r U s
EX:
Notation: R ∩ S
3. Set Difference (−): It finds tuples which are present in one relation but not in the second
relation.
Notation: r − s
4.Cross –Product:R X S returns a relation instance that contain all fields of R followed by all the
fields of S
The result of R X S, contains one tuples(r, s)for each pair r ∈ R, s ∈ S. This operation is also called as
Cartesian product.
Ex:
RxS
i)Theta Join:
allows you to merge two tables based on the condition It is denoted by symbol θ..
• Theta joins work for all comparison operators
• Syntax:
A ⋈θ B
The operation is defined as
ii).Equijoin:
When Theta join uses only equality comparison operator, it is said to be equijoin. The above example
corresponds to equijoin
. For example, the condition for equi join can be R.name1 = S.name2, that is, equalities between two
fields in R and S.
The schema of the result of an equijoin contains the fields of R (with the same names and domains as
in R) followed by the fields of S that do not appear in the join conditions. If this set of fields in the
result relation includes two fields that inherit the same name from R and S, they are unnamed in the
result relation
Ex:
A further special case of the join operation R S is an equijoin in which equalities are specified on
all fields having the same name in R and S
In this case, we can simply omit the join condition; the default is that the join condition is a collection
of equalities on all common fields.
e)Division(/):
The division operator is used for queries which involve the 'all'.
R1 ÷ R2 = tuples of R1 associated with all tuples of R2.
The resulting operation must have all combinations of tuples of relation S that are present in the first
relation or R.
2.9 Relational Calculus
Relational calculus is an alternative to relational algebra. In contrast to the algebra, which
is procedural, the calculus is nonprocedural, or declarative, in that it allows us to describe
the set of answers without being explicit about how they should be computed
TRC Query:
{T.NAME | EMPLOYEE(T) AND T.SALARY > 50,000}
Result:
NAME
Alice
Charlie
This query fetches the names of employees earning more than 50,000 from the EMPLOYEE table
TRC Query:
{T.NAME | ∃T ∈ EMPLOYEE (T.SALARY > 50,000 AND T.DEPARTMENT = 'HR')}
Result:
NAME
Alice
Charlie
This query retrieves the names of employees in the HR department who earn more than 50,000.
2. Domain Relational Calculus (DRC)
2.Domain Relational Calculus (DRC) is another type of non-procedural query language. Instead of
working with tuples, DRC works with individual attribute values.
In DRC, conditions are applied to domains (i.e., attributes of the table), and the result consists of
specific attributes that satisfy those conditions.
Notation:
Example:
Consider the same EMPLOYEE table. Now, let’s write a DRC query to find
the EMP_ID and NAME of employees who earn a salary greater than 50,000.
DRC Query:
{EMP_ID, NAME | EMPLOYEE(EMP_ID, NAME, SALARY) AND SALARY > 50,000}
Result:
EMP_ID NAME
101 Alice
103 Charlie
This query returns the EMP_ID and NAME of employees whose salary exceeds 50,000.
Using Quantifiers in DRC
Similar to TRC, DRC also supports Existential (∃) and Universal (∀) quantifiers to
refine queries.
Example:
Let’s say we want to find employees who earn more than 50,000 and work in
the HR department. Using quantifiers, we can achieve this as follows:
DRC Query:
{EMP_ID, NAME | ∃(EMP_ID, NAME, SALARY, DEPARTMENT) ∈
EMPLOYEE (SALARY > 50,000 AND DEPARTMENT = 'HR')}
Result:
EMP_ID NAME
101 Alice
103 Charlie