You are on page 1of 6

Homework Title / No.

: 2 Course Code :CSE301

Course Instructor : _________Chavi ralhan____ Course Tutor (if applicable) : ____________

Date of Allotment : ____9/9/2010______ Date of submission ________23/09/2010__________


Student’s Roll No._________B48_____________ Section No. : ________C2801____________
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 acknowledgment is made explicitly in the text,
nor has any part been written for me by another person.
Student’s Signature :Yogesh Gandhi
Evaluator’s comments:
_____________________________________________________________________
Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:

Question 1: Why do we use indexing? Give a suitable example in support of your answer.

Answer:

Indexes speed up the querying process by providing swift access to rows in the data tables. The
index provides a fast way to look up data based on the values within those columns. Creating and
removing indexes from a database schema will rarely result in changes to an application's code;
indexes operate 'behind the scenes' in support of the database engine. However, creating the
proper index can drastically increase the performance of an application.

For example, If you create an index on the primary key and then search for a row of data based
on one of the primary key values, SQL Server first finds that value in the index, and then uses the
index to quickly locate the entire row of data. Without the index, a table scan would have to be
performed in order to locate the row, which can have a significant effect on performance.
Example:
The SQL Server engine uses an index in much the same way a reader uses a book index. For
example, one way to find all references to INSERT statements in a SQL book would be to begin
on page one and scan each page of the book. We could mark each time we find the word
INSERT until we reach the end of the book. This approach is pretty time consuming and
laborious. Alternately, we can also use the index in the back of the book to find a page number
for each occurrence of the INSERT statements. This approach produces the same results as
above, but with tremendous savings in time.
Question 2: Under what circumstances, we will use foreign key? Give an example.
Answer:

A foreign key (FK) is a column or combination of columns used to establish and enforce a link
between the data in two tables. A link is created between two tables by adding the column or
columns that hold one table's primary key values to the other table. This column becomes a
foreign key in the second table. You can create a foreign key by defining a FOREIGN KEY
constraint when you create or alter a table.A FOREIGN KEY constraint is a candidate for an
index because:

• Changes to PRIMARY KEY constraints are checked with FOREIGN KEY constraints in
related tables.
• Foreign key columns are often used in join criteria when the data from related tables is
combined in queries by matching the column(s) in the FOREIGN KEY constraint of one
table with the primary or unique key column(s) in the other table.

Hence foreign is used when we want to connect two table from each other using primary key. It
also reduces redundancy. Main use is if we update a record in parent table. It will affect child
table also.

For example:-

We have two table one of student record of the college and other one is of hostel record. They
can be connected by each other using foreign key. This will help us in many ways. Such as if
data is updated in student table it will be automatically updated in other tables.

Question3: Produces a new relation with some of the attributes of relation and remove
duplicate tuples, give two different examples

Answer:

Example 1
Relation Poet city
Code City Age
L-1 Downtown 30
L-2 Redwood 40
L-3 Perryride 70

Relation Poet name:


Name Code
Jones L-1
Smith L-2
Hayes L-5

Select s.code,s.city,s.age,c.name,c.code from poet city s outer join poet name c on c.code=c.code;
Code City Age Name
L-1 Downtown 30 Jones
L-2 Redwood 40 Smith

Example 2:
Student table
Stud_no Sname Course
111 Yash Py
222 Aditya Cg
333 Yogesh Cs
Course table
Course_no Name
Py Pharmacy
Cg Computing

Select s.stud_no, s.sname, s.course, c.course_no, c.cname from student s outer join course c on
s.course=c.course_no;

Stud_no Sname Course Course_no Name


111 Yash Py Py Pharmacy
222 Aditya Cg Cg computing

Question 4: Can we use a virtual table for security purpose? Justify your answer and give
an example to create a view.
Answer:
Yes, virtual table can be used for security purpose.

A virtual table is a view. It serves as a security mechanism. If we have several tables in a


database and we want to view only specific columns from specific tables we can go for vitual
table. Though it is similar to a table, it is stored in the database. It is a query stored as an object.
Hence, a view is an object that derives its data from one or more tables. These tables are referred
to as base or underlying tables. This ensures that users are able to retrieve and modify only the
data seen by them. Users cannot see or access the remaining data in the underlying tables. A
view also serves as a mechanism to simplify query execution. Complex queries can be stored in
the form as a view, and data from the view can be extracted using simple queries.

A view can be created by using the CREATE VIEW statement.


CREATE VIEW view_name[(column_name[,column_name]….)]
The restrictions imposed on views are as follows:

• A view can be created only in the current database.


• The name of a view must follow the rules for identifiers and must not be the same as that
of the base table.
• A view can be created only if there is a SELECT permission on its base table.
• A SELECT INTO statement cannot be used in view declaration statement.
• A trigger or an index cannot be defined on a view.
• The CREATE VIEW statement cannot be combined with other SQL statements in a
single batch.

For Ex:

If we have tables which have various columns like name, sex, age, salary, address. But due to
some security reasons we only want to show them name age and sex. This can be done with the
help of virtual table. We can impose various restrictions on this and etc.

Consider the Publishers table below. If you want users to see only two columns in the table,
you can create a view called vwPublishers that will refer to the Publishers table and the two
columns required. You can grant Permissions to users to use the view and revoke Permissions
from the base Publishers table. This way, users will be able to view only the two columns
referred to by the view. They will not be able to query on the Publishers table.

Publishers

Publd PubName City State Country

0736 New Moon Books Boston MA USA


0877 Binnet & Hardly Washington DC USA

1389 Algodata Infosystems Berkeley CA USA

1622 Five Lakes Publishing Chicago IL USA

VW Publishers

Publd PubName

0736 New Moon Books

0877 Binnet & Hardly

1389 Algodata Infosystems

1622 Five Lakes Publishing

Question 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
b) Using a nested query in a Having clause ?
Answer:

a) Select branch_name frome table bank where deposit< (select AVG deposite FROM bank);

b) Select branch_name sum(deposite)from bank group by branch_name having


sum(deposit)<(select AVG (deposit)from bank);

Question 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:
Answer:

A)List all the employees whose location is ‘Pune’ and dept_name is ‘Computer’.

select employee.emp_name from employee join department on employee.


Dept_name=department. Dept_name where dept_name='computer' and location='pune';

B) Count the total number of departments. Also count the total number of employees whose
dept_name is ‘computer’

1) select count( distinct dept_name) as department from dept;


2) select count(eply.emp_name) from employee join dept on employee. Dept_name=department.
Dept_name where dept_name=‘computer’;

C) Add a new department ‘R&D’ in the database.

insert into dept(dept_name) values('R&D');

D) List the names of employees whose salary is greater than 50000 and dept_ name is
‘computer'.

select employee.emp_name from employee join department on employee.


Dept_name=department. Dept_name where dept_name='computer' and salary>50000;

E) List the names of employees having maximum and minimum salary

select Emp_name, salary from employee where salary= (select max(salary) from employee);
select Emp_name, salary from employee salary= (select min(salary) from employee);

F) Now change the department name from ‘computer’ to ‘software design’, wherever
applicable in the table.

update department set dept_name='software_design' where dept_name='computer';

End

You might also like