Professional Documents
Culture Documents
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
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;
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.
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
VW Publishers
Publd PubName
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);
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’.
B) Count the total number of departments. Also count the total number of employees whose
dept_name is ‘computer’
D) List the names of employees whose salary is greater than 50000 and dept_ name is
‘computer'.
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.
End