0% found this document useful (0 votes)
4 views

Ia 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Ia 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ASSIGNMANT_QUESTION BANK 2

1. List the characteristics of relational data model. Explain each one with example.

Ans : Characteristics Of Relations :


 Ordering of tuples in a relation r(R):
o The tuples are not considered to be ordered,
ordered, even though they appear to be in the tabular
form.
 Ordering of attributes in a relation schema R (and of values within each tuple):
o We will consider the attributes in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn>
to be ordered .
 (However, a more general alternative definition of relation does not require this
ordering).

 Values in a tuple:
o All values are considered atomic (indivisible).
o Each value in a tuple must be from the domain of the attribute for that column
 If tuple t = <
<v1,
v1, v2, …, vn> is a tuple (row) in the relation state r of
R(A1, A2, …, An)
 Then each vi must be a value from dom(Ai)
o A special null value is used to represent values that are unknown or inapplicable
to certain tuples.
 Notation:
o We refer to component values of a tuple t by:
 t[Ai] or t.Ai
 This is the value vi of attribute Ai for tuple t
o Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of
attributes Au, Av, ..., Aw, respectively in t

2. Explain how the different update operations deal with constraint violations.
Ans :
Update Operations on Relations
Relations:
 INSERT a tuple.
 DELETE a tuple.
 MODIFY a tuple.
 UPDATE may violate domain constraint and NOT NULL constraint on an attribute
being modified
 Any of the other constraints may also be violated, depending on the attribute being
updated:
o Updating the primary key (PK):
 Similar to a DELETE followed by an INSERT
 Need to specify similar options to DELETE
o Updating a foreign key (FK):
 May violate referential integrity
o Updating an ordinary attribute (neither PK nor FK):
 Can only violate domain constraints

3. Describe ALTER TABLE command. Explain how a new constraint can be added
and also an existing constraint can be removed using suitable examples.
Ans :

ALTER TABLE : The definition of a base table elements can be changed by using the ALTER
command.
The possible alter table actions include adding or dropping a column (attribute), changing a
column definition, and adding or dropping table constraints.

ALTER TABLE COMPANY.EMPLOYEE


ADD COLUMN Job VARCHAR(12);

ALTER TABLE COMPANY.EMPLOYEE


DROP COLUMN Address CASCADE;

It is also possible to alter a column definition by dropping an existing default clause


or by defining a new default clause.

The following examples illustrate this clause:


ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn
DROP DEFAULT;

ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn


SET DEFAULT ‘333445555’;

ALTER TABLE COMPANY.EMPLOYEE


DROP CONSTRAINT EMPSUPERFK CASCADE;

4. Discuss ON DELETE and ON UPDATE constraint, illustrate with Example.

Ans :
The DELETE Command
The DELETE command removes tuples from a relation. It includes a WHERE
clause, similar to that used in an SQL query, to select the tuples to be deleted.
U4A: DELETE FROM EMPLOYEE
WHERE Lname = ‘Brown’;

U4B: DELETE FROM EMPLOYEE


WHERE Ssn = ‘123456789’;

U4C: DELETE FROM EMPLOYEE


WHERE Dno = 5;

U4D: DELETE FROM EMPLOYEE;

The UPDATE Command


The UPDATE command is used to modify attribute values of one or more selected
tuples. As in the DELETE command, a WHERE clause in the UPDATE command
selects the tuples to be modified from a single relation.

For example, to change the location and controlling department number of project
number 10 to ‘Bellaire’ and 5, respectively, we use U5:

U5: UPDATE PROJECT


SET Plocation = ‘Bellaire’, Dnum = 5
WHERE Pnumber = 10;

U6: UPDATE EMPLOYEE


SET Salary = Salary * 1.1
WHERE Dno = 5;
It is also possible to specify NULL or DEFAULT as the new attribute value.
Notice that : each UPDATE command explicitly refers to a single relation only. To modify
multiple relations, we must issue several UPDATE commands.

5. Describe six clauses in the syntax of an SQL retrieval query. Show what type of
constructs can be specified in each of the six clauses. Which of the six clauses are
required and which are optional?

Ans :
The basic syntax of a SQL retrieval query includes six main clauses:
1. SELECT
2. FROM
3. WHERE
4. GROUP BY
5. HAVING
6. ORDER BY
1. SELECT Clause: The SELECT clause specifies the columns that you want to retrieve
in the result set.

Syntax: Example:
SELECT column1, column2, ... SELECT first_name, last_name, age
FROM table_name; FROM employees;

2. FROM Clause: The FROM clause specifies the table or tables from which to retrieve the
data.
Syntax: Example:
SELECT column1, column2, ... SELECT product_name, price
FROM table_name; FROM products;

3.WHERE Clause: The WHERE clause is used to filter the rows returned by specifying a
condition that must be met.
Syntax: Example:
SELECT column1, column2, ... SELECT customer_name, order_date
FROM table_name FROM orders
WHERE condition; WHERE order_status = 'Shipped';

