You are on page 1of 8

Annexture II

Homework Title/No. : 2___________ Course Code :CSE 301

Course Instructor : Manki Grover Course Tutor(if applicable) _________

Date of Allotment : Date of submission 22/2/2011

Student’s Roll No. :RC1804A03 Section No. –C1804

Declaration :

I declare that this assignment is my individual work. I have not copied from any other student’s work
or from any other source except where due acknowledgement is made explicitly in the text,nor has
any part been written for me by another person.

Student’s Signature :NISHANT

Evaluator’s comments :

___________________________________________________________________________

Marks obtained :______________________ out of _________________________

Content of Homework should start from this page only:

Q. 1 Views and joins play a very important role in DBMs justify


Ans.

Joins:
Using more than a single table of a database is usually essential.The basic form is just to list
all the needed tables in the from line.We do have to explain to the DBMS how the tables
should be joined together. The JOIN keyword is used in an SQL statement to query data from
two or more tables, based on a relationship between certain columns in these tables.

Various types of JOIN we can use are:

• JOIN: Return rows when there is at least one match in both tables
• LEFT JOIN: Return all rows from the left table, even if there are no matches in the
right table
• RIGHT JOIN: Return all rows from the right table, even if there are no matches in
the left table
• FULL JOIN: Return rows when there is a match in one of the tables

Since joins allow to access data from two or more tables they really are of importance
in DBMS.
Views:
Unlike ordinary tables (base tables) in a relational database, a view does not form part of the
physical schema: it is a dynamic, virtual table computed or collated from data in the database.
A SQL View is a virtual table, which is based on SQL SELECT query. Essentially a view is
very close to a real database table (it has columns and rows just like a regular table), except
for the fact that the real tables store data, while the views don’t. The view’s data is generated
dynamically when the view is referenced. A view references one or more existing database
tables or other views. In effect every view is a filter of the table data referenced in it and this
filter can restrict both the columns and the rows of the referenced tables.

Here is an example of how to create a SQL view using already familiar Product and
Manufacturer SQL tables:

CREATE VIEW vwAveragePrice AS


SELECT Manufacturer, ManufacturerWebsite, ManufacturerEmail, AVG(Price) AS
AvgPrice
FROM Manufacturer JOIN Product
ON Manufacturer.ManufacturerID = Product.ManufacturerID
GROUP BY Manufacturer, ManufacturerWebsite, ManufacturerEmail

A view can be referenced and used from another view, from a SQL query, and from stored
procedure. You reference a view as you would reference any real SQL database table:

SELECT * FROM vwAveragePrice.

Both the joins and views have their importance in DBMS and play a important role in
DBMS.

Q. 2 Compare relational algebra with relational calculus give egs.


Ans.
Relational Algebra:
The relational algebra is a procedural query language. It consists of a set of operations that
take one or two relations as input and produce a new relation as their result. The
fundamental operations in the relational algebra are select, project, union, set difference,
Cartesian product, and rename. In addition to the fundamental operations, there are several
other operations—namely, set intersection, natural join, division, and assignment.

to find those tuples pertaining to loans of more than $1250 made by the Jalandhar branch, we
write
σbranch-name =“Perryridge”∧ amount>1250 (loan)
loan-number branch-name amount
L-15 Jalandhar 1500
L-16 Jalandhar 1300

Relational calculus:
Relational calculus consists of two calculi, the tuple relational calculus and the domain
relational calculus, that are part of the relational model for databases and provide a
declarative way to specify database queries. This in contrast to the relational algebra which is
also part of the relational model but provides a more procedural way for specifying queries.

The relational algebra and the relational calculus are essentially logically equivalent: for any
algebraic expression, there is an equivalent expression in the calculus, and vice versa.

Example: Finding the loan number for each loan of an amount greater than $1400
{t | ∃ s ∈ loan (t[loan-number] = s[loan-number]∧ s[amount] > 1400)

Q. 3 “Does indexing play a role in database management system”. Explain your answer
with example.

Ans.

An index can be defined as a small table which has only two columns. The first column
contains a copy of the primary or candidate key of a table and the second column contains a
set of pointers holding the address of the disk block where that particular key value can be
found.

The main advantage of indexing lies is that index makes search operation perform very fast.
Example:Let us consider a table has a several rows of data, each row is 10 bytes wide. If you
want to search for the record number 70, the management system must thoroughly read each
and every row and after reading 99x10 = 990 bytes it will find record number 70. If we have
an index, the management system starts to search for record number 70 not from the table,
but from the index. The index, containing only two columns, may be just 4 bytes wide in each
of its rows. After reading only 99x4 = 396 bytes of data from the index the management
system finds an entry for record number 70, reads the address of the disk block where record
number 70 is stored and directly points at the record in the physical storage device. The result
is a much quicker access to the record (a speed advantage of 990:396).

Considering these advantages of indexing we must say that indexing not only plays role but
plays a vital role in database management system.
Part-B
Q.4Explain all the constraints of the SQl taking egs of two tables employee and dept?

Ans. Ans.The various constraints that are present in sql are:

1)Primary key

