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

ANS- Indexes are generated: (Probably) for fields specified with `PRIMARY KEY' or `UNIQUE' constraints in a CREATE TABLE statement. For fields specified in SQL statements such as CREATE [UNIQUE] INDEX indexname ON tablename (col [,col]...). But simply, database indexes help speed up retrieval of data. The other great benefit of indexes is that your server doesn’t have to work as hard to get the data. They are much the same as book indexes, providing the database with quick jump points on where to find the full reference (or to find the database row). Some fields are automatically indexed. A primary key or a field marked as ‘unique’ – for example an email address, a userid or a social security number – are automatically indexed so the database can quickly check to make sure that you’re not going to introduce bad data. The same principle applies for retrieving data from a database table. Without an index, the database system reads through the entire table (this process is called a 'table scan') to locate the desired information. With the proper index in place, the database system can then first go through the index to find out where to retrieve the data, and then go to these locations directly to get the needed data. This is much faster. CREATE TABLE subscribers ( subscriberid INT PRIMARY KEY, emailaddress VARCHAR(255), firstname VARCHAR(255), lastname VARCHAR(255) ); if we want to quickly find an email address, we create an index on the emailaddress field: CREATE INDEX subscriber_email ON subscribers(emailaddress) and any time we want to find an email address: SELECT firstname, lastname FROM subscribers WHERE emailaddress=’’; it will be quite quick to find. Question 2: Under what circumstances, we will use foreign key? Give an example. ANS-- A foreign key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted. For example, we have two tables, a CUSTOMER table that includes all customer data, and an ORDERS table that includes all customer orders. The constraint here is that all orders must be associated with a customer that is already in the CUSTOMER table. In this case, we will place a foreign key on the ORDERS table and have it relate to the primary key of the CUSTOMER table. This way, we can ensure that all orders in the ORDERS table are related to a customer in

the CUSTOMER table. In other words, the ORDERS table cannot contain information on a customer that is not in the CUSTOMER table. The structure of these two tables will be as follows: Table CUSTOMER column name Characteristic SID Primary Key Last_Name First_Name Table ORDERS column name Characteristic Order_ID Primary Key Order_Date Customer_SID Foreign Key Amount In the above example, the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the CUSTOMER table. Question3: Produces a new relation with some of the attributes of relation and remove duplicate tuples, give two different examples. ANS- Projection operation works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminate duplicates. student lname smith bond smith Fname Susan James Susan Stuid 1234 5555 8762 major comp maths comp

Π lname,fname(student) smith Susan bond James The union of two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated. R and S must be union compatible.

. PART-B Question 4: Can we use a virtual table for security purpose? Justify your answer and give an example to create a view. ANS-4 A view is a virtual tablewhich is mapped to the underlying physical tables using procedures stored in the server. This presents a simpler picture of the database to the user, and also restricts access to only those elements included in the view. Views can also be set up to restrict the rows of the virtual table to those meeting certain criteria.Thus, it will be possible to limit the patients in a study to an approved population. Example of view TABLE Customer (First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date date) and we want to create a view called V_Customer that contains only the First_Name, Last_Name, and Country columns from this table, we would type in, CREATE VIEW V_Customer AS SELECT First_Name, Last_Name, Country FROM Customer Now we have a view called V_Customer with the following structure: View V_Customer (First_Name char(50), Last_Name char(50), Country char(25))

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) b) Using a nested query in the from clause Using a nested query in a Having clause ?

ANS- select branch-name, tot-balance from (select branch-name, sum (balance) from account group by branch-name) as branch-total(branch-name, tot-balance) where tot-balance ¡ ( select avg (tot-balance) from ( select branch-name, sum (balance) from account group by branch-name) as branch-total(branch-name, tot-balance) ) b. Using a nested query in a having clause. select branch-name, sum (balance) from account group by branch-name having sum (balance) ¡ ( select avg (tot-balance) from ( select branch-name, sum (balance) from account group by branch-name) as branch-total(branch-name, tot-balance) ) 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: a) List all the employees whose location is ‘Pune’ and dept_name is ‘Computer’. ANS-select * from employee where location=’pune’ and dept_name=’computer’. b) Count the total number of departments. Also count the total number of employees whose dept_name is ‘computer’ ANS-Select count(*) from department. Select count(emp_name) from employee groupby emp_name having dept_name=’computer’.this can only done by making emp_name is unique in first table and dept_name is foreign key.since foreign key also refers the emp_name by making it unique in employee table. c) Add a new department ‘R&D’ in the database. ANS-update department set dept_name where dept_name=’R&D’ d) List the names of employees whose salary is greater than 50000 and dept_ name is ‘computer'. ANS-Select emp_name from employee where salary>=5000 and dept_name=’computer’. e) List the names of employees having maximum and minimum salary ANS-Select emp_name from employee groupby emp_name having salary=’maximum or minimum’.

f) Now change the department name from ‘computer’ to ‘software design’, wherever applicable in the table. ANS-Update department set dept_name=’software design’ where dept_name=computer

Sign up to vote on this title
UsefulNot useful