4. GROUP BY Clause: The GROUP BY clause is used to group rows based on


one or more columns.It is often used in conjunction with aggregate functions.

Syntax: Example :
SELECT column1, aggregate_function (column2) SELECT Dno, COUNT (*), AVG (Salary)
FROM table_name FROM EMPLOYEE
GROUP BY column1 GROUP BY Dno;
5. HAVING Clause: The HAVING clause is used in conjunction with the GROUP BY clause
to filter the results of aggregate functions.
Syntax: Example :
SELECT Dno, COUNT (*)
SELECT column1, aggregate_function(column2)
e_function(column2) FROM EMPLOYEE
FROM table_name WHERE Salary>40000
GROUP BY column1 GROUP BY Dno
HAVING condition HAVING COUNT (*) > 5;

6.ORDER BY Clause: The ORDER BY clause is used to sort the result set based on one or
more columns, either in ascending (ASC) or descending (DESC) order.
Syntax: Example:
SELECT column1, column2, ...
FROM table_name SELECT E_Name
ORDER BY column1 [ASC|DESC] FROM EMPLOYEE
ORDER BY E_Name ASC;

6. Outline the steps to convert the basic ER model to relational database schema with
example.
Ans :

7. Draw an ER diagram for BANKING database. Assume your own entities


(Minimum of 4 Entities), attributes and relationship.

Ans :
8. Draw an ER diagram for MOVIE database. Assume your own entities (Minimum
of 4 Entities), attributes and relationship.

Ans :

9. Draw an ER diagram for COMPANY database. Assume your own entities


(Minimum of 4 Entities), attributes and relationship.

Ans :
10. Define the following terms:
I) super key :
The set of attributes that can uniquely identify a tuple is known as Super Key. For esxxample,
STUD_NO, (STUD_NO, STUD_NAME), etc. A super key is a group of single or
multiple keys that identifies rows in a table. It supports NULL values.
 Adding zero or more attributes to the candidate key generates the super key.
 A candidate key is a super key but vice versa is not true.
 Super Key values may also be NULL.

II) candidate key : The minimal set of attributes that can uniquely identify a tuple is
known as a candidate key. For Example, STUD_NO in STUDENT relation.
 The value of the Candidate Key is unique and may be null for a tuple.
 There can be more than one candidate key in a relationship.

III) primary key : PRIMARY KEY in SQL is a column (or group of columns) that
uniquely identifies the records in that table. A primary key must contain unique
values and can not have any NULL value.
 It is a unique key.
 It can identify only one tuple (a record) at a time.
 It has no duplicate values, it has unique values.
 It cannot be NULL.

IV) foreign key: A foreign key is a column or a combination of columns in a table that
establishes a link between two tables in a relational database. It refers to the primary key
in another table, creating a relationship between them.

V) Alternate key : The keys that contain all the properties needed to become a Candidate
Key are known as Alternate Keys.

11. How triggers and assertions are defined in SQL? Explain each with examples

Ans :
Each assertion is given a constraint name and is specified via a condition similar to the WHERE
clause of an SQL query.
For example :
To specify the constraint that the salary of an employee must not be greater than the salary of the
manager of the department that the employee works for in SQL, we can write the following assertion:
CREATE ASSERTION SALARY_CONSTRAINT
CHECK ( NOT EXISTS ( SELECT *
FROM EMPLOYEE E, EMPLOYEE M,
DEPARTMENT D
WHERE E.Salary>M.Salary
AND E.Dno = D.Dnumber
AND D.Mgr_ssn = M.Ssn ) );
TRIGGER :

Another important statement in SQL is CREATE TRIGGER.


 In many cases it is convenient to specify the type of action to be taken when certain events
occur and when certain conditions are satisfied.
For example:
 A manager may want to be informed if an employee’s travel expenses exceed a certain
limit by receiving a message whenever this occurs.
 The action that the DBMS must take in this case is to send an appropriate message to
that user.
 The condition is thus used to monitor the database. Other actions may be specified,
such as executing a specific stored procedure or triggering other updates. The CREATE
TRIGGER statement is used to implement such actions in SQL.

Suppose we want to check whenever an employee’s salary is greater than the salary
of his or her direct supervisor in the COMPANY database.
 Several events can trigger this rule: inserting a new employee record, changing an
employee’s salary, or changing an employee’s supervisor. Suppose that the action to

take would be to call an external stored procedure SALARY_VIOLATION, which will notify
the supervisor. The trigger could then be written as in R5 below. Here we are
using the syntax of the Oracle database system.

R5: CREATE TRIGGER SALARY_VIOLATION


BEFORE INSERT OR UPDATE OF SALARY, SUPERVISOR_SSN
ON EMPLOYEE
FOR EACH ROW
WHEN ( NEW.SALARY > ( SELECT SALARY FROM EMPLOYEE
WHERE SSN = NEW.SUPERVISOR_SSN ) )
INFORM_SUPERVISOR(NEW.Supervisor_ssn,
NEW.Ssn );
12. Write syntax of Views and Specify whether Views are updatable? Create a view which
will display the department name, number of employees working and total salary
for each department.

