Ia 2
Ia 2
1. List the characteristics of relational data model. Explain each one with example.
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.
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’;
For example, to change the location and controlling department number of project
number 10 to ‘Bellaire’ and 5, respectively, we use U5:
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';
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 :
Ans :
8. Draw an ER diagram for MOVIE database. Assume your own entities (Minimum
of 4 Entities), attributes and relationship.
Ans :
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 :
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.
Ans :
Views (Virtual Tables) in SQL : A view in SQL terminology is a single table that is
derived from other tables
Syntax:
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.
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.
vii) Find the ids of sailors who have reserved a red boat or a green boat.
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.
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.