Professional Documents
Culture Documents
Ans. Starburst Active Rule System, has gained popularity mainly because of its simple syntax and
semantics, which adopts the classical set-oriented syntax and semantics of relational
databases
IF EXISTS (
SELECT *
WHERE E.Dno = D.Dno AND E.Sal > M.Sal AND D.Mgr = M.Name )
THEN ROLLBACK
• The rule is activated when an employee is created, and when the salary or the department
of an employee changes
• rollback = abort the transaction with the statement that caused the triggering event
✸ The constraint (the salary of employees is not larger than the salary of the manager of
their department) is declarative
✸ rule Mgrsals does not cover all the scenarios that could lead to violating the constraint
✸ another rule is necessary on relation Dept, to check the constraint when a department
changes its manager
5) Write the query for the following statement “If the average salary of employees gets over 100,
then reduces the salary of all employees by 10 %” using starburst rule?
Ans. Rules are triggered by the execution of operations in statements that are part of transactions
Rules are statement-level: they are executed once per statement even for statements that trigger
events on several tuples
CREATE RULE SalaryControl ON Emp
THEN
UPDATE Emp
8) Write the query for the following statement “If average salary of employees exceed 100,
reduce salary of each employee by 10%” using recursive rules?
Set Salary=.9*Salary
Table EMP:
Name Salary
john 90
David 100
on Employee
as
if exists (
select *
from Inserted I,
Employee E
begin
raiserror( 'Constraint Violation: The age of an employee must be less than the age of
his/her supervisor', 1, 1)
rollback
end
2) State the syntax for satisfying the following statement “The location of a project must be
one of the locations of its department.
Ans.
end
3) Definition and use of triggers in Oracle? Write the Trigger syntax in Oracle?
Ans. TRIGGERS are stored programs that are fired by Oracle engine automatically when DML
Statements like insert, update, delete are executed on the table or some events occur. The code
to be executed in case of a trigger can be defined as per the requirement.
Example:
[ENABLE / DISABLE ]
[WHEN condition]
DECLARE
declaration statements
BEGIN
executable statements
EXCEPTION
exception_handling statements
END;
4) Definition and use of triggers in DB2? Write the Trigger syntax in DB2?
Ans. A trigger is a piece of code that is automatically executed, or fired in response to a data
modification event on a table. The data modification event include insert, update, delete driven
by an INSERT, DELETE, UPDATE, and MERGE statement.
Db2 stores the triggers in the database catalog, where each trigger is attached to a single table.
You cannot call a trigger directly, only Db2 can call the trigger when an event occurs on the table.
Example:
ON { table_name |view_name }
{correlation_name |identifier}
action;
5) Given the relational schema:
EMPLOYEE(Name, Salary, DeptNum)
DEPARTMENT(DeptNum, ManagerName)
Define the following active rules in Oracle and DB2.
I) A rule that deletes all the employees belonging to a department when that department is
deleted.
end
II) A rule that reacts to the deletion of the employee who is manager in a department by
deleting that department and all its employees.
begin
end
III) A rule that, each time that salary of an employee becomes higher than that of his or her
manager, makes that salary equal to that of the manage.
declare x number;
begin
end
create trigger T1
from Professor
ii) Each student must have taken at least three courses in the subject of his or
her supervisor.
create trigger T2
after update of Subject on Student
for each row
when 3 < ( select count(*)
from Exam join Course on CourseTitle=Title
join Professor on Professor=Professor.Name
join Student on StudentName=Student.Name
join Professor as P2 on SuperVisor=P2.Name
where Professor.Subject=P2.Subject
and student.Name=new.Name )
then signal SQLSTATE 70006 (“Two few Courses”)
iii) Each student must have taken the exam for the course taught by his or her
supervisor.
create trigger T3
after update of SuperVisor on Student
for each row
when not exist ( select * from Exam join Course on CourseTitle=Title
where StudentName=new.Name and
Professor=new.Supervisor )
then signal SQLSTATE 70007 (“Exam loss”)
declare x number;
declare y number;
declare l number;
begin
from Employee
where DeptNum=new.DeptNum;
y=((x*l)-new.Salary+old.Salary)/l;
if (x>(y*1.03)) then
where DeptNum=new.DeptNum;
end
Ans.
create trigger T5
where Salary>80000
on n.Name=o.Name
where n.Salary<>old.Salary )
(DB2)
create trigger T5
where Salary>80000
9) A family tree represents the structure of a family. Show how the information of a family
tree Can be represented by means of a relational database, in which only the male line or
only the female line is represented.
Gregory Brown
To represent also the offspring of the female members, it is necessary to add another relation
for maternity.
10) Define a database schema that organizes, The information necessary to generate the radio
programmes page of a daily newspaper, with station, times and programme title; besides
the name, include transmission frequency and the location of the radio station.
PROGRAMME(Title,Station,Time)
This schema assumes that the same title can not be used for other programme in different
stations. If instead this happens, then the key for PROGRAMME must be composed of both Title
and Station.
Module II
PART– B ((Long Answer Questions))
7) CREATE TABLE Lab Test (Name, Physician, TestID) and write the program for Patients who were
the sole receivers of all tests ordered by a physician?
Ans. CREATE TABLE LabTest (Name, Physician, TestID) AS VALID EVENT HOUR AND TRANSACTION
2) Create a tables “employee” and “Employee Lifecycle”? Give the name of current
employees who do not work currently in any department?
3) Create a tables “employee”, “Supervision” and “WorksOn”? Give the name of super visors
who had work on a project at sometime?
4) Write the syntax for “Who has been on a drug for more than a total of six months”? Using
following Scenario:
● Patient records include information on the drugs prescribed to each patient.
● The valid time specifies the period(s) during which the drug was prescribed.
● The valid time has a granularity of day (transaction time granularity is system defined):
Ans: i) Who has been on a drug for more than a total of six months
Result will contain the maximal interval(s) when the patient has been on the drug
Restructuring of tables as part of the FROM clause helps keeping queries concise
5) Explain about Valid Time Projection and write the syntax of “What drugs was Melanie
prescribed during 1994”using the patient record?
From Prescription
The Result is a list of drugs, each associated with a set of0020period during 1994 that they
were prescribed to melanie .
6) Create a table of “employee” and Write the syntax to “Find the employee's salary at a given
time: e.g. the current one:”
Ans.
Select Salary
from Employee
8) Write the sql query “At any point in time an employee cannot work more than once in a
project.In other terms (SSN,PNumber) is a sequenced primary key for WorksOn”
begin
raiserror 13000
’At any point in time an employee cannot work more than once in a project’
rollback transaction
end
9) Write the sql query for finding the name(s) of the employee(s) who had the less salary on
1/1/2010. Make your own tables.
Ref: https://tinyurl.com/yk6zcqwq
1. Initial ly A = fq g, with q the initial goal, where q is a recursive predicate and is not a totally free
adornment.
3. For each recursive rule, determine the adornments of its recursive goals (i.e., of q or predicates
mutually recursive with q).
4. If the last step generated adornments not currently in A, add them to A and resume from step 2.
Otherwise halt.
The calling goal g is said to have the binding passing property when A does not contain any recursive
predicate with totally free adornment. In this case, we say that g has the unique binding passing
property when A contains only one adornment for each recursive predicate.
When the binding passing property does not hold, then a totally free adornment occurs, and
mutually recursive predicates must be computed as if the calling goal had no bound arguments.
Otherwise, the methods described in the previous sections are applicable, and the recursive
program is rewritten according to the method selected
9) Write the Algorithm Construction of the Rule-Goal graph rgg(P) for non-recursive program P?
Ans. The rule-goal graph for a program P is denoted rgg(P ). The rule-goal graph for a nonrecursive
program is constructed as follows:
Algorithm:
1. Initial step: The query goal is adorned according to the constants and deferred constants
(i.e., the variables preceded by $), and becomes the root of rgg(P ).
2. 2. Bindings passing from goals to rule heads: If the calling goal g unites with the head of the
rule r, with mgu , then we draw an edge (labeled with the name of the rule, i.e., r) from the
adorned calling goal to the adorned head, where the adornments for h(r) are computed as
follows: (i) all arguments bound in g are marked bound in h(r) ; (ii) all variables in such
arguments are also marked bound; and (iii) the arguments in h(r) that contain only constants
or variables marked bound in (ii) are adorned b, while the others are adorned f . For
instance, say that our goal is g = p(f (X1); Y1; Z1; a), and the head of r is h(r) = p(X2; g(X2 ;
Y2); Y2; W2). (If g and h(r) had variables in common, then a renaming step would be
required here.) A most general under exists for the two: = fX2=f (X1); Y1=g(f (X1); Y2); Z1=Y2;
W2=ag; thus, bindings might be passed from this goal to this
head in a top-down execution, and the resulting adornments of the head must be computed.
The united head is h(r) = p(f (X1); g(f (X1 ); Y2); Y2; a). For instance, say that the goal was
adorned pbf f b ; then variables in the rst argument of the head (i.e., X1) are bound. The
resulting adorned head is pbf f b , and there is an edge from pbf f b to pbf f b . But if the
adorned goal is pf bf b , then all the variables in the second argument of the head (i.e., X1;
Y2) are bound. Then the remaining arguments of the head are bound as well. In this case,
there is an edge from the adorned goal pf bf b to the adorned head pbbbb
3. Left-to-right passing of bindings to goals: A variable X is bound after the nth goal in a rule,
if X is among the bound head variables (as for the last step), or if X appears in one of the
goals of the rule preceding the nth goal. The (n + 1)th goal of the rule is adorned on the basis
of the variables that are bound after the nth goal.
The rule-goal graph determines the safety of the execution in a top-down mode and yields
an overall execution plan, under the simplifying assumption that the execution of a goal
binds all the variables in the goal
Ans. seniors who satisfied all requirements for a cs degree cannot be expressed using ALL, but
must instead be expressed using double negation and existential quartiers.
Find senior students where there is no required cs course our senior has not taken.
SELECT Name
FROM student
(SELECT s.Name
t.Name = s.Name
2) Find the name of junior-level students who have taken both cs101 and cs143 ?
Ans.
3) Write the query for “Junior-level Students who did not take course cs143”using Negation?
Ans.
4) Create a tables of student (Name, Major, Year), Took (Name, Course, Grade) and write the
query for “Find the senior students who are NOT missing any requirement” using Double
Negation?
Ans.
req(cs; Course);
-:hastaken(Name; Course).
:req missing(Name).
Turning a universally quantized query into a doubly negated existential query is never without
difficulty, but this is a skill that can be mastered with some practice. Indeed, such a transformation is
common in natural languages, particularly in euphemistic nuances.
Ans. The six basic operators of relational algebra are the selection(σ ), the projection(π), the
Cartesian product (x) (also called the cross product or cross join), the set union (U), the set
difference (-), and the rename (p). These six operators are fundamental in the sense that none of
them can be omitted without losing expressive power. Many other operators have been defined
in terms of these six. Among the most important are set intersection, division, and the natural
join, but aggregation is not possible with these basic relational algebra operations. So, we cannot
run sum of all employees’ salaries with the six operations
6) Explain how the magic sets and related methods are also applicable to programs that have
the binding passing property, but not the unique binding passing property. Apply the magic
sets method to the following example:
?sg(marc; Who).
sg(X; Y) <-- parent(XP; X); sg(YP; XP);
parent(YP; Y).
sg(A,A)
Ans. The recursive rule here states that X and Y are of the same generation if their respective parents
XP and YP also are of the same generation. The exit rule sg(X; X) states that every element of the
universe is of the same generation as itself. Obviously this rule is unsafe, and we cannot start a bottom-
up computation from it. However, consider a top-down computation on these rules, assuming for
simplicity that the fact parent(tom; marc) is in the database. Then, the resolvent of the query goal
with the first rule is parent(XP; marc); sg(XP; YP); parent(YP; Y). Then, by unifying the first goal in this
list with the fact parent(tom; marc), the new goal list become sg(tom; YP); parent(YP; Y). Thus, the
binding was passed from the first argument in the head to the first argument of the recursive predicate
in the body. Now, the recursive call unfolds as in the previous case , yielding the parents of tom, who
are the grandparents of marc. In summary, the top-down computation generates all the ancestors of
marc using the recursive rule. This computation causes the instantiation of variables X and XP while Y
and YP remain unbound. The basic idea of magic sets is to emulate this top-down binding passing using
rules to be executed in a bottom-up fashion Therefore, we can begin by restricting our attention to
the bound arguments and use the following rule: sg(X) parent(XP; X); sg(XP). Then, we observe that
the top-down process where bindings are passed from X to XP through parent can be emulated by the
bottom-up execution of the magic rule m:sg(XP) m:sg(X); parent(XP; X); ; the rule is constructed from
the last one by exchanging the head with the recursive goal (and adding the prefix \m."). Finally, as
the exit rule for the magic predicate, we add the fact m:sg(marc), where marc is the query constant.
the magic predicate m:sg is computed as shown by the first two rules in Example 1. shows how the
original rules are rewritten with the addition of the magic goal m:sg to restrict the bottom up
computation.
The magic sets method applied to Example 1
m:sg(marc).
sg0(X; X) m:sg(X).
?sg0(marc; Z).
Observe Example 1 , the exit rule has become safe as a result of the magic sets rewriting, since
only people who are ancestors of marc are considered by the transformed rules. Moreover, the magic
goal in the recursive rule is useful in narrowing the search because it eliminates people who are not
ancestors of marc.
SELECT
Part,
FROM
Assembly
ORDER BY
BASIC-PART;
8) Create a tables of Part_Cost (BASIC-PART,SUPPLIER, COST, TIME), Assembly (PART,
SUBPART, QTY) and write the query for "find the parts using 'top_tube'" using recursive
queries?
Ans.
FROM assembly
UNION
SELECT *
Ans.
Materialized views are excessively stored query execution results in the database. They can be used
to partially or completely answer queries which will be further appeared instead of re-executing query
from the scratch. There is a large number of published works that address the maintenance, especially
incremental update, of materialized views and query rewriting for using those ones. Some of them
support materialized views based on recursive query in datalog language. Although most of datalog
queries can be transferred into SQL queries and vice versa but it is not the case for recursive queries.
Recursive queries in the data log try to find all possible transitive closures. Recursive queries in SQL
(Common Table Expression – CTE) return direct links but not transitive closures.
UNION
The SELECT statement before UNION is obviously equivalent to the exit rule in Example 8.15, while
the SELECT statement after UNION corresponds to the recursive rule. Therefore we will refer to them
as exit select and recursive select, respectively. Since all subparts is a virtual view, an actual query on
this view is needed to materialize the recursive relation or portions thereof. For instance, the query
of Ref Answer 8 requests the materialization of the whole relation.