Ans :
Views (Virtual Tables) in SQL : A view in SQL terminology is a single table that is
derived from other tables

Syntax:

CREATE VIEW table name or view name


AS SELECT Column1,column2..
FROM table name1, table name2, table name3…
WHERE conditions;

V1: CREATE VIEW WORKS_ON1


AS SELECT Fname, Lname, Pname, Hours
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Ssn = Essn AND Pno = Pnumber;

V2: CREATE VIEW DEPT_INFO(Dept_name, No_of_emps, Total_sal)


AS SELECT Dname, COUNT (*), SUM (Salary)
FROM DEPARTMENT, EMPLOYEE
WHERE Dnumber = Dno
GROUP BY Dname;

Different strategies as to when a materialized view is updated are possible. The immediate
update strategy updates a view as soon as the base tables are changed;
13. Consider following schema for a Sailors database
Sailors (Sname, Sid, Rating, Age)
Reserves (Sid, Bid, day)
Boats (Bid, Bname, Color)
Write the queries in SQL to
i) Find the age of the youngest sailor who is eligible to vote for each rating level
with at least two such sailors.
select s1.rating,min(s1.age) from sailors s1 where
s1.age>18 group by s1.rating having count(*)>=2 ;

ii) Find the name of sailors who reserved all the boats of ‘Interlake’.

SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS (( SELECT B.bid
FROM Boats B )
EXCEPT
(SELECT R. bid
FROM Reserves R
WHERE R.sid = S.sid ))

iii) Find the sids of all sailors who have reserved red boats but not the green boats.

select s1.sname from sailors s1,reserves r1,boats b1 where


s1.sid=r1.sid and r1.bid=b1.bid and b1.color='red'
except
select s2.sname from sailors s2,reserves r2,boats b2 where
s2.sid=r2.sid and r2.bid=b2.bid and b2.color='green';

iv) Find the names of sailors who have reserved at least one boat.
select distinct s.sname from sailors s,reserves r where
s.sid=r.sid

v) Find the ids and names of sailors who have reserved two different boats on the same
day.

select distinct s.sname,s.rating+1 as inc from sailors


s,reserves r1,reserves r2,boats b1,boats b2
where s.sid=r1.sid and r1.reserveDate=r2.reserveDate and
r1.bid=b1.bid and b1.color<>b2.color;

vi) Count the number of different sailor names.

select distinct count(*) from sailors s;

vii) Find the ids of sailors who have reserved a red boat or a green boat.

select s1.sname from sailors s1 where s1.sid


in (select r1.sid from reserves r1 where r1.bid in(select b1.bid
from boats b1 where b1.color='red'))
viii) Find all information of sailors who have reserved boat number 103
select * from sailors s,reserves r
where s.sid=r.sid and r.bid=103;

ix) Find the name of sailors whose rating is better than some sailor called ‘xyz’.
select s1.sname from sailors s1 where s1.rating>any (select
s2.rating from sailors s2 where s2.sname='xyz')

x) Find the name of the sailors who are older than the oldest sailor with rating 10.

select avg(s.age) from sailors s where s.rating=10;

14. Consider the following schema for Banking database


Branch(Bname,Bcity,Assets)
Loan( Loan_no., Loan_amt, Bname)
Borrow(Loan_no,Cname) Account(Accno, Accbal, Bname)
Depositor(Accno, Cname) Customer(Cname, Street,City)

Write the queries in SQL to


i) Find all customer names who have an Account but not loan at the Bank.

ii) Find the average balance for each branch, if average balance is greater than
Rs.200000.
iii) Find the names of customers who have account in all the branches of Ballari.
iv) Find the all customers who live in Ballari city.
v) Find the names of all customers whose strret address includes ‘Main’.
vi) Find the names of all customers whose balance greater than the customers of
cantonment branch.

15. Consider the following schema for a company database


Employee (Name, SSN, address, Sex, salary, DNo)
Department (DName, DNumber, MGRSSN, MGRSTARDATE)
Project (Pname, pnumber, plocation, Dnum)
Work_on (ESSN, PNO, Hours)
Dependent (ESSN, Dependent_name, sex, B_date, Relationship)
Write the queries in SQL to
i) Retrieve the names of employees who have no dependents.
ii) Retrieve the name and address of all employees who work for the ‘research’
department
iii) Retrieve the name of an employee who works in same department as that of ‘RAJ’
iv) Retrieve the name of the manager of each department.
v) For each project retrieve the project number, project name and number of employee
who worked on that project.
vi) Retrieve the names of employees who work on all the project controlled by department
5.
vii) Retrieve the name of employee who have no dependents.
viii) Retrieve the number of male and female employee working in the company.
ix) Find the name of sailors whose rating is better than some sailor called ‘xyz’.
16. Find the name of the sailors who are older than the oldest sailor with rating 10.

You might also like