Professional Documents
Culture Documents
SSCITM
Relational model can represent as a table with columns and rows. Each row is known as a
tuple. Each table of the column has a name or attribute.
Attribute: It contains the name of a column in a particular table. Each attribute Ai must have
a domain, dom(Ai)
Relational instance: In the relational database system, the relational instance is represented
by a finite set of tuples. Relation instances do not have duplicate tuples.
Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify the
row in the relation uniquely.
Relational Algebra
Basic operations:
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to obtain the
result of the query. It uses operators to perform queries.
1. Selection Operation:
The select operation selects tuples that satisfy a given predicate.
It is denoted by sigma (σ).
1. Notation: σ p(r)
Where:
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes are eliminated from the table.
It is denoted by ∏.
1. Notation: ∏ A1, A2, An (r)
Input: ∏ NAME, CITY (CUSTOMER)
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
3. Union Operation:
Suppose there are two tuples R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in both R & S.
It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in R but not in S.
1. Notation: R - S
Input:
1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
Relational Calculus
Notation:
1. {T | P (T)} or {T | Condition (T)}
Where
For example:
1. { T.name | Author(T) AND T.article = 'database' }
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a tuple with
'name' from Author who has written an article on 'database'.
TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and
Universal Quantifiers (∀).
For example:
1. { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output: This query will yield the same result as the previous one.
Domain relational calculus uses the same operators as tuple calculus. It uses logical
connectives ∧ (and), ∨ (or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Notation:
1. { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
For example:
1. {< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}
Output: This query will yield the article, page, and subject from the relational javatpoint,
where the subject is a database.
1. The tuple relational calculus restricts to safe expression and is equal in expressive power to
algebra.
2. Thus for every relational algebra expression, there is an equivalent expression in the tuple
relational calculus expression, there is equivalent relational-algebra expression.
3. The expressive power of relational algebra is often used as how powerful a relational
database query language is.
4. The domain relational calculus is restricted to safe expressions,it is equivalent in
expressive power to the tuple relational calculus restricted to safe expression
(Q1) Find the names of sailors who have reserved boat 103
FROM from-list
WHERE qualification
Every query must have a SELECT clause, which specifies columns to be retained in the
result, and a FROM clause, which specifies a cross-product of tables. The optional WHERE
clause specifies selection conditions on the tables mentioned in the FROM clause.
Expressions and Strings in the SELECT Command: SQL supports a more general version
of the select-list than just a list of colu1nns. Each item in a select-list can be of the form
expression AS column_name, where expression is any arithmetic or string expression over
column names and constants, and column name is a new name for this column in the output
of the query. It can also contain aggregates such as sum and count etc.
NESTED QUERIES
It allows us to test whether a set is nonempty, an implicit comparison with the empty set.
Thus, for each Sailor row 5, we test whether the set of Reserves rows R such that R.bid = 103
AND S.sid = R.sid is nonempty.
If so, sailor 5 has reserved boat 103, and we retrieve the name. The subquery clearly depends
on the current row S and must be re-evaluated for each row in Sailors.
The occurrence of S in the subquery (in the form of the literal S.sid) is called a correlation,
and such queries are called correlated queries.
Set-Comparison Operators:
The set-comparison operators EXISTS, IN, and UNIQUE, are also with their negated
versions NOT.
SQL also supports op ANY and op ALL, where op is one of the arithmetic comparison
operators {<, <=, =, <>, >=, >}. (SOME is also available, but it is just a synonym for
ANY.)
AGGREGATE OPERATORS
NULL VALUES
SQL provides a special column value called null to use if value is unknown. We use null
when the column value is either unknown or inapplicable.
We must define the logical operators AND, OR, and NOT using a three-value logic in
which expressions evaluate to true(T), false(F), or unknown (U).
And OR
conditon1 condition2 Result conditon1 condition2 Result
F F F F F F
F T F F T T
F U F F U U
T F F
T F T
T T T
T T T
T U T
U MCA Dept.
Prepared by P. Sandeep Kumar, Asst. Prof U U Page 16
DBMS UNIT II
SSCITM
T U U
U U U
We can disallow null values by specifying NOT NULL as part of the field definition.
In addition, the fields in a primary key are not allowed to take on null values. Thus, there is
an implicit NOT NULL constraint for every field listed in a PRIMARY KEY constraint.
We can specify complex constraints over a single table using table constraints, which have
the form CHECK conditional-expression. For example, to ensure that rating must be an
integer in the range 1 to 10, we could use:
To enforce the constraint that Interlake boats cannot be reserved, we could use:
A user can define a new domain using the CREATE DOMAIN statement, which uses
CHECK constraints.
INTEGER is the underlying, or source, type for the domain ratingval, and every ratingval
value must be of this type. Values in ratingval are further restricted by using a CHECK
constraint; in defining this constraint, we use the keyword VALUE to refer to a value in the
domain.:
The optional DEFAULT keyword is used to associate a default value with a domain. If the
domain ratingval is used for a column in some relation and no value is entered for this
column in an inserted tuple, the default value 1 associated with ratingval is used.
As an example, suppose that we wish to enforce the constraint that the number of boats plus
the number of sailors should be less than 100. (This condition might be required, say, to
qualify as a 'smaIl' sailing club.) We could try the following assertion:
Action: A procedure that is executed when the trigger is activated and its condition is true.
A trigger can be thought of as a 'daemon' that monitors a database, and is executed when the
database is modified in a way that matches the event specification. An insert, delete, or
update statement could activate a trigger, regardless of which user or application invoked the
activating statement; users may not even be aware that a trigger was executed as a side effect
of their program.
A condition in a trigger can be a true/false statement.If the condition part evaluates to true,
the action associated with the trigger is executed.
A trigger action can examine the answers to the query in the condition part of the trigger,
refer to old and new values of tuples modified by the statement activating the trigger, execute
new queries, and make changes to the database.
In fact, an action can even execute a series of data-definition commands (e.g., create new
tables, change authorizations) and transaction-oriented commands (e.g., commit) or call host-
language procedures.
If a statement activates more than one trigger, the DBMS typically processes all of them, in
same arbitrary order. An important point is that the execution of the action part of a trigger
could in turn activate another trigger. In particular, the execution of the action part of a
trigger could again activate the same trigger; such triggers are called recursive triggers. The
potential for such chain activations and the unpredictable order in which a DBMS processes
activated triggers can make it difficult to understand the effect of a collection of triggers.