2)Foreign key

3)check

4)Not null

5)unique

We have to take example two tables employee and dept.

Primary key: Whatever attribute we are going to create primary key can’t be left empty and
should be unique.

I will use this constraint in employee table.

Creating table employee:

Create table employee(emp_id number(10) primary key,name varchar(15),branch


varchar(20));

Checking the primary key constraint:

1) Leaving the emp_id empty while inserting:

Insert into employee values(‘ ‘,’qwerty’,’accounts’);

Following error occurs:

Cannot insert NULL into (“SCOTT”.”EMPLOYEE”.”EMP_ID”)

It shows that null condition of primary key is fulfilled.

2) Entering same value in emp_id field twice:

Insert into employee(101,’xbx’,’manager’);

1 Row created.

Insert into employee(101,’sjs’,’so’);

Following error occurs:


Unique constraint (SCOTT.SYS_C009665) violated.

The above situations show that primary key constraint is verified.

2.)Foreign key:

Let’s create another table deptt in which emp_id is the foreign key.It means that value of
emp_id to be used in deptt table must present in employee table.

Create table deptt(deptt_no number(10),emp_id number(10),foreign key(emp_id) references


employee(emp_id));

Table created.

Inserting a value of emp_id that is not inserted in employee table:

Insert into deptt values(2213,112);

Now following error occurs:

Integrity constraint (SCOTT.SYS_C00966) violated-primary key not found.

Hence foreign key constraint is also verified.

3)Check:Using check constraint means that we are going to give some condition and that
condition should be fulfilled in order to successfully create table:

Let us use this constraint in both of the tables:

1)Employee table :To use check constraint in employee table it should be used while creating
the employee table.It will be like this:

Create table employee(emp_id number(10),branch varchar(15),check(emp_id like ‘1%’);

Now if we insert emp_id that doen’t start with 1,the constraint will be violated.

2)Deptt table :

Create table deptt(deptt_name varchar(15),deptt_no number(10),check(deptt_name like A%);

It means that if while inserting deptt_name in deptt table it should start with A or else
constraint will be violated.

4)NOT NULL:

Create table employee(emp_id not null,branch varchar(10);


Now if the emp_id is left null then the not null constraint is violated.

Insert into employee values(‘’,’it’);

Following error will occur

Cannot insert NULL into (“SCOTT”.”EMPLOYEE”.”EMP_ID”)

Similarily for deptt table:

Create table deptt(deptt_name varchar(10) not null,deptt_no number(10));

Insert into deptt values(‘’,10);

Cannot insert NULL into (“SCOTT”.”DEPTT”.”DEPTT_NAME”)

5.UNIQUE

Applying this constraint means that a value used once can’t be repeated.

Employee table:

Create table employee(emp_id number(10) unique,branch varchar(15));

Insert into employee(10,’cse’);

Insert into employee(10,’it’)

Following error will occur

Unique constraint (SCOTT.SYS_C009672) violated.

Deptt table:

Create table deptt(deptt_name varchar(20),deptt_no number(10) unique);

Insert into deptt values(‘HRM’, 12);

Insert into deptt values(‘SALES’,12);

Unique constraint (SCOTT.SYS_C009672) violated.

Q. 5: write an SQL query without using a with clause , to find all branches where the
total account deposit is less than the average total account deposit at all branches

a) Using a nested query in the from clause


Ans. select count(distinct(account_number) from account order by branch_name where
count(distinct(account_number)<all(select avg(count(distinct(account_number) from
account order by branch_name);

b) Using a nested query in a Having clause ?


select count(distinct(account_number) from account group by branch_name having
count(distinct(account_number)<all(select avg(count(distinct(account_number) from
account group` by branch_name);

Q. 6: Consider the table EMPLOYEE and Department with following fields:

Employee( Emp_id,Emp_name, Dept_no, salary)

Department(dep_no, Dept_name,Location)

Perform the following computations on table data:

a) List all the employees whose location is ‘Pune’ and salary greater than 10000
Ans select * from employee,department where department.location=’pune’ and
employee.salary>10000;
b) Count the total number of departments. Also count the total number of
employees whose dept_name is ‘computer’
Ans.
Counting total no. of departments:
Select count(dept_name)from department as total_dept _nos from department;
Counting total number of employees whose dept_name is ‘computer’

Ans. Select Dept_name ,count(*) "total_employees " from Department group by


Dept_name having Dept_name='computer'

c) You have forgot to put the primary key while creating table employee now make
emp_id as primary key
Ans. Alter table employee modify(emp_id number(10) primary key);
d) List the names of employees who work in department of sales
Ans. Select emp_name from department,employee where department.dept_name=’sales’;
e) List the names of employees having maximum and minimum salary
Ans. Select emp_name,max(salary),min(salary) from employee;
f) You want to change the name of some column say location to loc in dept table
how you will do it

Ans. Alter table table_name change location loc varchar(20);

You might also like